5

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

3
  • do you need to keep the stack or won't you mind if it gets lost? Commented May 22, 2018 at 7:57
  • The only thing I could think of is to popd the stack, store the (restored) current directories in some array-like variables and to re-pushd them then. If you only need to know the stack size, check out the prompt $+... Commented May 22, 2018 at 8:01
  • With a $+ 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% Commented May 22, 2018 at 8:09

3 Answers 3

16

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I never noticed that behavior before. Strange, considering that I use that command regularly. You clearly won this code-golfing :D
Thanks, this is exactly what I needed. Interesting, this also works on linux but with a little bit of a difference. On linux in addition to printing the stack, we are moved to the directory at the top of the stack.
@Phuthib, to achieve the same in Windows cmd, simply do popd afterwards to achieve the same as in Linux; if the stack should not be touched, to pushd "%CD%" afterwards to repush...
I wonder why this does not work in powershell.
6

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.

4 Comments

When you put the goto :loop inside the &&() the ||() part isn't neccessary
Thanks LotPings, I do find your answer helpful, and will keep it for future reference. But will award @Jeb as the correct answer as I was more looking for a single command.
@LotPings: true, but I wanted to show the "else part" too. Phuthib: please don't rename me, I like my name :D. And yes, jeb's answer is to be preferred (unless you really need the variables)
It looks like pushd output can't be processed by a for /f nor piped (just redirected to a file). So your batch has an advantage.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.