9

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!

Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
user2150777
  • 101
  • 1
  • 2
  • 6
  • Possible duplicate of [Setting a system environment variable from a Windows batch file?](http://stackoverflow.com/q/3803581/1380680) – Reinier Torenbeek Mar 13 '13 at 02:30
  • What you've written looks correct, assuming both batch files are run from within the same command session. Is it not working for you? – Simon MᶜKenzie Mar 13 '13 at 02:31
  • It doesnt work on new command window. For First it works C:\Program Files>echo %testvar% c:\Windows New cmd windows , it doesn't . It just shows C:\Documents and Settings\Z2021679>echo %testvar% %testvar% – user2150777 Mar 13 '13 at 02:41
  • 1
    I do not agree that this is a duplicate question. Setting a system var is different that using a global script variable , which is the opposite of SETLOCAL . – djangofan Jun 24 '13 at 18:57
  • You probably have SETLOCAL turned on. That would cause this to not work -- It's a valid question though -- with SETLOCAL turned on, how do you set a global variable (without using SETX, because we don't want the changes to persist). – BrainSlugs83 Apr 03 '14 at 18:32
  • For complex scripts with for loops, setlocal is critical. My advice, if you don't want to work with temp data files and if you're on a NTFS drive, is to use alternate data streams: echo data >:streamname and: more <:streamname. Other options would be to use reg query and reg add, works the same as setx but with fresh reads, whereas setx has only data from session init. The ads are more practical and are stored in the cwd, the reg and setx variants need more text processing for getting the data. – JasonXA May 26 '17 at 19:00

4 Answers4

2

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

Jacob Seleznev
  • 8,013
  • 3
  • 24
  • 34
  • I dont have SETX on my Office PC, we cant install any.. I found WINSET which i cannot use as well..http://www.dankalia.com/tutor/01002/0100201008.htm – user2150777 Mar 13 '13 at 02:35
  • @user2150777 SETX comes with the XP Service Pack 2 Support Tools. – Jacob Seleznev Mar 13 '13 at 03:24
  • When i use SETX on First Cmd Prompt C:\Program Files\Resource Kit>SETX TESTVAR=C:\Windows C:\Program Files\Resource Kit>echo %TESTVAR% C:\Windows On New Command Prompt C:\Documents and Settings\Z2021679>echo %TESTVAR% %TESTVAR% It doesnt work still. Thanks and regards – user2150777 Mar 13 '13 at 03:45
  • Try `Setx TESTVAR C:\Windows` – Jacob Seleznev Mar 13 '13 at 04:41
  • Yes, I installed "Windows XP Service Pack 2 Support Tools" and used SETX and it works ! Thanks a Lot ! – user2150777 Mar 13 '13 at 07:54
  • That will change the users' or machines' configurations... -- isn't there a way to do it in the wider scope (i.e. persist beyond `SETLOCAL` for the same session), but not modify their config? – BrainSlugs83 Apr 03 '14 at 18:35
1

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

Arman P.
  • 4,314
  • 2
  • 29
  • 47
1

See

SETX /?

from the prompt - and observe the critical point that the change aplies to FUTURE instances of cmd.exe only - not existing

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

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.

Tad
  • 934
  • 6
  • 10
  • This you are just echoing and not calling from test1.bat – user2150777 Mar 13 '13 at 02:44
  • Correct - I was showing that even calling them separately, but in the same process it does work. – Tad Mar 13 '13 at 03:06
  • Yes, It works when we call the test2.bat! Thanks a Ton ! – user2150777 Mar 13 '13 at 07:53
  • Not really a global var, more like a remnant of past declaration, a passthrough var if you will. If lets say you would run a child script from a parent, only the values set before the child was run will be passed through, if the parent alters after the var, that data never gets "globalized" and the child will still have the old value. – JasonXA May 26 '17 at 17:44