Windows Command Line – Navigate to Previous Directory

command linewindows

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

In linux usually use

cd -

for previous directory navigation.

Best Answer

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.

Related Question