0

两部分问题:

  1. 我正在尝试编写一个循环运行的 VBscript,但是有一个消息框,用户可以随时使用它来中止序列。我知道如果你有一个带有 msgbox 的序列,脚本将停止执行,直到收到答案,但我可以将它作为下标运行,这样它就不会干扰主脚本吗?

当我使用以下脚本时,我从来没有看到 msgbox

function test()
msgbox ("test")
end function
wscript.sleep 1000
msgbox "done

我的印象是函数可以让你获得输入。这甚至可以用纯 vbscript 完成吗?

4

2 回答 2

0

不是我想要的,但这是我发现的解决方法。它会生成一个临时的 msgbox,并在一段时间后自行关闭。给用户一个 5 秒的窗口来中止每个循环的序列。

set infobox = createobject("Wscript.shell")
do while E<N+1
    E=E+1
    if InfoBox.Popup ("Click cancel to stop sequence", _ 
            5, "Abort Sequence?", 1) = 2 then
            E=N+1
    end if
    loop
于 2013-10-25T18:50:59.557 回答
0

这里的技巧是让第一个脚本创建并启动第二个脚本。第二个脚本将在后台运行,然后可以等待并终止初始脚本进程...这可以通过函数轻松完成,并且可以在脚本开始时调用。当您的主脚本结束时,它只会终止先前创建的第二个脚本。注意:创建的第二个脚本将在运行时自动删除。请参阅下面的脚本以获得一个很好的工作示例:

Dim iKillPID

'Start Kill Script At Start Of Script
iKillPID = KillPID()

For X = 10 To 0 Step -1
    WScript.Echo "Closing in " & X & " Seconds"
    WScript.Sleep 1000
Next

'Kill The Kill Script At End Of Script
GetObject("winmgmts:root\cimv2:Win32_Process.Handle='" & iKillPID & "'").Terminate
MsgBox "This Script is Complete"

'$$$$$$$$$$
Function KillPID()
Dim strKillScriptPath, strKillCommand, KillFile, StrFileKill, iScriptPID
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Generates a Unique Temp File Name In The Same Directory As The Current Script
strKillScriptPath = objFSO.GetParentFolderName(WScript.ScriptFullName) & Chr(92) & Replace(objFSO.GetTempName, ".tmp", ".vbs")
    'Command Line To New Kill Script
strKillCommand = "WScript.exe " & Chr(34) & strKillScriptPath & Chr(34)
    'This part gets the Process ID of the Current Running Script
iScriptPID = GetObject("winmgmts:root\cimv2:Win32_Process.Handle='" & _
 CreateObject("WScript.Shell").Exec("CMD /C ping 127.0.0.1 -n 2 > nul").ProcessID & "'").ParentProcessID
    'String With Kill File Code (Script Process ID Included)
StrFileKill = _
"Const iKillProc = " & iScriptPID & vbCrLf & _
"Dim objFSO: Set objFSO = CreateObject(" & Chr(34) & "Scripting.FileSystemObject" & Chr(34) & ")" & vbCrLf & _
"objFSO.DeleteFile WScript.ScriptFullName, True" & vbCrLf & _  '<-- Deletes itself immediately upon running
"On Error Resume Next" & vbCrLf & _
"Set objKillProc = Nothing" & vbCrLf & _
"Set objKillProc = GetObject(" & Chr(34) & "winmgmts:root\cimv2:Win32_Process.Handle='" & Chr(34) & " & iKillProc & " & Chr(34) & "'" & Chr(34) & ")" & vbCrLf & _
"If objKillProc Is Nothing Then" & vbCrLf & _
"    MsgBox " & Chr(34) & "The Process Is Not Running" & Chr(34) & vbCrLf & _
"    WScript.Quit" & vbCrLf & _
"End If" & vbCrLf & _
"MsgBox " & Chr(34) & "Click OK To Kill The Script Process" & Chr(34) & vbCrLf & _
"Call KillProcess(iKillProc)" & vbCrLf & _
"WScript.Quit" & vbCrLf & _
"Sub KillProcess(iProcID)" & vbCrLf & _
"Dim objKillProc, strParentProc" & vbCrLf & _
"On Error Resume Next" & vbCrLf & _
"Set objKillProc = Nothing" & vbCrLf & _
"Set objKillProc = GetObject(" & Chr(34) & "winmgmts:root\cimv2:Win32_Process.Handle='" & Chr(34) & " & iProcID & " & Chr(34) & "'" & Chr(34) & ")" & vbCrLf & _
"If Err = 0 And Not objKillProc Is Nothing Then" & vbCrLf & _
"    If StrComp(objKillProc.Name, " & Chr(34) & "cmd.exe" & Chr(34) & ", 1) = 0 Or _" & vbCrLf & _
"     StrComp(objKillProc.Name, " & Chr(34) & "cscript.exe" & Chr(34) & ", 1) = 0 Or _" & vbCrLf & _
"     StrComp(objKillProc.Name, " & Chr(34) & "wscript.exe" & Chr(34) & ", 1) = 0 Then" & vbCrLf & _
"        strParentProc = objKillProc.ParentProcessID" & vbCrLf & _
"        objKillProc.Terminate()" & vbCrLf & _
"    Call KillProcess(strParentProc)" & vbCrLf & _
"    End If" & vbCrLf & _
"End If" & vbCrLf & _
"Set strParentProc = Nothing" & vbCrLf & _
"Err.Clear" & vbCrLf & _
"End Sub"
    'Write the Code To File
Set KillFile = objFSO.CreateTextFile(strKillScriptPath, True)
KillFile.WriteLine StrFileKill
KillFile.Close
Set KillFile = Nothing
WScript.Sleep 250
    'Execute The Script and Return the Script Process ID So You Can Kill It When The Script Ends
KillPID = CreateObject("WScript.Shell").Exec(strKillCommand).ProcessID
End Function
'$$$$$$$$$$

此外,如果您使用 CScript 作为 VBS 的脚本引擎,我相信您可以通过在命令提示符窗口中按 CTRL + C 来停止脚本。

现在,如果您有超级动力,您可以创建一个执行相同操作的 HTA,但显示一个用户窗体或自定义 Internet Explorer 窗口以供单击,它还可以循环检查进程是否仍在运行,并在脚本完成时自行关闭并且该过程不再运行。你也可以添加漂亮的颜色和一切!

于 2013-10-25T22:27:48.060 回答