Solved

Batch file to execute two .exe's

Posted on 2006-06-15
27
47,928 Views
Last Modified: 2011-08-18
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
0
Comment
Question by:Pro4ia
27 Comments
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 16913904
Just list them consecutively.  Batch files do not proceed until the each line is done executing.

file1.exe
file2.exe

0
 
LVL 7

Expert Comment

by:Okigire
ID: 16913975
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
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 16913989
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.
0
 
LVL 20

Expert Comment

by:Dufo G. Belski
ID: 16914004
Doesn't CALL only work with batch files?  Wouldn't

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

be better?
0
 
LVL 20

Expert Comment

by:Dufo G. Belski
ID: 16914020
Missed the last comment.  Start /wait would be sequential.  I was never aware that batch files would run .exes to completion before proceeding.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 16914023
but start /wait is not necessary as batch files execute sequentially.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 16914038
Test it - make a simple batch file -

notepad
calc


then run it - calc will not run until you close notepad.
0
 
LVL 4

Author Comment

by:Pro4ia
ID: 16914040
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.
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 16914081
Do you know the last file from file1 which will be extracted?

then you can use just wait until that file exists.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 16914097
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
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 150 total points
ID: 16915010
If you knew what the program that was running is you could do the following:

@echo off

start notepad.exe

call :WAITFORAPP notepad.exe

REM notepad.exe

goto :EOF

:WAITFORAPP

tasklist /FI "IMAGENAME eq %~1" 2>&1 | findstr "No tasks running" >NUL
if ERRORLEVEL 1 goto WAITFORAPP

Good Luck,
Steve
0
 
LVL 47

Expert Comment

by:dbrunton
ID: 16917483
Old dos batch files ran stuff sequentially until Win 95.  Not sure about NT batch files though.

Nowadays the commands run in parallel.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 16917514
Not true - like I said - test things - Simple batch file - notepad and calc.
0
 
LVL 4

Expert Comment

by:mctigue
ID: 16989088
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





0
 
LVL 9

Expert Comment

by:paraghs
ID: 17022894
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.
0
 
LVL 24

Expert Comment

by:SunBow
ID: 17030977
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.
0
 
LVL 24

Expert Comment

by:SunBow
ID: 17031035
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,  

:-))
0
 
LVL 4

Expert Comment

by:mctigue
ID: 17074631
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.
0
 
LVL 3

Expert Comment

by:xy8088
ID: 17141953
How about something like:

file1.exe
ping 127.0.0.1 -n 12
file2.exe
0
 
LVL 38

Assisted Solution

by:BillDL
BillDL earned 150 total points
ID: 17165368
John.
What version of DOS are you using (eg. Win98's version 7.10 command.com), or are you using Windows 2000/XP's "DOS" (cmd.exe)?
If using a Win9x DOS version, are you running the batch file from FULL DOS or from a DOS Window from within Windows?

I know that sounds a bit daft, but you haven't stated your OS nor whether your self-extracting *.exe files are 16-bit (eg. created with PKZIP or similar) or 32-bit GUI "dialog" types (eg. created with WinZip or WinRAR).

In addition to this, 32-bit "dialog" type self-extracting archives normally ask for user input unless executed with, or originally created to automatically use, the appropriate command line parameters to suppress the dialog and unpack silently.  

From my previous tests, batch files that are run from within Win9x's FULL DOS execute commands sequentially, whereas the same batch file run from a DOS Window WITHIN Windows 9x usually execute commands in parallel.  Windows XP executes them sequentially when run from a "command script" (*.cmd or *.bat) and the "Full DOS" scenario doesn't apply to XP anyway.
I don't know about Windows 2000 or 2003 in this respect.

Take a look at Windows XP   START /?

Quote:
"When executing an application that is a 32-bit GUI application, CMD.EXE DOES NOT wait for the application to terminate before returning to the command prompt.  This new behavior DOES NOT occur IF executing within a command script".

How about testing the errorlevel return code to ensure that the first job completed successfully and without errors before then proceeding to the next command?  I tend to regard that as a pretty important aspect to performing jobs like this.  Surely if something goes wrong while extracting the first .exe file, you DON'T want it to proceed to the next one?

Personally, I would tend to stick with the start /wait command and errorlevel tests just to be absolutely sure.  Something like this (assuming here that anything other than a Zero return code spells an error situation):

@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 :ALLOK
: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

I know it's a bit "blocky" and long-winded, but demonstrates my suggested approach.
0
 
LVL 38

Expert Comment

by:BillDL
ID: 17165403
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
0
 
LVL 38

Expert Comment

by:BillDL
ID: 17330488
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.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 17330629
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...  
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 17331248
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...
0
 
LVL 38

Expert Comment

by:BillDL
ID: 17351319
Thank you, sirbounty, SteveGTR, and CetusMOD
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

19 Experts available now in Live!

Get 1:1 Help Now