- 1 year ago
Receive variable from a called batch file
To receive a variable from a called batch file, you can use the call
command in combination with the set
command to pass the variable value from the called batch file back to the caller. Here's an example:
Caller Batch File (caller.bat):
batch@echo off
REM Call the called.bat file and pass the variable
call called.bat
REM Access the variable received from called.bat
echo The value received is: %received_variable%
Called Batch File (called.bat):
batch@echo off
REM Set the variable
set received_variable=Value from called.bat
In this example, the call
command is used to execute the called.bat
batch file from within the caller.bat
batch file. When the called.bat
file is executed, it sets the value of the received_variable
variable using the set
command.
After the called.bat
file execution is complete, the control returns to the caller.bat
file. The caller.bat
file can then access the value of the received_variable
variable by using %received_variable%
.
When you run the caller.bat
file, it will display the following output:
csharpThe value received is: Value from called.bat
Note that the call
command allows the called batch file to modify the environment variables of the caller batch file. This allows you to pass values back to the caller batch file.