Batch file MS Win CMD PushD/PopD它是如何工作的,为什么不在“a”中;对于/f"-环
在我一次又一次找不到答案后,我写了以下脚本:Batch file MS Win CMD PushD/PopD它是如何工作的,为什么不在“a”中;对于/f"-环,batch-file,cmd,pushd,Batch File,Cmd,Pushd,在我一次又一次找不到答案后,我写了以下脚本: <!-- : 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 :: ------------------------------------
<!-- : 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...
/F的
在新的cmd.exe上下文中启动命令。
因此,pushd在那里执行,然后上下文将被关闭。
新cmd.exe中的pushd根本不会影响父上下文。
因此popd不会还原pushd
如果你只想弹出所有的pushd,你可以使用一个循环
@echo off
setlocal EnableDelayedExpansion
pushd C:\windows\
pushd c:\windows\system32
:popd_all
echo #: !cd!
popd && goto popd_all
此方法的工作方式与代码中使用的方式相同,使用for/f
循环:
@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)
我不知道你对没有参数的pushd
有什么期望?它的工作原理类似于一张CD
——它只显示当前目录。如果在提示符中包含$+
,则在cmd行上测试时可以看到pushd堆栈的深度。请参阅帮助pushd
和help prompt
尝试使用多个pushd的脚本,如果您在没有参数的情况下使用“pushd”,您将看到返回了一个推送目录列表-这就是我一直使用的cd的小差异:echo%cd%-多亏了您,我现在知道了一个较短的方法?提示可能是使用For循环的解决方案?您应该接受jeb的回答,For/f强制的新cmd上下文不知道以前cmd的pushd堆栈,并且您的目标无法解决。非常感谢您的教导:我不清楚。所以现在看来这是正确的答案但是真的没有PopD返回的解决方案吗?我知道新的cmd继承了环境,而不是pushd堆栈
你可以看到,当堆栈中有条目并发出cmd/k pushd
时,PopD
命令的这种行为以前没有记录过!我在的表5中插入了POPD
(我在这里向您表示感谢)<代码>:)
这个答案没有错!据说我不能把两个答案都标为正确。所以我必须选择,也许这不好。很抱歉下一次我希望我做得对,而且aks会更好。有人说,使用重定向到文件是一种丑陋的方法——我也更喜欢jeb的解决方案——但也许这就是为什么PC比家用电脑更安全的原因:CMD脚本比基本脚本更丑陋。。。最后我不得不说:如果我喜欢使用“For/F”循环,这是我的解决方案-当然我不必…-但这不是我的问题。下次我希望我能提出一个更好的问题。一点也不!
@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)