- 1 year ago
CMD .bat file include by "sub.bat" vs "CALL sub.bat"
Comment
1 / 1
In Windows batch files (.bat), there is a difference between using sub.bat
and CALL sub.bat
. Let's explore each of them:
- Using
sub.bat
:
When you usesub.bat
without theCALL
command, the control will be transferred to thesub.bat
script, but it will not return to the calling batch file aftersub.bat
has finished executing. This means any code written after the call tosub.bat
in the original batch file will not be executed untilsub.bat
is completed.
Example:
batchfile.bat:
batch@echo off
echo This is the main batch file
sub.bat
echo This line will not be executed until sub.bat is completed.
sub.bat:
batch@echo off
echo This is the sub batch file
In this case, the output will be:
vbnetThis is the main batch file
This is the sub batch file
The line "This line will not be executed until sub.bat is completed." will not be printed.
- Using
CALL sub.bat
:
When you useCALL sub.bat
, the control will be transferred tosub.bat
, and aftersub.bat
has finished executing, the control will return to the original batch file at the line where theCALL
command was used. This allows you to execute code after theCALL
statement in the original batch file.
Example:
batchfile.bat:
batch@echo off
echo This is the main batch file
CALL sub.bat
echo This line will be executed after sub.bat is completed.
sub.bat:
batch@echo off
echo This is the sub batch file
In this case, the output will be:
vbnetThis is the main batch file
This is the sub batch file
This line will be executed after sub.bat is completed.
Using CALL
is typically preferred when calling other batch files from a main batch file, as it allows you to continue with the execution of the main batch file after the called batch file has finished its task. However, in some cases, using sub.bat
without CALL
may be desired when you want to jump to another batch file and not return to the original batch file afterward.