Question Details

No question body available.

Tags

windows batch-file cmd

Answers (1)

May 29, 2025 Score: 2 Rep: 108 Quality: Low Completeness: 60%

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.