如何在调用另一个函数之前等待命令行活动完成?

时间:2014-10-22 12:57:26

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

这是我的代码。我们为msbuild进程使用一组命令。根据环境,命令和其他活动会有所不同。所以,我提出了以下内容。但是,有一些命令需要一段时间(约1米)完成。我的脚本不会等待。而且,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
    

1 个答案:

答案 0 :(得分:1)

首先,请勿使用SendKeys。将所有命令放在批处理文件中并调用它。

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

START /WAIT MyProgram.exe