2

After I faild again and again to find the answer I wrote these Script:

<!-- : first Line of "JustPopdBack.cmd"-Script (a CMD+Script for Windows)
@echo off %debug%
setLocal enableExtensions

::    Copyleft 2017 Markus Merkle (aka Mäx) - Licence: GNU-GPLv3
::  ----------------------------------------------------------------------------
::  Cause it seems stupid to make a short Script long just for Right
::  of the Author, so the Licence-Text is not embeded in this Script.
::  !!! Please have a look on <http://www.gnu.org/licenses/>
::      if you still don't know the GNU-GPLv3.
::  ----------------------------------------------------------------------------
::   This is just a free and open TestScript
::   Feel free to used or modify but please mark this work (how as done).
:: -----------------------------------------------------------------------------
 title JustPopdBack-Test
 color 0a                                 %= cause I like LightGreen on Black =%
 echo.
 echo. Just a Test-Script for PushD and PopD Command
 echo.
 echo. To use it please Enter some Directorys first
 echo. Don't shy to use diffrent Drives (C:\ or D:\ etc. )
 echo. ...and feel free to use the TAB-Key then...
 echo. Lets PushD in this Dir and after just Enter Nothing Popd out.
::******************************************************************************
 :dLoop for UserQuest and Pushd in
 echo.
 set "NxtDir="
 set /P "NxtDir=NextDir? :\> "
 if defined NxtDir (PushD %NxtDir% & goTo dLoop)
::------------------------------------------------------------------------------
 echo.
 echo. Lets see the PushD History
 pushD
 echo. now try again with "For /F"-Loop a PopD Back.
 for /f %%a in ('pushD') do (echo %%a && PopD)
 echo.
 echo This don't work - did you know why?
 timeOut -1
:*******************************************************************************
 :PopDback that will work
 PopD && Echo. Uh I PopD'd - let's'ee PushD now: && PushD && goTo PopDback
 :: but I don't like that - can you help to make me happy ?
 --------------------------------------------------------------------------------
 timeout -1
 color
 exit /b                                        %= That the End of this Script =%
 ::       Thnx to all my Teachers 
 ::    ...but the realy ones not the called & most have just 60 Minutes for me...

I hope you see why I wrote this Script and also hope all you like to give me many comments so we can find out what is going wrong here and maybe there ... (hey not in my Head - I hope :-)

My Targed of this Post is a Working of

for /f %a in ('PushD') do PopD
Mäx
  • 48
  • 6
  • 2
    I don't know what you expect from a `pushd` without arguments? It works like a `CD` - it simply shows the current dir. If you include `$+` in your prompt you can see the depth of the pushd stack while testing on the cmd line. See help `pushd` and `help prompt` –  Mar 04 '17 at 09:20
  • Try the script with more than one PushD and you'll see you got back a List of pushed Directorys if you use "pushd" without argument - thats the small diference to cd I allways use: echo %cd% - thanks to you I know now a shorter way... ? The Prompt could be the solution to use a For loop ? – Mäx Mar 04 '17 at 09:20
  • You should accept jeb's answer, the new cmd context forced by the for /f doesn't know of the previous cmd's pushd stack and your target isn't solvable. –  Mar 04 '17 at 10:55

2 Answers2

4

A for /F starts the command in a new cmd.exe context.
Therefore the pushd is executed there and then the context will be closed.
The pushd in the new cmd.exe doesn't affect the parent context at all.
Therefore the popd doesn't revert the pushd.

If you only want to popd all pushd's you could use a loop

@echo off
setlocal EnableDelayedExpansion
pushd C:\windows\
pushd c:\windows\system32

:popd_all
echo #: !cd!
popd && goto popd_all
jeb
  • 70,992
  • 15
  • 159
  • 202
  • Thank You very much for teaching me: That was not clear to me. So now it seems to be the right answer! - but is there really no solution to PopD-Back? – Mäx Mar 04 '17 at 09:28
  • IMHO the new cmd does inherit the environment but not the `pushd stack` You can see that when when having entries in the stack and issuing `cmd /k pushd` –  Mar 04 '17 at 10:52
  • 2
    This behavior of `popd` command was not previously documented! I inserted `POPD` in **Table 5** at [this answer](http://stackoverflow.com/a/34987886/778560) (and I am giving the credit to you here). **`:)`** – Aacini Mar 04 '17 at 13:01
  • THIS ANSWER IS NOT WRONG! I'm very said I can't mark two answers as right. So I've to choose, and well maybe it's bad. Sorry! Next time I hope I do it right and also aks even better. – Mäx Mar 04 '17 at 14:09
1

This method works in the way to used in your code, with a for /f loop:

@echo off
setlocal

pushd C:\windows\
pushd c:\windows\system32

pushd > "%tmp%\pushd.txt"
for /f "usebackq delims=" %%a in ("%tmp%\pushd.txt") do (echo %%a & PopD)
Aacini
  • 59,374
  • 12
  • 63
  • 94
  • Someone said, use a redirection to a file is a ugly method - and I also like the solution from jeb much more - but maybe this is why PC more secure than homecomputers: CMD-Scripting is much more ugly than BASIC ... At the end I've to said: This is my solution if I like to work with a "For /F" Loop - of cause I don't have to … - but that was not my questions. Next time I hope I make a better question. Thany at all! – Mäx Mar 04 '17 at 14:05