Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a .bat file and set local environment variables in a single line on Windows?

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...

like image 275
Nick Avatar asked Nov 01 '25 03:11

Nick


1 Answers

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
like image 99
GOTO 0 Avatar answered Nov 02 '25 22:11

GOTO 0