Programming Field

Cd, Chdir - DOS/Command Prompt Reference

Changes the current directory. (acronym for Change Directory)

Syntax

cd [/D] <directory>
chdir [/D] <directory>
cd [<drive>:]
chdir [<drive>:]

Options

cd, chdir This command can be either ‘cd’ or ‘chdir’.
<directory>

Specifies the directory path to move to.

There are ‘..’ (the parent directory), ‘.’ (the current directory), and ‘\’ (the root directory) as special directories, and you can specify these names as a directory path.

* To change the current drive, you need to type ‘x:(enter)’ (x is a drive letter) directly without using Cd/Chdir.

/D [Windows NT series] If <directory> is a path which contains a drive letter, changes the current drive.
<drive>:

Displays the current directory for specified drive. Colon ‘:’ is necessary after the drive letter. (e.g. ‘cd D:’)

If <drive>: is omitted (simply type ‘cd’ or ‘chdir’), displays the current directory for the current drive.

Details

The prompt (session) has ‘the current directory’ and ‘the current drive’. When ‘C:\WINDOWS>’ is displayed on the prompt (in case Prompt setting is ‘$p$g’), the current directory is equal to the underlined. Chdir only changes ‘the current directory’, not ‘the current drive’ ([Windows NT series] except when called with ‘/D’ option).

The current directory exists for each drive. If you encounter ‘I probably changed the directory but it seems to be unchanged’, please use /D option or change the current drive. For details please see ‘Current directories in MS-DOS/Windows’ and ‘Changing the drive in the prompt’.

[Windows NT series] If you want to save the current directory temporarily and to restore it after any works, you can use Pushd and Popd commands. Also, if you change the current directory after calling Setlocal, you can restore by calling Endlocal (or by exiting the batch program).

[Windows NT series] [Extensions] If Command Extensions are enabled, the following behavior is added to Cd/Chdir.

  • If the case of the name specified at <directory> differs from the one from the actual directory name, the actual name will be used for the current directory. If Extensions are disabled, the current directory is set to the name <directory> as-is.
  • Even if the path <directory> includes space characters, you do not need quotations (" ") for the path; Cd/Chdir interprets the path correctly. If Extensions are disabled, quotations are required for the path including spaces.

Samples

Sample 1

chdir Temp

Moves (changes) to ‘Temp’ directory.

Sample 2

cd ..

Moves to the parent directory.

Sample 3

D:
cd \MyData\Music

Set the current directory to ‘D:\MyData\Music’. (Note that if you forget to move the current drive by typing ‘D:’ (simply type ‘cd D:\MyData\Music’), the current directory seems to be unchanged because the current drive is not changed.)

* ‘cd D:\MyData\Music’ means ‘changing the current directory of D drive’, so this syntax is useful when to change the current directory of the other drive without changing the current drive. (see Sample 5)

[Windows NT series] By using ‘/D’ option, you can change the current directory with following one line:

cd /D D:\MyData\Music

Sample 4

cd "C:\My Documents"

[Windows] To move to the directory which name is long (long-path) or includes spaces, you need to use quotations (" ").

[Windows NT series] [Extensions] If Command Extensions are enabled, quotations are not required.

Sample 5 (Prompt environment)

C:\My Documents>cd D:\MyData\Sheets
C:\My Documents>copy .\data.xls D:

Changes D drive's current directory to ‘MyData\Sheets’, and copies ‘C:\My Documents\data.xls’ into the directory ‘D:\MyData\Sheets’.

See also