• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 887
  • Last Modified:

Batch Job - Call one after another

Hello experts,

I have multiple batch jobs that I need to run in a sequence.  I don't know how long it will take each job to run, so I don't want to set a schedule.

I want to create 1 batch file that is schedule to run at 1am.  Is it possible to set this batch file to execute one batch job and then once complete, to start the other one?

Example:

Call Batch1.bat......
Complete....
Call Batch2.bat........
Complete
Call Batch3.bat
0
holemania
Asked:
holemania
2 Solutions
 
Bill PrewCommented:
Yes, and you actually are very close to the mechanics.  If you want to wait for each BAT to complete before the next starts then use CALL, as in this bat file example:

@echo off
call batch1.bat
call batch2.bat
call batch3.bat

~bp
0
 
Steve KnightIT ConsultancyCommented:
Just to add... be careful you don't use the command "EXIT" to finish any of the batches otherwise it will stop the main one too.  Use EXIT /B to finish them instead or just let it work its way to the end of the batch file.  If one relies on the other at all you might want to set an error level and check for it, i.e.

@echo off
set error=NO
(call batch1.cmd || set error=Batch1 %errorlevel%
call batch2.cmd || set error=%error%Batch2 %errorlevel%
call batch3.cmd || set error=%error%Batch3 %errorlevel%
) > logfile.txt

if "%error%"=="NO" (
  echo Completed OK
  exit /b 0
) else (
  echo There was at least one error -- %error%
  exit /b 1
)

That would give you a logfile.txt of the output of the other batches and return errorlevel "1" to your scheduler if any of the other three had an error.

Lots of things you can do like emailing etc. but depends what you are scheduling and how important it is!

Steve
0
 
Lionel MMSmall Business IT ConsultantCommented:
Steve is the master at batch files so if what you suggest works for you great. I deal with this by adding a line at the end of each batch file to start the next. That is to say at the end of batch1.bat and have a line to start batch2.bat, namely
Start batch2.bat; and then in batch2.bat I have the line Start batch3.bat; and then have my exit after the start batchx.bat to close the current one

@echo off
commands
commands
start batch2.bat
exit
0
 
holemaniaAuthor Commented:
Thank you.  That worked wonderfully.
0
 
Steve KnightIT ConsultancyCommented:
No problem... a little over complicating a three line batch but with the benefit of some error checking.

thanks

Steve
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now