difference between running another batch script by its name and by using call

You can invoke another batch script from a batch script like this:

rem a.bat
echo helloa1
b.bat
echo helloa2

 

rem b.bat
echo hellob1
exit /b
echo hellob2

Here, the batch script a.bat invokes another batch script b.bat. You may expect it displays “helloa1″,”hellob1″, and “hello a2″. But it only shows the first two. This is because when you run another script by directly using its name, the current script stops, and the execution flow never goes back to the current script again. This is probably not what you want. You may want the first script continues after the second script terminates. To do that, you need to run the second script from the first script using “call” as follows:

rem a.bat
echo helloa1
call b.bat
echo helloa2

Now, after b.bat exits(note that exit /b will not terminate the cmd.exe that executes the scripts), a.bat continues. See this post for a discussion.

 

If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!
Posted in

Leave a Reply