How to avoid command window popping on cmd.exe
The simplest option is to start the thing minimized. Shortcut Target:
cmd /c START /MIN \path\to\test.bat
or
cmd /c START /MIN cmd /k ( ping 1.1.1.1 -w 10000 -n 1 && @ECHO All OK)
It's not hidden or anything, but it doesn't show up on the desktop or—worse—steal the focus.
If you want the window to go away on its own, "cmd /c ..." will make that happen. "cmd /k ..." will leave the window open.
Plan B is referred to by @CodyGray in the SU link he posted. There are programs that don't open windows, like wperl, pythonw, or wscript (natively available on Windows). If you can pass your command through to one of those things, then you could effectively double-click an icon and have it run "silently."
If Perl's available, I'd certainly go with that because you can craft some pretty powerful one-liners that won't require creating other files.
wperl -MWin32 -MNet::Ping -e "$p=Net::Ping->new('icmp',10000); if ($p->ping('192.168.1.1')) { Win32::MsgBox('Ping Successful', 1 + MB_OK, 'All Good'); }"
In your example, you're chaining commands together, the latter is a notification. If you don't want to have a window open for the first command, it would be awkward to do it for the second when you're notifying the user of something. Having the process call "cmd /c start cmd /c @echo Everything's OK" would probably do it, but using CMD windows for user notification is probably not something the HCI guys would smile at.
No, all batch files open in command-line windows; this has nothing to do with the presence of cmd.exe
in your particular file. A batch file is simply a number of command-line commands, one per line.
I don't understand why you write test.bat
the way you do. I'd rather expect
ping 1.1.1.1 -n 1 -w 10000
echo second command goes here
If, for some bizzare reason, you really need to use only a single line, you can simply do
ping 1.1.1.1 -n 1 -w 10000 && echo second command goes here
As Andreas Rejbrand already explained, the command prompt window is not from the explicit cmd.exe
invocation within your script but from executing the .bat
script itself. (And despite your claim, you haven't provided any evidence why explicitly invoking cmd.exe
is necessary. The whole point of a .bat
script is to batch commands together.)
That said, the silentbatch program that Paul Miner and I wrote can execute batch scripts and suppress the command prompt window. To use it, you would have to create a Windows shortcut that invokes silentbatch.exe test.bat
and double-click on that rather than double-clicking on test.bat
directly, however.