Enterprise Software

From the forums: Branching from within a batch file

This thread from the Support forums provides a valuable lesson in programming batch files.

Recently, TechRepublic member Nightwing posted this question with a value of 200 points in the Windows section of the Support forums: “I need Windows 98 to start a specific program and when the user exits that program, I want the computer to perform a shutdown. The problem is: when the batch files activates the program, it continues to the shutdown line. I need the batch file to wait to exit the program it activates before it goes to the shutdown line.”

And here’s the final answer
TechRepublic member AsyscoKid earned those TechPoints by posting this answer: “Create a batch file to run the program. We'll call it MyBatch.bat. Then create another batch file. We'll call it RunProg.bat, which would look like this:
rem This is RunProg.bat
@echo off
call Mybatch.bat
shutdown

“Calling one batch file from another causes the calling batch file to hand over execution (control) to the called file. The original (calling) file resumes only when the called bat is finished running.”

Everyone who has learned a programming language has mastered this concept. Your main program starts running, branches out to smaller programs (subroutines), and those smaller programs transfer control back to the main program.
For more help using DOS, check out “DOS is *not* dead” and “DOS is *still* not dead (part 2).”
Check out TechRepublic’s forums
You can earn TechPoints by answering questions in our forums. If you don’t know how many TechPoints you have, click the My TechPoints link in our left nav bar. To comment on this tip, please comment below or drop us a note.

Recently, TechRepublic member Nightwing posted this question with a value of 200 points in the Windows section of the Support forums: “I need Windows 98 to start a specific program and when the user exits that program, I want the computer to perform a shutdown. The problem is: when the batch files activates the program, it continues to the shutdown line. I need the batch file to wait to exit the program it activates before it goes to the shutdown line.”

And here’s the final answer
TechRepublic member AsyscoKid earned those TechPoints by posting this answer: “Create a batch file to run the program. We'll call it MyBatch.bat. Then create another batch file. We'll call it RunProg.bat, which would look like this:
rem This is RunProg.bat
@echo off
call Mybatch.bat
shutdown

“Calling one batch file from another causes the calling batch file to hand over execution (control) to the called file. The original (calling) file resumes only when the called bat is finished running.”

Everyone who has learned a programming language has mastered this concept. Your main program starts running, branches out to smaller programs (subroutines), and those smaller programs transfer control back to the main program.
For more help using DOS, check out “DOS is *not* dead” and “DOS is *still* not dead (part 2).”
Check out TechRepublic’s forums
You can earn TechPoints by answering questions in our forums. If you don’t know how many TechPoints you have, click the My TechPoints link in our left nav bar. To comment on this tip, please comment below or drop us a note.
0 comments

Editor's Picks