Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid command window popping on cmd.exe

I have a command as:

cmd.exe /c ping 1.1.1.1 -n 1 -w 10000 && echo second command goes here

But when executed it opens a command window. Is there a way to avoid the command window from popping up?

PS: I cannot remove cmd.exe from there. As you can see, I am trying to tie two commands one after the other in the same string.

Thanks.

Edit: Sorry. Its not a bat file. I want to execute 2 commands in the "UninstallString" of msiexec. I was trying so many things that my question got a bit carried away.

The command is:

msiexec <product> <package> && reg delete /xxx
like image 797
sambha Avatar asked Jun 21 '13 21:06

sambha


2 Answers

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.

like image 129
mojo Avatar answered Oct 16 '22 15:10

mojo


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
like image 2
Andreas Rejbrand Avatar answered Oct 16 '22 15:10

Andreas Rejbrand