14

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
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Martin
  • 3,396
  • 5
  • 41
  • 67
  • 1
    The previously existing command line window, from which you execute your script, closes? I've never seen that happen. Or do you mean, a VBScript executed from Windows Explorer? What version of Windows are you using? – Jean-François Corbett Sep 23 '11 at 09:12
  • Jean-Francois Corbett is right, I can't see the cmd window that you executed the script inside closing, surely it should just give you a new cmd line? – Bali C Sep 23 '11 at 09:19
  • @Jean-FrancoisCorbett i am not talking about the window from which i execute the script. When the script starts executing a new window pops up and closes quickly. I am using windows 7. – Martin Sep 23 '11 at 09:25
  • Are you specifically opening a command-line window in your script? Or is this just something that always happens automatically when you run a script? (That doesn't happen for me.) Please show us (the relevant portions of) your code. – Jean-François Corbett Sep 23 '11 at 09:52
  • 1
    @Jean-FrancoisCorbett Set objShell = WScript.CreateObject("WScript.shell") objShell.Run "SyncToyCmd.exe -R", 1, True – Martin Sep 23 '11 at 10:54
  • Thanks. Now we can answer... see below. – Jean-François Corbett Sep 23 '11 at 11:20

3 Answers3

24

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

More info on the use of the & versus the && command separators here.

feetwet
  • 3,248
  • 7
  • 46
  • 84
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
4

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
Tao
  • 13,457
  • 7
  • 65
  • 76
-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
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • Why throw the user into a panic with a "this message will self-destruct in 5 seconds"? Better to let the user choose when it will close. – Jean-François Corbett Sep 23 '11 at 09:49
  • If you give them enough time it won't make them "panic" and by using a message like I suggested the user would know when it would close, also using a messagebox might obscure the view of the results of the app meaning that they have to move it out of the way to see anything – Bali C Sep 23 '11 at 10:06
  • Oops, given the clarified question, this won't work, and neither will my original answer... – Jean-François Corbett Sep 23 '11 at 11:19
  • -1 because the WScript.Shell object is not the WScript object (so the first statement clobbers the 'real' WScript that has a .Sleep method) and has no .Sleep method. – Ekkehard.Horner Sep 23 '11 at 11:33
  • @Ekkehard.Horner That was my answer before the original question was edited/revised as was Jean's first answer – Bali C Sep 23 '11 at 12:48
  • 1
    @Bali C: the order of publication is irrelevant - your code is wrong and should be corrected. – Ekkehard.Horner Sep 23 '11 at 13:01