I am calling a .bat file on windows. The .bat file relies on the presence of some environment variables.
How can I set these environment variables at the same time as calling the .bat file such that the environment variables are local to the .bat file and do not propagate as global variables.
I would ideally like to do this in one line...
You're looking for SETLOCAL/ENDLOCAL. Variables assigned between those statements do not affect the rest of the script.
setlocal
set LOCALVAR=XYZ
call MyScript.bat
endlocal
You can write the four statements in a line if you prefer using & as a separator:
setlocal & set LOCALVAR=XYZ & call MyScript.bat & endlocal
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With