- 1 year ago
What is the difference between !call and call in windows batch
In Windows batch scripting, the call
command is used to call one batch script from another, allowing you to execute the called script and then return to the original script. The call
command is commonly used when you want to invoke another script or subroutine and then continue execution of the original script after the called script completes.
On the other hand, the !call
syntax is not a built-in feature of the Windows batch language. The !
character is a special character used for delayed expansion in batch scripts when you enable it with the setlocal EnableDelayedExpansion
command. Delayed expansion allows you to evaluate variables inside loops or blocks without the need for the %var%
syntax.
Here's a brief explanation of each:
call
command:
Thecall
command is used to invoke another batch script or subroutine within the same script. It is typically used to call a separate batch script or to execute a labeled subroutine defined within the same script. After the called script or subroutine is executed, control returns to the original script.Example:
batch@echo off
echo This is the main script
call sub_script.bat
echo Back in the main script
!call
with delayed expansion:
The!
character is used for delayed expansion in batch scripts. It is typically enabled using thesetlocal EnableDelayedExpansion
command at the beginning of the script. When delayed expansion is enabled, you can use the!var!
syntax to expand the value of a variable inside loops or blocks.Example:
batch@echo off
setlocal EnableDelayedExpansion
set var=Hello
echo Normal expansion: %var%
echo Delayed expansion: !var!
for %%i in (1 2 3) do (
set var=Loop %%i
echo Inside loop: %var% (normal expansion)
echo Inside loop: !var! (delayed expansion)
)
endlocal
In summary, the call
command is used to invoke other scripts or subroutines, while the !call
syntax is not valid in standard batch scripting and is not used for calling scripts. Instead, the !
character is used for delayed expansion of variables when enabled with setlocal EnableDelayedExpansion
.