问题
I am trying to list the directories that have been added to the directory stack using the command:
pushd
I cannot find any references or command on how to print the directory stack contents.
Any help is appreciated
回答1:
A pushd
without parameters outputs the list of the stacked directories.
pushd c:\
pushd windows
pushd help
pushd windows
pushd en-us
pushd
Will output
c:\Windows\Help\Windows
c:\Windows\Help
c:\Windows
c:\
c:\Temp
The latest pushed directory is missing here, but can simply retrieved by %__CD__%
.
And there is one more directory (in my case C:\temp
), as that will be the directory after the last executed popd
.
回答2:
Nice challenge. As already noted, there seems to be no built-in way to get that information, so you need a script to do it step-by-step:
@echo off
setlocal enabledelayedexpansion
set origin=%cd%
rem build a demo stack:
pushd c:\
pushd windows
pushd help
pushd windows
pushd en-us
rem get stack step by step:
set i=0
:loop
popd && (
set /a i+=1
echo !i! --- %cd%
set "p[!i!]=%cd%"
) || (
goto :TopOfStack
)
goto :loop
:TopOfStack
echo stack empty.
cd %origin%
rem restore stack:
set p[
for /l %%i in (%i%,-1,1) do (
pushd "!p[%%i]!"
)
Note: see also jeb's answer.
来源:https://stackoverflow.com/questions/50462331/list-directory-stack-in-windows