Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 48697
  • Last Modified:

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
0
Pro4ia
Asked:
Pro4ia
2 Solutions
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Just list them consecutively.  Batch files do not proceed until the each line is done executing.

file1.exe
file2.exe

0
 
OkigireCommented:
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
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Dufo G. BelskiRetired bureaucrat/desktop supportCommented:
Doesn't CALL only work with batch files?  Wouldn't

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

be better?
0
 
Dufo G. BelskiRetired bureaucrat/desktop supportCommented:
Missed the last comment.  Start /wait would be sequential.  I was never aware that batch files would run .exes to completion before proceeding.
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
but start /wait is not necessary as batch files execute sequentially.
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Test it - make a simple batch file -

notepad
calc


then run it - calc will not run until you close notepad.
0
 
Pro4iaAuthor Commented:
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
 
cwwkieCommented:
Do you know the last file from file1 which will be extracted?

then you can use just wait until that file exists.
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
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
 
SteveGTRCommented:
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
 
dbruntonCommented:
Old dos batch files ran stuff sequentially until Win 95.  Not sure about NT batch files though.

Nowadays the commands run in parallel.
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Not true - like I said - test things - Simple batch file - notepad and calc.
0
 
Stephen McTigueAssistant Director of Information TechnologyCommented:
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
 
paraghsCommented:
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
 
SunBowCommented:
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
 
SunBowCommented:
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
 
Stephen McTigueAssistant Director of Information TechnologyCommented:
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
 
xy8088Commented:
How about something like:

file1.exe
ping 127.0.0.1 -n 12
file2.exe
0
 
BillDLCommented:
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
 
BillDLCommented:
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
 
BillDLCommented:
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
 
sirbountyCommented:
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
 
SteveGTRCommented:
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
 
BillDLCommented:
Thank you, sirbounty, SteveGTR, and CetusMOD
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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