How to keep the VBScript command window open during execution

坚强是说给别人听的谎言 提交于 2019-11-27 06:05:53

问题


When I execute a VBScript, the command window that it creates closes quickly before the user gets a chance to read the output. How can I get the window to stay open without modifying windows registry?

This is the code:

Set objShell = WScript.CreateObject("WScript.shell") 
objShell.Run "SyncToyCmd.exe -R", 1, True

回答1:


You can send your execution command through the cmd.exe command interpreter, along with a pause command which will give the user a Press any key to continue . . . prompt to close the window.

objShell.run "%comspec% /c ""SyncToyCmd.exe -R & pause""", 1, True

Or to keep the window alive, use the /k flag instead of /c:

objShell.run "%comspec% /k SyncToyCmd.exe -R", 1, True

But beware, your VBScript will not continue (or terminate) until this cmd window is manually closed.

The %comspec% environment variable refers to the correct command to open the command interpreter as per your operating system. On my XP machine, for instance, %comspec% is equal to C:\WINDOWS\system32\cmd.exe.

See cmd.exe documentation here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true

For more info on the use of the & versus the && command separator: https://support.microsoft.com/en-us/kb/279253




回答2:


Assuming that it's the popped-up command window that you want to keep open (rather than the one running your VBScript), you can use CMD.exe's Pause command to achieve this:

Set objShell = WScript.CreateObject("WScript.shell") 
objShell.Run "cmd.exe /C ""SyncToyCmd.exe -R & Pause"" ", 1, True



回答3:


Make it sleep for a while, maybe tell the user it will close in 5 seconds?

Set WScript = CreateObject("WScript.Shell") 
WScript.Sleep 5000


来源:https://stackoverflow.com/questions/7526598/how-to-keep-the-vbscript-command-window-open-during-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!