I wonder if I can execute some programs for different cmd's using .bat file. Look at this example .bat file:
start cmd //number one
start cmd //number two
ping localhost //call in number one
ping 192.168.1.100 //call in number two
I know that both will be executed in main window (the window where I started .bat file), but I think its easy to get the idea. This code is quite useless, but its just an example.
Thanx for all replies.
EDIT: I know about /k switch, but any way to do this not using it?
You can start your commands with
start cmd /k ping localhost
start cmd /k ping 192.168.1.100
That will start two new command line prompts, run the ping command in each one seperately and both windows and the /k switch will make them stay open afterwards.
Ah, posted before your edit ... ;) The only way to interact with a shell is giving it a command to be executed when it starts. There is no way to have interaction between shells
CMD.EXE has two parameters, /C and /K, which let you specify a command to execute. /C closes the window when the command is finished, whereas /K keeps it running.
If you want to excute multiple commands within a single window, you'll need to concatenate them with && or similar - this will require quoting; CMD /? will tell you all the details on that - or you can have it start a batch file containing the commands.
Related
Say, if I have
foo.exe
bar.exe
baz.exe
How do I run all of them from a batch file asynchronously, i.e. without waiting for the previous program to stop?
Using the START command to run each program should get you what you need:
START "title" [/D path] [options] "command" [parameters]
Every START invocation runs the command given in its parameter and returns immediately, unless executed with a /WAIT switch.
That applies to command-line apps. Apps without command line return immediately anyway, so to be sure, if you want to run all asynchronously, use START.
Combining a couple of the previous answers, you could try start /b cmd /c foo.exe.
For a trivial example, if you wanted to print out the versions of java/groovy/grails/gradle, you could do this in a batch file:
#start /b cmd /c java -version
#start /b cmd /c gradle -version
#start /b cmd /c groovy -version
#start /b cmd /c grails -version
If you have something like Process Explorer (Sysinternals), you will see a few child cmd.exe processes each with a java process (as per the above commands). The output will print to the screen in whatever order they finish.
start /b : Start application without creating a new window. The
application has ^C handling ignored. Unless the application
enables ^C processing, ^Break is the only way to interrupt
the application
cmd /c : Carries out the command specified by string and then terminates
You can use the start command to spawn background processes without launching new windows:
start /b foo.exe
The new process will not be interruptable with CTRL-C; you can kill it only with CTRL-BREAK (or by closing the window, or via Task Manager.)
Create a batch file with the following lines:
start foo.exe
start bar.exe
start baz.exe
The start command runs your command in a new window, so all 3 commands would run asynchronously.
Use the START command:
start [programPath]
If the path to the program contains spaces remember to add quotes. In this case you also need to provide a title for the opening console window
start "[title]" "[program path]"
If you need to provide arguments append them at the end (outside the command quotes)
start "[title]" "[program path]" [list of command args]
Use the /b option to avoid opening a new console window (but in that case you cannot interrupt the application using CTRL-C
There's a third (and potentially much easier) option. If you want to spin up multiple instances of a single program, using a Unix-style command processor like Xargs or GNU Parallel can make that a fairly straightforward process.
There's a win32 Xargs clone called PPX2 that makes this fairly straightforward.
For instance, if you wanted to transcode a directory of video files, you could run the command:
dir /b *.mpg |ppx2 -P 4 -I {} -L 1 ffmpeg.exe -i "{}" -quality:v 1 "{}.mp4"
Picking this apart, dir /b *.mpg grabs a list of .mpg files in my current directory, the | operator pipes this list into ppx2, which then builds a series of commands to be executed in parallel; 4 at a time, as specified here by the -P 4 operator. The -L 1 operator tells ppx2 to only send one line of our directory listing to ffmpeg at a time.
After that, you just write your command line (ffmpeg.exe -i "{}" -quality:v 1 "{}.mp4"), and {} gets automatically substituted for each line of your directory listing.
It's not universally applicable to every case, but is a whole lot easier than using the batch file workarounds detailed above. Of course, if you're not dealing with a list of files, you could also pipe the contents of a textfile or any other program into the input of pxx2.
I could not get anything to work I ended up just using powershell to start bat scripts .. sometimes even start cmd /c does not work not sure why .. I even tried stuff like start cmd /c notepad & exit
start-Process "c:\BACKUP\PRIVATE\MobaXterm_Portable\MobaXterm_Portable.bat" -WindowStyle Hidden
Need to heavily exercise an eight-core server under Windows. Found a Linux exercising test at Put server on heavy load for testing and would like to perform a similar workout using command prompt commands.
Solved: I created a batch file DO.BAT which contains one line
dir /s /r /on & do ^z
and then from within a command prompt window entered
start do
which opened a new command prompt window and launched DO.BAT to loop until a Ctrl-Break is entered. Then, repeated that until many windows were running.
Many thanks to Bali C who mentioned the start command in How to run multiple DOS commands in parallel?
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
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.
You could also create a shortcut to you batch script and then in the properties select to start the app minimized. It is explained here: CNET: How to automatically start a program minimized in Windows
Step 1: Right-click on the shortcut of the program you want to start minimized and select Properties.
Step 2: Click on the drop-down menu under Run.
Step 3: Select "Minimized," then click the OK button.
From win cmd:
start /b cmd.exe /c "ping 1.1.1.1 -n 1 -w 10000 & echo second command goes here"
runs both commands without opening another cmd window, leaving parent window unblocked for further commands without waiting.
[Context: I'm trying to create a shortcut to a .bat file with a relative "Start in" path as roughly described here and here.]
cmd.exe supports the /c switch. According to the documentation, this should cause it to "carry out the command and then terminate."
But the switch seems to be ignored when the command is a .bat file.
For example, if you create a shortcut with the following Target (to a normal, non-bat command):
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ notepad.exe test.txt"
Everything works as expected: Notepad opens and the console (shell) disappears. But if you replace the command above with a .bat file instead, like so:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat"
(where test.bat contains only "notepad.exe test.txt") Notepad opens as before but the console sticks around like an unwanted friend. Why? And more to the point, How do I make it go away?
UPDATE: I know I can use wscript, as in this solution, but then I lose the option of having a custom icon (I'm stuck with the default .vbs icon).
The start command begins a new process for the batch file. The original cmd.exe then terminates, but leaves the new process, which hangs around because it's waiting for notepad.exe to terminate.
Change your bat file contents to:
start "" notepad.exe test.txt
Then your batch file will not wait for notepad to exit before continuing execution.
Another thing to try:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat & exit"
The nuclear option would be to write a small program in the (compiled) language of your choice that launches the .bat file and then exits. Then you can give it a custom icon, and make it do whatever you like.
You might also take a look at Autoit from http://autoitscript.com as an alternative to batch. - the Run() command can do this kind of thing with better predictability. Since it makes an executable you can link this from a shortcut directly. You can also do a whole lot more of course, like run as a different user, insert delays or handle errors, that are hard to do with batch.
You don't need the full kit, just the Aut2EXE folder from the download will do.
BTW, build your exes without UPX compression as that leads to AV false positives.
I'm a little late but here is the answer.
The documentation for start states:
Syntax
START "title" [/D path] [options] "command" [parameters]
If command is an internal cmd command or a batch file then the command
processor is run with the /K switch to cmd.exe. This means that the
window will remain after the command has been run.
If start is used to execute a batch file, the opened cmd instance wont close.
You could also use call instead.
call C:\test.bat
I have a program that changes my desktop wallpaper by dragging the picture file onto it. I also have a wireless network program that can auto open programs everytime it connects to a certain network.
I want to change my desktop everytime it connects to a certain network, but running the wallpaper program doesn't do anything unless I drag the picture onto it. However, I can also run a cmd prompt "c:/program.exe picture.jpg"
I tried creating a batch file START C:/PROGRAM.EXE PICTURE.JPG, but it doesn't work.
So basically I am trying to create a program that can run the cmd prompt "c:/program.exe picture.jpg" - can you help, please?
Remove the "start" from the batch file, and make sure any paths with spaces in them are enclosed in quotes, otherwise they'll be broken into arguments.
For example:
"C:\Program Files\MyProgram.exe" "C:\Documents and Settings\Me\MyPicture.jpg"
A batch job should work. Try skipping that START from your example.
Start - Run - Type :
cmd /c "start /max ""C:\Program Files\MyProgram.exe"" ""C:\Documents and Settings\Me\MyPicture.jpg""" .
The cmd /c - starts a new cmd instance and quits