Link to home
Start Free TrialLog in
Avatar of Pro4ia
Pro4ia

asked on

Batch file to execute two .exe's

I need to create a batch file to execute two exe's (file1.exe & file2.exe).  These files are zip archives that self-extract.  The important thing here is that file1.exe needs to be unzipped first before file2.exe starts unzipping.  (It will show an error if file2.exe runs first or simultaneously with file1.exe)  What is the best way to create the batch file to execute these two files in order?

T.I.A.
John
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Just list them consecutively.  Batch files do not proceed until the each line is done executing.

file1.exe
file2.exe

Avatar of Okigire
Okigire

For future reference, sometimes an EXE file will halt the execution of the rest of the BATch file.  In otherwords, file1.exe will run and when it finishes, so does the batch file (and you never see file2 run).  In such a case, you would add CALL before it:

call file1.exe
call file2.exe
Actually, call is used to call other batch files - you want to use start - but he doesn't want the execution to be simultaneous - they need to be sequential, so start will NOT be used in this case.
Doesn't CALL only work with batch files?  Wouldn't

start /Wait file1.exe
start /wait file2.exe

be better?
Missed the last comment.  Start /wait would be sequential.  I was never aware that batch files would run .exes to completion before proceeding.
but start /wait is not necessary as batch files execute sequentially.
Test it - make a simple batch file -

notepad
calc


then run it - calc will not run until you close notepad.
Avatar of Pro4ia

ASKER

I tried just using

file1.exe
file2.exe

and 2nd exe ran before first one finished and prompted me to run the first completely before running the second.  I guess it's just the nature of these self-archived zip exes.

Maybe I should use a timer instead or wait for a key to be entered after totally installing the first.

What do you guys think.
Do you know the last file from file1 which will be extracted?

then you can use just wait until that file exists.
If that happened, then it's probably got one process spawning another then exiting, which would cause that behaviour.  If that's the case, you can try the start /wait but I suspect that won't work.  If it doesn't, you can get a sleep utility and do this:

file1
sleep 5
file2

The sleep command pauses the script for x seconds (in this example, 5).  
Sleep is part of the 2003 Resource Kit:
http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&DisplayLang=en
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Old dos batch files ran stuff sequentially until Win 95.  Not sure about NT batch files though.

Nowadays the commands run in parallel.
Not true - like I said - test things - Simple batch file - notepad and calc.
You could make two batch files.

The first one might be called batch1.bat, and the second one might be batch2.bat

The first one should look something like this:

@echo off
file1.exe
call batch2.bat
:end

The second one should look something like this:

@echo off
file2.exe
:end





Try Delayexec :
http://www.rjlsoftware.com/software/utility/delayexec/

I created this batch file :
delayexec notepad 1
delayexec calc 20

and the batch file executed both the programs.
The reason could lie in this statement on its website :
"DelayExec will close after the file specified on the command line is launched."
So, once notepad is launched, first command of delayexec is completed, and the batch file executes second command.

Give it a try.
asyscokid >  I was never aware that batch files would run .exes to completion before proceeding.

They don't. Or rather, they cannot be counted on to do that.

A running program can say "finished" any time to the calling program, but it is more of a delivery receipt than a read receipt or ompletion acknowledgement.

It depends on the programs used, and you'd best check them well, and their habits, to assess whether other workarounds are desireable.

With inside knowledge, one cheat is to know the habits of Program one. It extracts files. Cheat: what is the last file? Once you know that, (or force it at compilation) then you can just sit back and wait for it to show up. Adding a timer like timewait can be beneficial, holding up the return to batch continuation and counting up how long it takes while you are at it. You can later add an automated quit exit, for places you think it is simply taking too long.
Pro4ia > Maybe I should use a timer instead or wait for a key to be entered after totally installing the first.

Sure, why not? Or, try to get independent output from file1 to the disk, for at least something like completion code or status. Then you can check the disk for it to show up, and continue on with the next script or whatever.

cwwkie > Do you know the last file from file1 which will be extracted? then you can use just wait until that file exists.

:-))

dbrunton >  Not sure about NT batch files though.

Exactly, NT will run in any order, thus the problem with inconsistent startups.

leew > If that happened, then it's probably got one process spawning another then exiting,  

:-))
another thing you could try might be:

@echo off
file1.exe
pause
file2.exe
:end


That would make it so you would have to press a key before it executes the second exe.
How about something like:

file1.exe
ping 127.0.0.1 -n 12
file2.exe
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry SunBow, I see now that you already mentioned "completion code or status".

Whoops, problem with a misplaced "ALLOK" label there after an error.  Revised version:

@echo off
start /wait file1.exe
if not errorlevel 0 goto :ERR1
goto :STEP2
:STEP2
start /wait file2.exe
if not errorlevel 0 goto :ERR2
goto :ALLOK
:ERR1
echo **** Warning: file1 did not unpack properly! ****
echo.
echo Press any key to terminate job ...
pause>nul
goto :END
:ERR2
echo **** Warning: file2 did not unpack properly! ****
echo.
echo Press any key to terminate job ...
pause>nul
goto :END
:ALLOK
echo ****** Job completed successfully *****
echo.
goto :END
:END
EXIT /B
While I have the utmost respect for SteveGTR's exceptional and in-depth knowledge of working from the command line, there are issues here which I raised and asked earlier, and which have not been answered.  I asked what operating system the commands were to be used in and (if applicable) whether they were being run in Full DOS mode.

The answer to this is important, because Steve's TASKLIST command is not applicable to old DOS and Windows 9x.  Unfortunately Pro4ia has not returned to say whether he is running an OS that supports that command.  If not, then Steve's comment was nevertheless an excellent suggestion, and perhaps should then be better included in a points split with other comments that should have worked more universally.  We won't know for sure though until Pro4ia does return.  Hopefully he will come back with good news that SteveGTR's suggestion did work.
I agree BillDL - perhaps both yours (http:#17165368) & Steve's comment would be acceptable in a split, since they'd cover multiple scenarios between them...  
We're working with incomplete information. Nothing regarding operating systems was given in the initial question.

In any case I have no problem with a split...
Thank you, sirbounty, SteveGTR, and CetusMOD