Solved

Batch Job - Call one after another

Posted on 2013-01-01
5
875 Views
Last Modified: 2013-01-04
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
Comment
Question by:holemania
5 Comments
 
LVL 52

Assisted Solution

by:Bill Prew
Bill Prew earned 100 total points
ID: 38734845
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
 
LVL 43

Accepted Solution

by:
Steve Knight earned 400 total points
ID: 38735131
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
 
LVL 24

Expert Comment

by:lionelmm
ID: 38736338
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
 

Author Closing Comment

by:holemania
ID: 38744973
Thank you.  That worked wonderfully.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38745120
No problem... a little over complicating a three line batch but with the benefit of some error checking.

thanks

Steve
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now