How do I declare a global variable in a batch script?
Example:
Test1.bat:
set testvar=C:\Windows
echo %testvar%
Now I should be able to use this testvar in other batch script (test2.bat)
Test2.bat:
echo %testvar%
Thanks and Regards!
How do I declare a global variable in a batch script?
Example:
Test1.bat:
set testvar=C:\Windows
echo %testvar%
Now I should be able to use this testvar in other batch script (test2.bat)
Test2.bat:
echo %testvar%
Thanks and Regards!
By setting a system environment variable permanently. See here.
Applies To: Windows Server 2008, Windows Vista
For Windows XP download and install Windows XP Service Pack 2 Support Tools
As far as I know the only way to use one variable in multiple batch scripts is to create custom system environment variables. You can find detailed walkthrough on how to do it here: global environment variables
See
SETX /?
from the prompt - and observe the critical point that the change aplies to FUTURE instances of cmd.exe only - not existing
If the 2 batch files are run in the same command process it will work just fine.
This is test1.bat
set testvar="test123"
echo In test1.bat
echo %testvar%
This is test2.bat
echo In test2.bat
echo %testvar%
This is output from running test1.bat and then test2.bat
C:\temp>test1.bat
C:\temp>set testvar="test123"
C:\temp>echo In test1.bat
In test1.bat
C:\temp>echo "test123"
"test123"
C:\temp>test2.bat
C:\temp>echo In test2.bat
In test2.bat
C:\temp>echo "test123"
"test123"
This also works if you call one batch from another batch (because it is the same process)
Change test1.bat to be
set testvar="test123"
echo In test1.bat
echo %testvar%
call test2.bat
Now output looks like this
C:\temp>test1.bat
C:\temp>set testvar="test123"
C:\temp>echo In test1.bat
In test1.bat
C:\temp>echo "test123"
"test123"
C:\temp>call test2.bat
C:\temp>echo In test2.bat
In test2.bat
C:\temp>echo "test123"
"test123"
If you want it to be available to different command processes you'll need to look at setx or setting a system variable.