Forum Discussion
AlexKaras
10 years agoCommunity Hero
Hi,
Assuming that the process exits when done, you may consider the function that was created ages ago:) (VBScript) :
'-----------------------------------------------------------------------------
' Function starts program and waits until it finishes
Function ExecAndWaitForEnd(ByVal strProg, ByVal strArgs)
Dim WshShell
Dim oExec
Dim strCommand
Dim allOutput
ExecAndWaitForEnd = -1
' strProg = Utilities.AnsiQuotedStr(Trim(strProg), """")
strProg = Trim(strProg)
If (Left(strProg, 1) <> """") Then
strProg = """" & strProg & """"
End If
strCommand = strProg & " " & strArgs
' oLogFile.WriteLineToLog (cLOGSTRINFO & "Executing: " & strCommand & "...")
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec(strCommand)
Do While oExec.Status = 0
BuiltIn.Delay 500
Loop
allOutput = ReadAllFromAny(oExec)
Log.Message "ExecAndWaitForEnd(): Call exit code=" & oExec.ExitCode, _
"CmdLine: " & strCommand & vbCrLf & vbCrLf & "Output: " & allOutput
ExecAndWaitForEnd = oExec.ExitCode
' oLogFile.WriteLineToLog (cLOGSTRINFO & "Done. (Return code = " & oExec.ExitCode & ")")
Set oExec = Nothing
Set WshShell = Nothing
End Function
'-----------------------------------------------------------------------------
Function ReadAllFromAny(ByRef oExec)
If Not oExec.StdOut.AtEndOfStream Then
ReadAllFromAny = oExec.StdOut.ReadAll
Exit Function
End If
If Not oExec.StdErr.AtEndOfStream Then
ReadAllFromAny = cLOGSTRERROR & "STDERR: " & oExec.StdErr.ReadAll
Exit Function
End If
ReadAllFromAny = -1
End Function
'-----------------------------------------------------------------------------
Call sample:
Call ExecAndWaitForEnd("cmd.exe", _
"/c copy /y " + Utilities.AnsiQuotedStr(strSourceDB, """") + " " + _
Utilities.AnsiQuotedStr(strTargetDB, """"));