- Command to run a .bat file
- 4 Answers 4
- 1. RUN the batch file with full path
- 2. CALL the batch file with full path
- 3. Change directory and RUN batch file with one command line
- 4. Change directory and CALL batch file with one command line
- 5. Change directory and CALL batch file with keeping current environment with one command line
- Running Windows batch file commands asynchronously
- 7 Answers 7
- How to run multiple .BAT files within a .BAT file
- 18 Answers 18
- using «&«
- Using CALL
- CMD /C , Pipes , FOR /F
- START
- SCHTASKS
Command to run a .bat file
I’m trying to make my Visual Studio build script execute a .bat file that does something important.
Here is what I’m want to do right now:
But it doesn’t work.
I have to do this to make it work:
But this is pretty difficult to add to the Visual Studio script.
How can I do this in one single line?
4 Answers 4
«F:\- Big Packets -\kitterengine\Common\Template.bat» maybe prefaced with call (see call /? ). Or Cd /d «F:\- Big Packets -\kitterengine\Common\» & Template.bat .
CMD Cheat Sheet
CMD.exe
First thing to remember its a way of operating a computer. It’s the way we did it before WIMP (Windows, Icons, Mouse, Popup menus) became common. It owes it roots to CPM, VMS, and Unix. It was used to start programs and copy and delete files. Also you could change the time and date.
For help on starting CMD type cmd /? . You must start it with either the /k or /c switch unless you just want to type in it.
Getting Help
For general help. Type Help in the command prompt. For each command listed type help (eg help dir ) or /? (eg dir /? ).
Some commands have sub commands. For example schtasks /create /? .
The NET command’s help is unusual. Typing net use /? is brief help. Type net help use for full help. The same applies at the root — net /? is also brief help, use net help .
References in Help to new behaviour are describing changes from CMD in OS/2 and Windows NT4 to the current CMD which is in Windows 2000 and later.
WMIC is a multipurpose command. Type wmic /? .
Punctuation
Naming Files
Starting a Program
See start /? and call /? for help on all three ways.
There are two types of Windows programs — console or non console (these are called GUI even if they don’t have one). Console programs attach to the current console or Windows creates a new console. GUI programs have to explicitly create their own windows.
If a full path isn’t given then Windows looks in
The directory from which the application loaded.
The current directory for the parent process.
Windows NT/2000/XP: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32.
Windows NT/2000/XP: The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The directories that are listed in the PATH environment variable.
Specify a program name
This is the standard way to start a program.
In a batch file the batch will wait for the program to exit. When typed the command prompt does not wait for graphical programs to exit.
If the program is a batch file control is transferred and the rest of the calling batch file is not executed.
Use Start command
Start starts programs in non standard ways.
Start starts a program and does not wait. Console programs start in a new window. Using the /b switch forces console programs into the same window, which negates the main purpose of Start.
Start uses the Windows graphical shell — same as typing in WinKey + R (Run dialog). Try
Also program names registered under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths can also be typed without specifying a full path.
Also note the first set of quotes, if any, MUST be the window title.
Use Call command
Call is used to start batch files and wait for them to exit and continue the current batch file.
Other Filenames
Typing a non program filename is the same as double clicking the file.
Keys
Ctrl + C exits a program without exiting the console window.
For other editing keys type Doskey /? .
↑ and ↓ recall commands
ESC clears command line
F7 displays command history
ALT + F7 clears command history
F8 searches command history
F9 selects a command by number
ALT + F10 clears macro definitions
Also not listed
Ctrl + ← or → Moves a word at a time
Ctrl + Backspace Deletes the previous word
Home Beginning of line
End End of line
Ctrl + End Deletes to end of line
There are many possibilities to solve this task.
1. RUN the batch file with full path
The easiest solution is running the batch file with full path.
Once end of batch file Template.bat is reached, there is no return to previous script in case of the command line above is within a *.bat or *.cmd file.
The current directory for the batch file Template.bat is the current directory of the current process. In case of Template.bat requires that the directory of this batch file is the current directory, the batch file Template.bat should contain after @echo off as second line the following command line:
Run in a command prompt window cd /? for getting displayed the help of this command explaining parameter /D . change to specified directory also on a different drive.
Run in a command prompt window call /? for getting displayed the help of this command used also in 2., 4. and 5. solution and explaining also %
dp0 . drive and path of argument 0 which is the name of the batch file.
2. CALL the batch file with full path
Another solution is calling the batch file with full path.
The difference to first solution is that after end of batch file Template.bat is reached the batch processing continues in batch script containing this command line.
For the current directory read above.
3. Change directory and RUN batch file with one command line
There are 3 operators for running multiple commands on one command line: & , && and || .
For details see answer on Single line with multiple commands using Windows batch file
I suggest for this task the && operator.
As on first solution there is no return to current script if this is a *.bat or *.cmd file and changing the directory and continuation of batch processing on Template.bat is successful.
4. Change directory and CALL batch file with one command line
This command line changes the directory and on success calls the batch file.
The difference to third solution is the return to current batch script on exiting processing of Template.bat .
5. Change directory and CALL batch file with keeping current environment with one command line
The four solutions above change the current directory and it is unknown what Template.bat does regarding
- current directory
- environment variables
- command extensions state
- delayed expansion state
In case of it is important to keep the environment of current *.bat or *.cmd script unmodified by whatever Template.bat changes on environment for itself, it is advisable to use setlocal and endlocal .
Run in a command prompt window setlocal /? and endlocal /? for getting displayed the help of these two commands. And read answer on change directory command cd ..not working in batch file after npm install explaining more detailed what these two commands do.
Now there is only & instead of && used as it is important here that after setlocal is executed the command endlocal is finally also executed.
Running Windows batch file commands asynchronously
How do I run all of them from a batch file asynchronously, i.e. without waiting for the previous program to stop?
7 Answers 7
Using the START command to run each program should get you what you need:
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 .
30 copy FILA DESTA and I wan them to be async, but I also want to have some idea when they’re all done. – Doug Jul 17 ’17 at 9:36
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:
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.
You can use the start command to spawn background processes without launching new windows:
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:
The start command runs your command in a new window, so all 3 commands would run asynchronously.
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
If you need to provide arguments append them at the end (outside the command quotes)
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:
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.
How to run multiple .BAT files within a .BAT file
I’m trying to get my commit-build.bat to execute other .BAT files as part of our build process.
Content of commit-build.bat :
This seems simple enough, but commit-build.bat only executes the first item in the list ( msbuild.bat ).
I have run each of the files separately with no problems.
18 Answers 18
When not using CALL, the current batch file stops and the called batch file starts executing. It’s a peculiar behavior dating back to the early MS-DOS days.
All the other answers are correct: use call. For example:
History
In ancient DOS versions it was not possible to recursively execute batch files. Then the call command was introduced that called another cmd shell to execute the batch file and returned execution back to the calling cmd shell when finished.
Obviously in later versions no other cmd shell was necessary anymore.
In the early days many batch files depended on the fact that calling a batch file would not return to the calling batch file. Changing that behaviour without additional syntax would have broken many systems like batch menu systems (using batch files for menu structures).
As in many cases with Microsoft, backward compatibility therefore is the reason for this behaviour.
Tips
If your batch files have spaces in their names, use quotes around the name:
By the way: if you do not have all the names of the batch files, you could also use for to do this (it does not guarantee the correct order of batch file calls; it follows the order of the file system):
You can also react on errorlevels after a call. Use:
to give back an errorlevel. 0 denotes correct execution. In the calling batch file you can react using
Use if errorlevel 1 if you have an older Windows than NT4/2000/XP to catch all errorlevels 1 and greater.
To control the flow of a batch file, there is goto 🙁
As others pointed out: have a look at build systems to replace batch files.
If we want to open multiple command prompts then we could use
/k : is compulsory which will execute.
Launching many command prompts can be done as below.
. +1 as this is exactly the solution i was looking for! – Christian Noel May 15 ’13 at 10:58
You are calling multiple batches in an effort to compile a program. I take for granted that if an error occurs:
1) The program within the batch will exit with an errorlevel;
2) You want to know about it.
‘||’ tests for an errorlevel higher than 0. This way all batches are called in order but will stop at any error, leaving the screen as it is for you to see any error message.
If we have two batch scripts, aaa.bat and bbb.bat, and call like below
When executing the script, it will call aaa.bat first, wait for the thread of aaa.bat terminate, and call bbb.bat.
But if you don’t want to wait for aaa.bat to terminate to call bbb.bat, try to use the START command:
To call a .bat file within a .bat file, use
(Yes, this is silly, it would make more sense if you could call it with foo.bat , like you could from the command prompt, but the correct way is to use call .)
If that doesn’t work, replace start with call or try this:
Looking at your filenames, have you considered using a build tool like NAnt or Ant (the Java version). You’ll get a lot more control than with bat files.
If you want to open many batch files at once you can use the call command. However, the call command closes the current bat file and goes to another. If you want to open many at once, you may want to try this:
And so on or repeat start cmd » call . » for however many files. This works for Windows 7, but I am not sure about other systems.
using «&«
As you have noticed executing the bat directly without CALL , START , CMD /C causes to enter and execute the first file and then the process to stop as the first file is finished. Though you still can use & which will be the same as using command1 & command2 directly in the console:
In a term of machine resources this will be the most efficient way though in the last block you won’t be able to use command line GOTO , SHIFT , SETLOCAL .. and its capabilities will almost the same as in executing commands in the command prompt. And you won’t be able to execute other command after the last closing bracket
Using CALL
In most of the cases it will be best approach — it does not create a separate process but has almost identical behaviour as calling a :label as subroutine. In MS terminology it creates a new «batch file context and pass control to the statement after the specified label. The first time the end of the batch file is encountered (that is, after jumping to the label), control returns to the statement after the call statement.»
You can use variables set in the called files (if they are not set in a SETLOCAL block), you can access directly labels in the called file.
CMD /C , Pipes , FOR /F
Other native option is to use CMD /C (the /C switch will force the called console to exit and return the control) Something that cmd.exe is doing in non transparent way with using FOR /F against bat file or when pipes are used. This will spawn a child process that will have all the environment ot the calling bat. Less efficient in terms of resources but as the process is separate ,parsing crashes or calling an EXIT command will not stop the calling .bat
START
Allows you more flexibility as the capability to start the scripts in separate window , to not wait them to finish, setting a title and so on. By default it starts the .bat and .cmd scripts with CMD /K which means that the spawned scripts will not close automatically.Again passes all the environment to the started scripts and consumes more resources than cmd /c :
Unlike the other methods from now on the examples will use external of the CMD.exe utilities (still available on Windows by default). WMIC utility will create completely separate process so you wont be able directly to wait to finish. Though the best feature of WMIC is that it returns the id of the spawned process:
You can also use it to start a process on a remote machine , with different user and so on.
SCHTASKS
Using SCHTASKS provides some features as (obvious) scheduling , running as another user (even the system user) , remote machine start and so on. Again starts it in completely separate environment (i.e. its own variables) and even a hidden process, xml file with command parameters and so on :
Here the PID also can acquired from the event log.