MS-Win-CMD PushD/PopD 它是如何工作的,为什么不在"For /f"循环中



我一次又一次失败以找到我写下这些脚本的答案:

<!-- : 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...

希望您明白为什么我写这个脚本,也希望您所有人都喜欢给我很多评论,以便我们可以在这里找出什么问题,也许在那里...(嘿,不在我的脑海 - 我希望: - )

我对这篇文章的折磨是

的工作
for /f %a in ('PushD') do PopD

a for /F在新的cmd.exe上下文中启动命令。
因此,按钮是在此执行的,然后将关闭上下文。
新的cmd.exe中的推送根本不会影响父上下文。
因此,popd不会恢复按压。

如果您只想流行所有pushd,则可以使用循环

@echo off
setlocal EnableDelayedExpansion
pushd C:windows
pushd c:windowssystem32
:popd_all
echo #: !cd!
popd && goto popd_all

此方法在代码中使用的方式可以使用for /f循环:

@echo off
setlocal
pushd C:windows
pushd c:windowssystem32
pushd > "%tmp%pushd.txt"
for /f "usebackq delims=" %%a in ("%tmp%pushd.txt") do (echo %%a & PopD)

最新更新