why is my cd %myVar% being ignored?

I save my original path in originalPath and then move to another folder. At the end when I do cd %originalPath%, it doesn't work. It stays in the new path.

I have tried using pushd %myPath% and popd, but it doesn't work either.

C:\BatchTests\ has my script and the folder Subfolder1.

My script:

@echo off
set myPath=Subfolder1
set folderList=input.txt
REM set originalPath=%CD%
set originalPath=%~dp0  

REM pushd %myPath%
cd %myPath%
setlocal EnableDelayedExpansion

:process

REM The rest of my script

echo %originalPath%
REM echo %originalPath% prints: C:\BatchTests\
cd %originalPath%
REM popd
pause

Why is this happening?


1. Quoting unknown paths

Paths not known on running a batch file and not being fixed could contain 1 or more spaces. This means path strings should be quoted like file names.

2. Change drive and directory

Command CD changes by default the current directory on current drive only.

Option /D to change current directory to a directory on any drive with a letter should be always used when current directory on starting a batch file is not fixed on same drive as temporarily used current directory.

3. setlocal and endlocal save and restore also current directory

The command setlocal creates always a new copy of the environment table which is destroyed when using endlocal or exiting the batch file restoring previous table. Also the states of command extensions and delayed expansion is saved and restored. See this answer with an example demonstrating what happens on using setlocal and endlocal.

But additionally setlocal saves also current directory and endlocal restores it as the following code demonstrates.

@echo off
set "InitialPath=%CD%"
echo 1. Current directory is: %CD%
cd /D "%windir%\Temp"
echo 2. Current directory is: %CD%
setlocal
rem Change current directory to parent directory.
cd ..\
echo 3. Current directory is: %CD%
setlocal
rem Change current directory to root of current drive.
cd \
echo 4. Current directory is: %CD%
endlocal
echo 5. Current directory is: %CD%
endlocal
echo 6. Current directory is: %CD%
cd /D "%InitialPath%"
echo 7. Current directory is: %CD%
set "InitialPath="
pause

4. Trailing spaces and tabs assigned to environment variable

With not using double quotes on assigning a value to an environment variable, command set appends everything after first equal sign up to end of line to the environment variable including not visible trailing spaces and tabs. There are trailing spaces on line set originalPath=%~dp0  , see answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? for more details.


Your code improved to avoid all the possible issues listed above:

@echo off
set "myPath=Subfolder1"
set "folderList=input.txt"
REM set "originalPath=%CD%"
set "originalPath=%~dp0"

REM pushd "%myPath%"
cd /D "%myPath%"
setlocal EnableDelayedExpansion

:process
REM The rest of my script

endlocal
echo %originalPath%
REM echo %originalPath% prints: C:\BatchTests\
cd /D "%originalPath%"
REM popd
pause

And taking point 3 into account and as aschipfl also explained well, following would work, too.

@echo off
set "myPath=Subfolder1"
set "folderList=input.txt"

setlocal EnableDelayedExpansion
cd /D "%myPath%"

:process
REM The rest of my script

endlocal
pause

Actually it does not really stay in the changed directory. The real problem is that setlocal, besides environment variable changes, caches the current directory, which you already changed by cd %myPath%.

Then you switch back to the former directory by cd %originalPath%. But as soon as the batch script finishes execution, an implicit endlocal occurs which resets the current directory to the one previously cached by setlocal.

The issue is exactly the same if you remove REM from the pushd and popd lines and comment out the cd lines instead.

To solve the problem, simply move the cd %myPath% line (or the pushd command) down below setlocal. Actually you do even not need to store the current directory initially when you change it only in between the setlocal/endlocal block (even if endlocal is not explicitly stated):

@echo off
set myPath=Subfolder1
set folderList=input.txt

setlocal EnableDelayedExpansion
REM pushd %myPath%
cd %myPath%

:process

REM The rest of my script

REM popd
pause
endlocal