I am trying to call a vbs file from another. The called file executes. However the control is not returning to the original vbs file. This file appears to be running as I see wscript process in task manager. But i don't see the output - the step after the run command. Any help/suggestion would be appreciated.

1.) vbs file 1 (original vbs file test3.vbs)

Set objShell = WScript.CreateObject ("WScript.shell")
strErrorCode  = objShell.run("cmd /K C:\temp\a\test2.vbs", 0, True)
msgbox "complete test3"
Set objShell = Nothing

2.) vbs file 2 (called vbs file - test2.vbs)

msgbox "in test2"
WScript.Sleep 10000
msgbox "complete - test2"

3.) Expected output :

in test2
complete - test2
complete test3

4.) actual:

in test2
complete - test2

1 Answers

1
JosefZ On Best Solutions

objShell.run("cmd /K C:\temp\a\test2.vbs", 0, True) never ends due to the /K switch:

cmd /?
Starts a new instance of the Windows command interpreter

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
    [[/S] [/C | /K] string]

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains

So cmd window remains open but it is hidden owing to the intWindowStyle parameter: 0 = Hides the window and activates another window, supposing the Run method syntax pattern as .Run(strCommand, [intWindowStyle], [bWaitOnReturn])

Use either

strErrorCode  = objShell.run("cmd /C C:\temp\a\test2.vbs", 0, True)

or

strErrorCode  = objShell.run("wscript.exe C:\temp\a\test2.vbs", 0, True)

or even

 strErrorCode  = objShell.run("C:\temp\a\test2.vbs", 0, True)