当只运行一条objShell.Run
行时,我有这个代码非常适合我。
Set objShell = WScript.CreateObject("WScript.Shell")
retval = objShell.Run ("cmd /c title Phase 1 & robocopy C:\this C:\that /MIR /L",1,True)
If retval < 0 Then
MsgBox "cmd aborted, return value is: " & retval
ElseIf retval > 7 Then
MsgBox "robocopy error, return value is: " & retval
Else
MsgBox "robocopy successful, return value is: " & retval
End If
当我想拥有多条objShell.Run
行时,我该怎么办?
E.g。
retval = objShell.Run ("cmd /c title Phase 1 & robocopy C:\this C:\that /MIR /L",1,True)
retval = objShell.Run ("cmd /c title Phase 2 & robocopy C:\thistoo C:\thattoo /MIR /L",1,True)
retval = objShell.Run ("cmd /c title Phase 3 & robocopy C:\andthis C:\andthat /MIR /L",1,True)
我可以使每个retval变量唯一,例如retval1
,retval2
,retval3
并迭代[If..Then..Else]语句,但这感觉很笨,可能不是最好的方法。
对我而言,最好的方法是MsgBox
显示&#34;报告&#34;关于每个objShell.Run
行,当它们完成时。除非所有行都成功,否则MsgBox "all done"
会很好。
我需要从哪个方向开始搜索?函数,数组?
答案 0 :(得分:1)
我从未使用过该对象WScript.Shell,
但看了你的代码,样本和解释。 我想这就是你的意思?
'-> Initialise
SequenceSteps = 3
Result = ""
ErrString = ""
MyCommandSequence(0) = "cmd /c title Phase 1 & robocopy C:\this C:\that /MIR /L"
MyCommandSequence(1) = "cmd /c title Phase 2 & robocopy C:\thistoo C:\thattoo /MIR /L"
MyCommandSequence(2) = "cmd /c title Phase 3 & robocopy C:\andthis C:\andthat /MIR /L"
'-> Process
ErrorOccurred = False
For Counta = 0 To SequenceSteps - 1
Set objShell = WScript.CreateObject("WScript.Shell")
retval = objShell.Run (MyCommandSequence(Counta), 1, True)
'-> process request result
If retval < 0 Then
ErrString = "cmd aborted, return value is: " & retval
'** NEW CODE LINE **
Exit For
ElseIf retval > 7 Then
ErrString = "robocopy error, return value is: " & retval
'** NEW CODE LINE **
Exit For
Else
Result = Result & "robocopy successful, return value is: " & retval & vbcrlf
End If
Next
'-> Display Accordingly
If Trim(ErrString) <> "" Then
MsgBox Result & vbcrlf & ErrString
Else
MsgBox "All Done"
End if
重新编辑的代码