views:

14274

answers:

5

I know I have already answered a similar question (Running Batch File in background when windows boots up), but this time I need to launch a batch:

  • from another batch
  • without any DOS windows displayed
  • with all arguments passed to the invisible batch

The first batch is executed in a DOS windows. However, I do not want the second batch (launch by the first in a asynchronous way) to display also a DOS windows.

I have come up with a vbs script which does just that and I put the script as an answer for others to refer to, but if you have other ideas/solutions, feel free to contribute. I will choose an official answer amongst your propositions.


Thank you all for the answers. From what I understand, if I need to asynchronously call a script to run in a invisible mode:

  • from a second script already in a DOS windows, start /b is enough
  • from Windows, without triggering a second window, my solution is still valid.
+12  A: 

Here is a possible solution:

From your first script, call your second script with the following line:

wscript.exe invis.vbs run.bat %*

Actually, you are calling a vbs script with:

  • the [path]\name of your script
  • all the other arguments needed by your script (%*)

Then, invis.vbs will call your script with the Windows Script Host Run() method, which takes:

  • intWindowStyle : 0 means "invisible windows"
  • bWaitOnReturn : false means your first script does not need to wait for your second script to finish

Here is invis.vbs:

set args = WScript.Arguments
num = args.Count

if num = 0 then
    WScript.Echo "Usage: [CScript | WScript] invis.vbs aScript.bat <some script arguments>"
    WScript.Quit 1
end if

sargs = ""
if num > 1 then
    sargs = " "
    for k = 1 to num - 1
     anArg = args.Item(k)
     sargs = sargs & anArg & " "
    next
end if

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run """" & WScript.Arguments(0) & """" & sargs, 0, False
VonC
2k views and what a great post + answer, more people should be up voting this thread
Ric Tokyo
+7  A: 

Do you need the second batch file to run asynchronously? Typically one batch file runs another synchronously with the call command, and the second one would share the first one's window.

You can use start /b second.bat to launch a second batch file asynchronously from your first that shares your first one's window. If both batch files write to the console simultaneously, the output will be overlapped and probably indecipherable. Also, you'll want to put an exit command at the end of your second batch file, or you'll be within a second cmd shell once everything is done.

P Daddy
Correct. I believed start /b would open a new windows, but if executed from a DOS windows, it does share the same windows.
VonC
+2  A: 

In the other question I suggested autoexnt. That is also possible in this situation. Just set the service to run manually (ie not automatic at startup). When you want to run your batch, modify the autoexnt.bat file to call the batch file you want, and start the autoexnt service.

The batchfile to start this, can look like this (untested):

echo call c:\path\to\batch.cmd %* > c:\windows\system32\autoexnt.bat
net start autoexnt

Note that batch files started this way run as the system user, which means you do not have access to network shares automatically. But you can use net use to connect to a remote server.

You have to download the Windows 2003 Resource Kit to get it. The Resource Kit can also be installed on other versions of windows, like Windows XP.

Wimmel
Interesting, but a tight overkill for this scenario.
VonC
+2  A: 

Convert the batch file to an exe, e.g. with Bat To Exe Converter, and choose the option to run it as a ghost application, i.e. no window.

Rob Kam
Possible, but I will try to avoid any extra step in this instance.
VonC
A: 

Run it under a different user name, using "runas" or by scheduling it under a different user in Windows Scheduled Tasks.

JosephStyons
Possible, but not exactly what I am after (direct asynchronous call in background)
VonC