2

see this example please:

C:\
..ex1\
....start.bat
....111\
......1.bat
......222\

start.bat:

@echo off
pushd 111
call 1.bat
popd 

echo CD is wrong:%cd%    
pause

1.bat

@echo off
setlocal
pushd 222
echo 1.bat says we are in %cd%

when i run start.bat i get:

1.bat says we are in C:\ex1\111\222
CD is wrong:C:\ex1\111

as you see CD (current dir) should be C:\ex1 not C:\ex1\111 because i used setlocal, it seems that setlocal have no effect when pushd is nested in that way

if i change

pushd 111
call 1.bat

to

cd 111 
call 1.bat

or to

call 111\1.bat

everything works fine so setlocal works fine in simple pushd cases

is this a bug? if not can you explain why this happens?

1 Answer 1

2

You're encountering this behavior because setlocal does not affect the current working directory or the directory stack managed by pushd and popd.

What's happening in your script

In start.bat:

@echo off
pushd 111

call 1.bat

popd 
echo CD is wrong:%cd%    
pause

In 1.bat:

@echo off
setlocal

pushd 222

echo 1.bat says we are in %cd%

When you run start.bat, it prints:

1.bat says we are in C:\\ex1\\111\\222
CD is wrong:C:\\ex1\\111

This happens because you're doing a pushd inside 1.bat, but you never do a matching popd, so the directory stack isn't fully reverted.

Also, setlocal only controls the environment variables (like %PATH%, %TEMP%, etc.), it has no impact on the current directory or the directory stack.

Sign up to request clarification or add additional context in comments.

6 Comments

@BadrElmers ... & echo %cd% will return the previous value of cd variable. The & operator makes the entire expression run as like in the parentheses (...) operator and all variables in between does evaluate after a most top (...) expression.
@BadrElmers: you are correct, but your reasoning is invalid. To get a correct result, you can use delayed expansion or not use variables: setlocal & pushd 1 & cd & endlocal & cd (in a batch file, because setlocal has no effect at the command line)
@VibesAr Please read this answer for details about the commands SETLOCAL and ENDLOCAL. You wrote: "You're encountering this behavior because setlocal does not affect the current working directory or the directory stack managed by pushd and popd. That is right. But endlocal restores the current directory pushed by setlocal on stack. cmd.exe always implicitly calls endlocal before exiting the processing of a batch file like 1.bat for each setlocal executed during batch file processing without a matching endlocal.
@VibesAr The chapter 3 in my answer on why is my cd %myVar% being ignored? describes the current directory restore behavior by endlocal with a simple demonstration batch file.
This behavior of setlocal/endlocal commands was described in Setlocal/Endlocal commands save/restore current directory! thread...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.