Navigate to previous directory in windows command prompt
Solution 1:
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.
Solution 2:
You can use pushd and popd:
C:\WINDOWS>pushd \ C:\>popd C:\WINDOWS>
Solution 3:
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
Solution 4:
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
$