Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Let's say I have two batch files, foo.bat and bar.bat, that are located like so:

root\foo.bat
root\grub\bar.bat

Let's also say that both files contain a line that sets a variable, like so:

set PWD=%cd%

Now, if foo.bat calls .\grub\bar.bat, will bar.bat override the PWD variable set in foo.bat?

share|improve this question

2 Answers 2

scope is global/ variable is shared as you say..so to echo you, yes.

also, if you use setx it will work for the next cmd session and future ones because it's in the registry., but set and setx aren't to be compared in discussion of scope. environment variables are generally global. But you can use setlocal in a batch file at the start and the variables set there remain local

C:\DOCUME~1\bob>type n.bat <ENTER>
setlocal
set a=99

C:\DOCUME~1\bob>echo %a% <ENTER>
%a%

C:\DOCUME~1\bob>n <ENTER>

C:\DOCUME~1\bob>setlocal

C:\DOCUME~1\bob>set a=99

C:\DOCUME~1\bob>echo %a% <ENTER>
%a%

C:\DOCUME~1\bob>
share|improve this answer
up vote 1 down vote accepted

I ran an experiment. Changing the environment variable in bar.bat causes it to change in foo.bat. I suppose this was to be expected because environment variables set using the set command (not setx) are scoped to the command session.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.