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
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.
:Dcmd, simply do popd afterwards to achieve the same as in Linux; if the stack should not be touched, to pushd "%CD%" afterwards to repush...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.
:D. And yes, jeb's answer is to be preferred (unless you really need the variables)With PowerShell, running pushd (the alias for Push-Location) alone will not display the directory stack (LIFO queue), it will only add the current directory to the top of it. You can view (and access) the stack with Get-Location -Stack.
C:\ >
C:\ > cd .\Temp
C:\Temp > pushd
C:\Temp > cd C:\Windows\
C:\Windows > pushd
C:\Windows > Get-Location
Path
----
C:\Windows
C:\Windows > Get-Location -Stack
Path
----
C:\Windows
C:\Temp
C:\Windows > $stack = Get-Location -Stack
C:\Windows > $stack
Path
----
C:\Windows
C:\Temp
C:\Windows > $stack.Count
2
C:\Windows > $stack.Path
C:\Windows
C:\Temp
C:\Windows > $stack.Path[0]
C:\Windows
C:\Windows > $stack.Path[1]
C:\Temp
popdthe stack, store the (restored) current directories in some array-like variables and to re-pushdthem then. If you only need to know the stack size, check out theprompt $+...$+in the prompt you can see the depth of the stack, but otherwise I don*t know a way to obtain stack entries aside from popd/ %CD%