【问题标题】:How to wait for command line activity to finish before calling another function?如何在调用另一个函数之前等待命令行活动完成?
【发布时间】:2014-10-22 12:57:26
【问题描述】:

这是我的代码。我们为我们的 msbuild 进程使用了​​一组命令。根据环境的不同,命令和其他活动会有所不同。所以,我想出了如下的东西。但是,有些命令需要一段时间(大约 1m)才能完成。我的脚本不等待。而且,MoveStagedFolders() 函数不会启动。我单独执行了这个函数,它可以工作。所以我的问题是:

  1. 为什么没有调用 MoveStagedFolders()?
  2. 如何等到命令行活动完成。

        Option Explicit
        Dim wshShell, environment, strBuildDirectory, strWebsiteDirectory
        On Error Resume Next
        strBuildDirectory = "someFoldersPath\*"
        strWebsiteDirectory = "\\DestinationPath\"
        environment = InputBox("Please Enter the Environment.")
    
        if  Trim(environment) <> "" then
            Set wshShell = wscript.CreateObject("wscript.Shell")
            wshShell.Run "C:\Windows\system32\cmd.exe" 
            WScript.Sleep 500   
            wshShell.sendkeys "cd c:\Program Files +9x86+0\Microsoft Visual Studio 13.0\Common7\Tools"
            wshShell.sendkeys "{ENTER}"
            wshShell.sendkeys "vsvars32.bat"
            wshShell.sendkeys "{ENTER}"
            wshShell.sendkeys "cd c:\working\develop"
            wshShell.sendkeys "{ENTER}"
            wshShell.sendkeys "msbuild/t:clean"
            wshShell.sendkeys "{ENTER}"
            wshShell.sendkeys "msbuild/p:configuration=" & environment
            wshShell.sendkeys "{ENTER}" 
            'I need to wait about a minute here.
            if UCase(environment) = "QA" then
                'Restart IIS
                wshShell.sendkeys "iisreset /restart localhost"
                wshShell.sendkeys "{ENTER}"
    
            elseif environment = "123" then
                'I need to move some folders to shared folder
                Call MoveStagedFolders()
                'Wait until the moving is done
                if MoveStagedFolders = true then
                    'MsgBox "Restarting IIS"
                    wshShell.sendkeys "iisreset /restart devIp"
                    wshShell.sendkeys "{ENTER}"
                end if
            end if
        else
            MsgBox "No environment was entered. Build may not succeed."
        end if
    
        Function MoveStagedFolders()
            With CreateObject("Scripting.FileSystemObject")
                'Need to overwrite folders
                .CopyFolder strBuildDirectory, strWebsiteDirectory, true
            End With
            MoveStagedFolders = true
        End Function
    

【问题讨论】:

  • 我不认为SendKeys 是您的最佳选择。您可以将所有命令放在BAT 文件中并从您的VBScript 调用该批处理文件吗?如果您使用Run 函数调用它并将True 作为第三个参数传递,您的VBScript 将等待批处理文件完成后再继续。
  • 我知道这不是最佳选择。我当然试一试。感谢您的快速回答

标签: vb.net powershell batch-file command-line vbscript


【解决方案1】:

首先,不要为此使用SendKeys。将所有命令放在一个批处理文件中并调用它。

其次,您可以使用START 命令让批处理文件等待命令完成,如下所示:

START /WAIT MyProgram.exe

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    相关资源
    最近更新 更多