51

Is there any command / tool to navigate previous directory in windows command prompt?

In linux usually use

cd -

for previous directory navigation.

ukanth
  • 10,660
  • 11
  • 43
  • 60

10 Answers10

50

You can use pushd and popd:

C:\WINDOWS>pushd \
C:\>popd
C:\WINDOWS>
John T
  • 163,373
  • 27
  • 341
  • 348
  • Thx John, But this is not exactly what i am looking for. – ukanth May 17 '10 at 16:24
  • 2
    It's exactly what I want for my batch scripts, thanks. It even changes drives without needing the \d switch – Deebster Feb 08 '12 at 10:59
  • 2
    __Warning__ With `pushd/popd` you __have to considerate the possibility of errors__. In a script when you `pushd` a directory that doesn't exists it will not finish in the stack, but when you `popd` you will exit of one level: variable not filled correctly,missprint,wrong drive... You think to be in a directory different from the one in which you are, with serious problem e.g. when you delete files/dirs. Disaster (Linux syntax): `cd $HOME; mkdir A; pushd A; mkdir B; pushd D; do stuff (in A, you believe in B); popd; rm -rf * ` you believe in A but...too late, all your home directory is gone. – Hastur Mar 05 '16 at 07:30
  • Thanks. It's the best and easiest way. – yushulx Apr 24 '20 at 07:48
30

Save the following to eg. mycd.bat somewhere in your path:

@echo off
if '%*'=='' cd & exit /b
if '%*'=='-' (
    cd /d %OLDPWD%
    set OLDPWD=%cd%
) else (
    cd /d %*
    if not errorlevel 1 set OLDPWD=%cd%
)

Then always remember to use mycd instead of cd to change directories and drives.

Alternatively, use a doskey macro:

C:\>doskey cd=mycd $*

The only caveat is if you omit the space between cd and .. or \, you will get the builtin version of cd not the doskey macro... and you still have to remember not to use C:, D: etc. to change drive.

Hugh Allen
  • 9,820
  • 6
  • 34
  • 42
  • It looks like it shouldn't work, but it does. OLDPWD gets set correctly. – Hugh Allen May 18 '10 at 07:52
  • 2
    +1 This is nice , it works! but painful to use mycd (or whtevr ) – ukanth May 18 '10 at 10:13
  • @HughAllen can make a doskey macro for cd\ and cd.. doskey cd\=cd \ or doskey cd\=c:\mycd.bat \ doskey C:=c:\mycd.bat C: – barlop Nov 07 '11 at 13:13
  • That bat will fail for c:\blah.bat %USERPROFILE% (so cd or cdd %USERPROFILE% will fail). To fix, change single quotes to double quotes. – barlop Nov 23 '11 at 12:52
5

if you are running the batch file you can use

  cd /D  %~dp0

This will jump back to the original path from where the batch file was run

ggonsalv
  • 351
  • 3
  • 10
5

If you want the exact behavior of bash, why not use bash? I have cygwin installed and it is very nice. It doesn't make you stick to its UNIX tools - it will happily call any windows executable. For cmd.exe builtins you can create an alias:

hugh@comp07 ~/testdir                             
$ alias cm='cmd /c'                               

hugh@comp07 ~/testdir                             
$ cm dir                                          
 Volume in drive C has no label.                  
 Volume Serial Number is AC2A-8378                

 Directory of C:\cygwin\home\hugh\testdir         

18/05/2010  02:02 PM    <DIR>          .          
18/05/2010  02:02 PM    <DIR>          ..         
               0 File(s)              0 bytes     
               2 Dir(s)   1,365,155,840 bytes free

hugh@comp07 ~/testdir                             
$ 
Hugh Allen
  • 9,820
  • 6
  • 34
  • 42
3

There's a freeware cmd clone with extra features including cd - called Take Command Console LE.

alt text

Gaff
  • 18,569
  • 15
  • 57
  • 68
Hugh Allen
  • 9,820
  • 6
  • 34
  • 42
1

The accepted answer is very great for the requirement. While I often have to switch among many recent directories instead of just just two (current and previous).

So I recently made a batch to make my daily jobs easier. https://gist.github.com/programus/2d2738b2a746140186f7738b678bdcec

Simon Sheehan
  • 9,114
  • 12
  • 52
  • 71
Programus
  • 153
  • 6
1

Depending what your goal is, you could just start a new cmd session by doing 'cmd', move directory and do whatever you want, when you then do 'exit' to leave the session you'll be back in the directory you were when you started the new session.

user210211
  • 11
  • 1
0

What I do is

rem capture the path of the initial dir
set RET_DIR=%CD%
rem do stuff...
rem and then return to the initial dir
cd %RET_DIR%
amphibient
  • 2,133
  • 9
  • 30
  • 41
0

I've been using this little-known program for directory changing on Linux and Windows for over 12 years and I really don't understand why many more people don't use it because it's brilliant.

CD Deluxe lets you change to the last directory with cd - and you can go to the second last directory with cd -2 and the third with cd -3 and do on. Just cd will give you the directory history.

https://github.com/m6z/cd-deluxe

paradroid
  • 22,761
  • 10
  • 76
  • 114
0

You can easily make a doskey macro to navigate to previous directory.

rem test.bat

@echo off
doskey cdx=@echo off$tpushd %cd%$T$tcd $1$t@echo on
doskey rcdx=popd
cmd
echo on

Save the above code to any batch file and whenever you want to open command prompt, open this file.

I have used pushd command to save current directory in stack and whenever you want to get back to previous directory, you can use rcdx or popd command.

Disadvantage: high memory usage by cmd if cdx command is used many times.