How to call one batch file after another - batch-file

I have a batch file that i am testing, all i want to do is the following
CALL ping.bat
Then after that batch file has run i want to run another file:
CALL ping2.bat
Right now i have these two lines beside each other in a batch file the first batch file will fire successfully but the second one does not . Any suggestions?
CALL ping.bat
CALL ping2.bat
Ping .bat is just:
ping 127.0.0.1

Check that you don't have exit somewhere in the first batch. Some people habitually use that to jump out of a batch file which is not the proper way to exit a batch (exit /b or goto :eof is).
Another option is that you might call another batch in the first one without call.

There's a chance your ping.bat is simply calling itself, if its contents is merely ping 127.0.0.1, as you say.
I would append .exe after ping to make things sure.
As jeb has by all means justly suggested, choosing a different name for your batch file is an even better solution.

Assume you have 3 batch files.
ping1.bat which has the content ping 127.0.0.1
ping2.bat which has the content ping 127.0.0.1
ping3.bat which has the below two lines
call ping1.bat
call ping2.bat
If you have all the three batch files in a single folder,(lets say under C:\NewFolder)
then if you double click ping3.bat, you won't get any error for sure.
Note: If you don't want to wait for the first command to complete, then use start keyword which just initiate the process and proceed with the next line in the batch file, whereas call
will do it sequentially(comes to next line only after the current process completes , start allows parallelism)
To do it parallel use the below two lines of code in the ping3.bat:
start ping1.bat
start ping2.bat

don't call the file you are calling from the batch the same name as the command you are trying to invoke...renamed to gnip.bat and works fine

Not exactly sure what you wanted to do here, but I assume that you wanted to do this:
run FIRST.bat
from FIRST.bat you wish to call SECOND.bat
While SECOND.bat is executing, FIRST.bat should remain paused
After SECOND.bat finishes its execution, FIRST.bat should resume and call THIRD.bat
In that case, from your actual batch file, you should start ping.bat and ping2.bat like this:
::some code here
start /wait ping.bat
start /wait ping2.bat
::some code here
Then in both ping.bat and ping2.bat the last line should be exit. Their code should look like this:
::some code here, might be ping 127.0.0.1
exit
So now your actual batch file will start ping.bat and it will wait for it to finish (exit). Once the ping.bat closes, your actual batch file will go to the next line and it will start ping2.bat etc.

The ping command acts differently on different operating systems. Try forcing the ping command to stop after a few echo requests with a -n switch.
ping -n 4 127.0.0.1

Related

How to don't quit batch file from another script with exit /b 0

I have a program1.cmd file that finishes the commands with exit /b 0. This program cannot be edited.
I've created a test.bat and I must call program1.cmd, however, I need to process in sequence program2.cmd and then program3.exe. How to do that?
When you want to call one batch file from another (whether .BAT or .CMD), you should use the CALL command to return to the calling batch file. See SS64 on CALL for more information. Using the information you gave in your question, TEST.BAT would contain the following commands:
CALL program1.cmd
CALL program2.cmd
program3
REM could also be CALL program3.exe, but doesn't need to be.
(If the two .CMD files and the .EXE are not in the same directory as TEST.BAT, you would need to supply paths to them, not just the file names.)

Executing bat files one by one

What I got is a list of bat files:
file1.bat
file2.bat
…
file29.bat
I need them to run one after each other. Meaning when the file1.bat closes file2.bat starts and so on.
I tried this, but it doesn't work properly:
start /wait call file1.bat
start /wait call file2.bat
You might want to add more to your question to make it easier to understand. My guess is that you want the bat file to open the next one then close itself after that.
If that's what you want to do; add these commands to each of the files:
start file2.bat
exit
Of course, you'll want to change start file2.bat to start file3.bat and so on for each file.
If you want file1.bat to manage all of the files, I don't think that's possible in batch.
You didn't describe how exactly it's not doing what you expected. I'm guessing that what happens is you're having to shut down each script before the next one will continue.
Documentation on start says:
WAIT Start application and wait for it to terminate.
command/program
If it 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 you must use start then you could force it to use the /c switch which will automatically close the window once it's done:
start /wait cmd /c call file1.bat
I'm not really sure you accomplish anything by using call so that ought to be equivalent to just:
start /wait cmd /c file1.bat
Using start creates a new window for each program and you may just want to have it all run in a single command processor window.
As noted by Biffin you can just list them all out in a master script and they will run in order.
call file1.bat
call file2.bat
...
call file29.bat
And a shorthand for that is:
for /l %%f in (1; 1; 29) do call file%%f.bat
Remember to double up those percent characters inside a batch script but not on the command line.
This question might explain some of the unexpected behavior you were seeing.
How to run multiple .BAT files within a .BAT file

redirecting a stream for the entire .bat file inside itself

I've recently started working with .bat files, and I'm trying to redirect the output to a file.
I've found 2 options, so far:
echo aaa > out.txt - which sends the output of the single echo command to the specified file (can also be appended using >>)
calling the entire file from the cmd using somefile.bat > out.txt (which is actually similar to number 1, as it sends the output of the single command somefile.bat to out.txt)
What I'm looking for is something else - I'm trying to have a line in my file that sends all the output from that point forth to the file.
Thanks!
echo this goes to screen
(
echo this line goes to the file
echo also this line and the ping-output
ping www.stackoverflow.com
echo and this
)>file.txt
echo this goes to screen again
Note:
all inside the block (between ( and )) is parsed at once. If you use variables inside the block, you may need delayed expansion.
There is no universal solution. It depends of the batch file requirements.
For a lot of batch files, the answer from Stephan will work without problems, taking in consideration what he pointed: all the code is inside a block and any variable management inside it may require delayed expansion.
Other alternative is to move the code under a subroutine, calling it with the redirection
#echo off
call :mainProcess %* > outputFile
exit /b
:mainProcess
:: here the batch file begins
echo %1 %2 %3

what does start/wait means in batch file

In batch file, what does start/wait means
Example:
ECHO start/wait C:\\Example.exe
I am trying to run an exe, so what does start/wait indicates over here?
/wait forces the batch file to halt processing until the called program has finished executing.
(This can be useful, for example, if you are loading multiple items in your windows Startup folder, and the nature of the programs require that one be finished before the next starts loading. Put them all in a single batch file, using the /wait parameter, and only put a shortcut to the batch file in the Startup folder.) Command line parameters of the START command can be combined in a single line. Example:
START /max /wait NOTEPAD.EXE SOME.TXT
Source: http://aumha.org/a/batches.php

Need a line of code for a batch file to open another program and then close the original batch file

I am writing a series of batch files to demonstrate to my class what a computer virus can do. All I need is a SIMPLE line of code that will open another file. Any help is greatly appreciated.
Is this what you're looking for?
#echo off
echo One
start notepad.exe
exit
echo Two
It will echo "One", start notepad, then exit the Command Prompt in which it's running. It won't echo "Two".
If you want to open a document (or a web page, or whatever) rather than an executable, just specify its pathname in place of "notepad.exe" and start will do the right thing.
If you don't want to close the Command Prompt, but just want to end the execution of the batch script, do this:
#echo off
echo One
start notepad.exe
goto :eof
echo Two
That will return you to the prompt without echoing "Two".
Well here is how to open another executable but what do you mean close the original batch file?
start /d C:\Windows\System32\calc.exe
and then to exit early you could just use exit. but if you reach the end of the batch it may just close anyway.

Resources