- How do I run a bat file in the background from another bat file?
- 7 Answers 7
- How to call a .bat file from any location in CMD on Windows
- 6 Answers 6
- How to call .bat file from another .bat file when run as administrator in Windows 7
- 2 Answers 2
- Calling a .BAT file from another .bat file
- 4 Answers 4
- How to run multiple .BAT files within a .BAT file
- 18 Answers 18
- using «&«
- Using CALL
- CMD /C , Pipes , FOR /F
- START
- SCHTASKS
How do I run a bat file in the background from another bat file?
I have a «setup» script which I run in the morning which starts all the programs that I need. Now some of those need additional setup of the environment, so I need to wrap them in small BAT scripts.
How do I run such a script on Windows XP in the background?
CALL env-script.bat runs it synchronously, i.e. the setup script can continue only after the command in the env-script has terminated.
START/B env-script.bat runs another instance of CMD.exe in the same command prompt, leaving it in a really messy state (I see the output of the nested CMD.exe, keyboard is dead for a while, script is not executed).
START/B CMD env-script.bat yields the same result. None of the flags in CMD seem to match my bill.
7 Answers 7
Actually, the following works fine for me and creates new windows:
Combine that with parameters to start , such as /min , as Moshe pointed out if you don’t want the new windows to spawn in front of you.
Two years old, but for completeness.
Standard, inline approach: (i.e. behaviour you’d get when using & in Linux)
Notes: 1. CALL is paired with the .bat file because that where it usually goes.. (i.e. This is just an extension to the CMD /C CALL «foo.bat» form to make it asynchronous. Usually, it’s required to correctly get exit codes, but that’s a non-issue here.); 2. Double quotes around the .bat file is only needed if the name contains spaces. (The name could be a path in which case there’s more likelihood of that.).
If you don’t want the output:
If you want the bat to be run on an independent console: (i.e. another window)
If you want the other window to hang around afterwards:
Note: This is actually poor form unless you have users that specifically want to use the opened window as a normal console. If you just want the window to stick around in order to see the output, it’s better off putting a PAUSE at the end of the bat file. Or even yet, add ^& PAUSE after the command line:
How to call a .bat file from any location in CMD on Windows
I have a Batch file which I want to execute in CMD from any directory. Something like this:
File name: MyBatch
Open CMD: c:\Program Files> MyBatch
How can I accomplish this?
6 Answers 6
Set that location in your PATH environmental variable.
I wouldn’t put it the root or the system directory.
I keep a directory with all my scripts in C:\DRR\CMD
and either set it in the MyComputer GUI or run at the command script:
How about. «%MyBatch%» ? (the double qoutes are intended)
That should work!
to change your Variable, use set MyBatch=»Path\Whatever.bat»
and to ask the user for a String, use set /p MyBatch=»Question? »
— or, you can use a BAT-to-EXE converter to run the batch in a Executable.
You could just put it in your c:\windows\system32 directory, as its always in the system path.
You would need to set the PATH environment variable to include the path to your batch file
If you are talking Windows, then the PATH Environment variable is what you need to set.
The path where your bat file is placed should be appended to the PATH variable. In your example append «C:\;» in the value for Path environment variable.
Then you can execute MyBatch.bat from anywhere on the command line.
Create a folder called Batches (lets say in your C drive).
Append C:\Batches in your path environment variable and you then can run batch files in that directory from anywhere.
How to call .bat file from another .bat file when run as administrator in Windows 7
Here’s a simple example with two bat files, caller.bat and callee.bat in the same directory.
When I run caller.bat by double clicking it in Explorer it works as expected but if use right-click «Run as administrator» I get
‘callee.bat’ is not recognized as an internal or external command.
2 Answers 2
The problem is that when run as administrator the current working directory is changed to C:\Windows\System32. My solution was to explicitly change the current working directory in caller.bat to be the same as the directory from where the file is run. This is done by extracting the drive and path from the %0 parameter as shown below:
The /D argument to cd causes the directory as well as the path to change and is needed to handle the case where the caller .bat file is not on the C: drive.
dp0callee.bat directly to avoid leave administrative working directory. (I mean directory, not privileges). – gmo Jun 24 ’13 at 13:40
Another solution is to add the directory where your scripts are stored to your path system environment variable. You could do this through the GUI that windows provides in the advanced system settings (type environment variable into the start menu and you should see the option), or you can run a cmd session as admin and enter: setx path %path%;»your script dir» /M
The /M makes it system wide, not just your user (this is what requires the admin). The double quotes are only required if your path has any spaces in it. The path variable contains a list of paths delimited by semi-colons. The above just appends a new entry to the existing list. Adding an entry to the list allows you to execute programs from this directory without specifying the path.
Finally, if this does not work on it’s own you may need to also add .cmd and/or .bat to your pathext variable: setx pathext %pathext%;.cmd;.bat /M
Calling a .BAT file from another .bat file
I have a .bat file to which I can pass parameters.
Briefly, it runs an SQL to load Table1 on the Dev environment. Now, I want it to load multiple tables overnight. So, I set up a master .BAT which goes something like
If I submit MASTER_LOAD.BAT from cmd, it executes the load for Table1 but does not proceed to Table2 load. These are the last two lines of the WRAPPER.BAT
4 Answers 4
Your exit %error_status% command in LOAD_TABLE_WRAPPER.BAT is terminating your batch session, so your MASTER_LOAD.BAT never gets a chance to resume with the next call.
You can fix the problem simply by adding the /B option to your EXIT command
I almost never use EXIT without /B in a batch file (though there are times when /B is not wanted).
But another alternative is to run the called scripts via CMD instead of CALL.
There are a number of differences between the methods
CALL with EXIT /B
- Comparatively fast
- Can preserve environment variable values upon return (SETLOCAL is available if you don’t want to preserve values)
- Called script inherits delayed expansion and extension states (enabled or disabled)
CMD /C
- Comparatively slow
- Cannot preserve environment variable values upon return. (You could write out the definitions to a file and load them back in upon returning to the master, but that is not convenient or efficient)
- Called script always starts with default delayed expansion and extension states (Normally delayed expansion is disabled and extensions are enabled)
I would never recommend using CMD /C over CALL unless the called batch files have EXIT without the /B option and you cannot modify the batch file to add the /B option.
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.