Is there a way to undo all pushd
at the end of script. What I have is:
pushd somwhere
rem doing stuff
goto end
:end
popd
goto :EOF
What I'd like to have is:
setlocal
pushd somwhere
rem doing stuff
goto :EOF
But that doesn't work, the directory stays "pushd". Another try where I can't say how many pushd
will occur would be:
set CurrentDir=%CD%
rem doing a lot of pushd and popd
pushd somwhere
:POPBACK
if /i not "%CD%" == "%CurrentDir%" popd & goto POPBACK
But that looks like I can easily get stuck at :POPBACK
. Adding a counter to exit that loop is a bit uncertain at which dir I end up.
In that context I'd like to know if a can see the stack of pushd directories. The only thing I found was $+
in prompt $P$+$G
which adds a '+' for each pushd directory e.g. D:\CMD++>
. But I can't see a way how to make use of that.
EDIT:
I just noticed there is something wrong with the $+
of prompt
that confused me.
The setlocal
in the example above actually makes the script return to the initial directory, but the prompt shows a '+' indicating a missing popd
.
D:\CMD>test.cmd
D:\CMD>prompt $P$+$g
D:\CMD>setlocal
D:\CMD>pushd e:\DATA
e:\DATA+>goto :EOF
D:\CMD+>popd
D:\CMD>
EDIT:
Due to the hint to the difference between local environment and directory stack I expanded the example form above, pushing to C:\DATA before calling the script. the script returns to where it has been called from (C:\DATA). So far so good. The first popd
shows no effect as it removes the last pushd
from stack but not changing the directory, since the directory change has been undone by the (implicit) endlocal. The second popd
returns to initial directory as expected.
D:\CMD>pushd C:\DATA
C:\DATA+>d:\cmd\test.cmd
C:\DATA+>prompt $P$+$g
C:\DATA+>setlocal
C:\DATA+>pushd e:\DATA
e:\DATA++>goto :EOF
C:\DATA++>popd
C:\DATA+>popd
D:\CMD>