# How to change dir quickly and effectively in Windows command line?

For example, I am in C:\test1, and I have another directory C:\test2\sub2

I want a command line tool can switch to sub2 quickly like this:

quickchange sub2


...and this program may setup the directory database for entire disk and update the database manually.

So, is there a suitable tool for my requirements in the Windows command line?

(ps: I don't want to use cygwin)

Edit: I want the program to be able to search the database to find where sub2 resides and change to it. If multiple directories match, it should ask me to select.

I know a small tool named QCD can do the job, but QCD hasn't updated for long time and has some bugs.

-

Sorry for the late answer, it took me a couple years to find this question.

You can use a batch script that does this, then refer to that batch script either with a DOSKEY macro or put it in your path.

I wrote this script, it has the following use cases:

1. If the directory exists without looking in subdirectories, switch to it.
2. If one match is found, switch to it.
3. If multiple matches are found, list them and prompt for a choice.
5. If no argument is given, list all subdirectories and prompt for a choice.

Here's my script:

@echo off

setlocal enabledelayedexpansion

set target_directory=%1
set /a prompt_index=0
if exist %target_directory% (
set chosen_path=%target_directory%
goto :path_set
)
for /f "tokens=*" %%A IN ('dir /s /b /ad %1 2^>nul') do (
SET /a prompt_index+=1
set dir_!prompt_index!=%%A
)
if %prompt_index%==0 (
) else if %prompt_index%==1 (
set chosen_index=1
) else if %prompt_index% GTR 1 (
for /l %%i in (1, 1, %prompt_index%) do (
echo %%i. !dir_%%i!
)
echo.
set /p chosen_index="Choose a directory: "
)
set chosen_path=!dir_%chosen_index%!
:path_set
endlocal & set chosen_path=%chosen_path%
if defined chosen_path (
pushd %chosen_path%
set chosen_path=
)

-

You can use ColorConsole, amongst plenty other goodies, you'll get a tabbed interface with multi CMD and favorite commands, which makes life a LOT easier.

ColorConsole is freeware and portable (doesn't require installation).

-

Try something like this:

@ECHO OFF
goto %1
:docs
cd c:\Users\Phoshi\Documents
GOTO END
:userdir
cd c:\users\Phoshi
GOTO END
:programfiles
cd "c:\program files"
GOTO END
:startup
GOTO END
:logs
cd "c:\Program Files\Miranda-IM\Logs\"
GOTO END
:music
cd C:\Users\Phoshi\Audio
GOTO END
:pictures
cd C:\Users\Phoshi\Pictures\
GOTO END
:drop
cd "C:\Users\Phoshi\Documents\Drop Box\"
GOTO END
:system32
cd "C:\Windows\System32\"
goto end
:fallout
cd "c:\Program Files\Bethesda Softworks\Fallout 3\"
goto end
:end


Save it as a batch file, put it on your path (\system32, for example), and then you can type, say "go docs" to go to your documents. You could write a small program to help manage them, if you want, too.

The specific example you have would be very difficult, and probably wouldn't work if two directories had the same name.

-

I wrote a cd replacement in Perl over a decade ago, that gives you ksh-like features (e.g. CDPATH, cd -, $OLDPWD) in a DOS command line. You can do everything with it except tab-completion, which it can't do because it's not a shell replacement, it's a cd replacement, so when you hit <TAB> you're still under the command shell. I'd have to clean up the code a bit (so I'm not embarrassed; it's rock solid in terms of bugs), but I can post it if you're interested. Here's the built-in help for it: ============================================================================= cdp - provides UNIX-style cd capabilities under Win32 Copyright (C) 1997-8, Joseph L. Casadonte Jr. All rights reserved. Version: 2.12 Usage: cdp [-h | -help] cdp - cdp [-x] [-unix | -dos] cdp [-x] -quote <AbsolutePathName> cdp [-x] -quote [-look <dir> | -exc <dir>] <Target> cdp [-x] <oldpart> <newpart> cdp [-x] [-noquote] <AbsolutePathName> cdp [-x] [-noquote] [-look <dir> | -exc <dir>] <Target> Commands are interpretted in the order given above. Note: the presence of the -[no]quote option affects the order. options: -h Get help (this message) and exit -help Get help (this message) and exit -x suppresses the evaluation of$CDP.

-dos    cdp with no arguments echos current working directory
-unix   cdp with no arguments changes to root of current directory
[default: -unix]

-quote  will assume multiple words in <Target> or <AbsolutePathName>
are quoted together (i.e. program files will be interpreted
as 'program files').  May be negated (-noquote).  Used mainly with
$CDP (see below). [default: -noquote] -look <dir> will attempt to find a matching directory by appending <dir> to each element of$CDPATH\<Target>, or failing that, by
matching an element of $CDPATH\<Target> itself. May be specified more than once: cdp -look foo -look bar perl5 Cannot be used in conjunction with -exc. Used mainly with$CDP (see below).

-exc <dir>   will look exclusively for a matching directory by appending
<dir> to each element of $CDPATH\<Target>. If no exact match is found it will *not* match to an element of$CDPATH\<Target> alone.  Cannot be used in conjunction with
-look.  Used mainly with $CDP (see below). cdp - ----- Will change to the immediately previous directory ala UNIX by using the$OLDPWD environment variable.

cdp [-unix | -dos]
------------------
Will either change to the root directory (-unix option) or echo the current
directory (-dos option).

cdp [-quote] <AbsolutePathName>
-------------------------------
Will attempt to change into the directory <AbsolutePathName>.  Note:
<AbsolutePathName> is anything that begins with a drive letter (e:\foo), a
front- or back-slash (\temp) or a period (../foo).

cdp [-quote -look <dir> -exc <dir>] <Target>
--------------------------------------------
cdp will use the environment variable $CDPATH to find a valid directory to change into by appending <Target> to each element in$CDPATH until a match is
found.  $CDPATH (or %CDPATH% for the dos-heads) is a semi-colon delimited list of directories. Directories with spaces in them are fine (i.e. c:\program files) but UNC names are not (i.e. \\machine\share). The order of the directories in$CDPATH is important, as the first match wins.
Once a valid directory is found, cdp will change to that directory,
changing drives if necessary.  cdp will not match the current working
directory.

You may include the current directory in the CDPATH environment variable by
using the '.' (i.e. set CDPATH=.;c:\my documents;d:\source).  Most people
will put this at the very beginning of their CDPATH.  It is also extremely
useful if using aliasing (see below).

The two options, -look and -exc, provide ways of extending the match criteria.
If you always change into the source directory of a project (xxx\src), then
you can put the option "-look src" in your $CDP variable, to always look for a "src" directory when a match on <Target> is found. Multiple "-look <dir>" options are resolved in the order given on the command line. If none are found, the match to <Target> alone stands. The "-exc <dir>" option is used to look exclusively for a particular match to <Target>\<dir>. If one is not found, no match is returned (i.e. <Target> alone will not match). See the Examples below for more info. cdp <oldpart> <newpart> ----------------------- You can substitute a piece of the current directory name specified by <oldpart> with <newpart>. For example, if you were in the directory: c:\release\really\long\directory\structure\src you could type: cd release debug and find yourself in: c:\debug\really\long\directory\structure\src assuming it exists. See examples below. ============================================================================= Examples ======== Given the following directory structure: e:\program files e:\program files\perl5 e:\program files\perl5\docs e:\program files\perl4 e:\program e:\program\perl5 e:\program\perl5\docs e:\files e:\files\perl5 e:\files\perl5\docs and the following Environment Variables: set CDPATH=.;e:;e:\program files set CDP=-quote set OLDPWD=e:\files\perl5 this command in this directory will result in this ----------------- --------------------- ------------------------- cdp perl5 c:\ e:\program files\perl5 cdp perl5 e:\program files e:\program files\perl5 cdp perl5 e:\program e:\program\perl5 cdp e:\files\perl5 <anywhere> e:\files\perl5 cdp \program e:\<anywhere> e:\program cdp ..\perl5 e:\program files\perl4 e:\program files\perl5 cdp -unix e:\program files\perl5 e:\ cdp -dos e:\program files\perl5 e:\program files\perl5 cdp - <anywhere> e:\files\perl5 cdp program files e:\program\perl5 e:\program files cdp -x program files e:\program\perl5 e:\files\perl5 cdp 'program files' e:\program\perl5 e:\program files cdp -noquote 'program files' e:\program\perl5 e:\program files cdp -noquote program files e:\program\perl5 e:\files\perl5 cdp -x -quote program files e:\program\perl5 e:\program files cdp -look docs perl5 c:\ e:\program files\perl5\docs cdp -look docs perl4 c:\ e:\program files\perl4 cdp -exc docs perl5 c:\ e:\program files\perl5\docs cdp -exc docs perl4 c:\ cdp: path not found ============================================================================= Backslash ('\') vs. Frontslash ('/') ==================================== cdp allows you to use, and even mix, backslashes and frontslashes together. The following are identical: cdp c:\program files\perl5 cdp c:/program files/perl5 cdp c:\program files/perl5 cdp c:/program files\perl5 ============================================================================= Aliasing ======== It is possible to alias the DOS command 'cd' to be cdp. This way, you use the features of cdp without having to remember to type a seperate command. To do this, you need to use a DOSKEY macro. For example: doskey cd=cdp$*

The "$*" tells DOSKEY to pass all the arguments it received to the new program. To remove the alias, type: doskey cd= If you ever need to invoke the regular DOS 'cd' command, you can always use 'chdir'. For more information, type: doskey /? ============================================================================= Environment Variables ===================== Note: Environment Variables are case sensitive for Perl. CDPATH - semi-colon delimited list of directories to search thru [default: "."] [$CDPATH: ".;c:\my documents;l:\gc3\glog_deploy\web\xsl;l:\gc3\glog_deploy\web;l:\gc3\glog\server;l:\gc3\glog\oracle;c:\sandbox;c:\my documents\home_page;\;"]

CDP - a set of default options to be used each time cdp is invoked. This
is so you do not have to specify the same options every time you use
cdp.

These options are processed *before* any options on the command line, and
can be overridden (i.e. if -quote is set in $CDP, -noquote on the command line overrides it). The use of$CDP can be suppressed by specifying
the -x option on the command line.
[$CDP: "-quote"] CDPFNAME - filename to be used by the Perl script to communicate with the surrounding batch script (this is a serious hack!). Can be changed at the top of the script. [$CDPFNAME: "c:\temp\cdpath.bat"]

TEMP - used by the scripts to create $CDPFNAME. [$TEMP: "c:\temp"]

OLDPWD - used to store the previous directory (see "cdp -" invocation)

=============================================================================
Files used
==========
%TEMP%\cdpath.bat - file used by the Perl script to communicate with the
surrounding batch script.  Can be changed to whatever
is needed at the top of the script.

=============================================================================
====================================

=============================================================================
=============================================================================

-
looks nice. not sure it'll meet the OP's specs, but i'd be interested in grabbing a copy of it. – quack quixote Oct 26 '09 at 5:00

You might as well use 'subst'

It let's you assign a drive letter to a directory. So you can set directory \my stuff\whatever\dunno\something' to be X: and you can set '\more stuff\not that stuff\but\this stuff\ to Y:

Now you can just use the drive letter to do all the stuff you'd normally do.

-

for example, I am in c:\test1, and I have another directory c:\test2\sub2

I'm assuming

c:\test1>cd c:\test2\sub2


isn't an option ?
You can always use history to switch between directories (press F7).

I want a command line tool can switch to sub2 quickly like this:
quickchange sub2
and this program may setup the directory database for entire disk and update the database manually.

And if there are (and we always assume there will be) two directories by the name of "sub2" ? This concept will not exactly like this.

so, is there suitable tool for my require in Windows command line?

There is something similar, see pushd and popd in your cmd help. They enable you to quickly switch between two directories.

See here:

C:\test1>pushd c:\test2\sub2
c:\test2\sub2>pushd C:\test1
C:\test1>popd
c:\test2\sub2>popd
... to infinity


but you'll still have problems if switching between multiple directories.

(ps: I don't want to use cygwin)

-

Use pushd and popd.

C:\Documents and Settings\~quack> pushd d:\foo

D:\foo> pushd e:\bar

E:\bar> popd

D:\foo> popd

C:\Documents and Settings\~quack>


pushd pushes your current directory onto a stack, then cd's to the argument directory. The stack can be as large as you want. popd removes the directory on the top of the stack and cd's to it.

-

You actually can't do that. It is kind of impossible, from a technical aspect.

Imagine you had a folder structure like this:

School Work/
English/
Assignments/
Class Work/
Documents/

Science/
Assignments/
Class Work/
Documents/

Math/
Assignments/
Class Work/
Documents/


Now, If you ran quickchange Documents from within the School Work folder, what folder would you expect to go to? There are three matches, and no way of knowing which one you actually want to get to. Sure, it might be able to prompt you as to which folder you are after, but that does not scale very well

Imagine this same folder structure for every student in a school, on a server, so it looks something like this:

Josh Hunt\
School Work\
[etc]

Cathy Wells\
School Work\
[etc]

Jack Thompson\
School Work\
[etc]

James Smith\
School Work\
[etc]

[repeat for all 1300 students]


Clearly, this does not scale well. There are now 3900 Documents folders.

The only possible way to make navigating folders easier is using Tab Completion. You would use it like this:

/Schoolwork/\$ dir E<tab>[nglish/]A<tab>[ssignments/]


Just to clarify, <tab> indicates you hitting the tab button on the keyboard, whereas [nglish/] indicates what tab completion has filled in for you.

-
+1, i've no idea what i would do without tab completion :) – quack quixote Oct 26 '09 at 2:30
so I hope the tool can give me a selection for same named dir, a small tool QCD can do this job, but QCD hasn't updated for long time and has several bugs. – Zhongshu Oct 26 '09 at 2:31

Like this cd \s* ?

-
What does that do? – Tom Wijsman Apr 29 '12 at 14:26

Tab autocompletion of the path after cd isn't fast enough? Usually it only takes me 5 or 6 keystrokes to get where i need to go that way.

-
Because I don't remember where the sub2 resident, for entire disk remember every directory is difficult ;) – Zhongshu Oct 26 '09 at 2:10
This assumes that you don't have any two directories with the same name on the system anywhere. Good luck with that – MDMarra Oct 26 '09 at 2:11
@Zhongshu Most people have some sort of idea how files and folders are organised on their computer. – Josh Hunt Oct 26 '09 at 2:17
so I hope the tool can give me a selection for same named dir, a small tool QCD can do this job, but QCD hasn't updated for long time and has several bugs. – Zhongshu Oct 26 '09 at 2:28