DEV Community

Victor James
Victor James

Posted on • Originally published at windowscmd.com on

CHDIR

CHDIR – This command-line tool changes Directory, move to a specific Folder. CHDIR is the equivalent word of CD. And the CHDIR is an internal command.

CMD Line

CMD Line is a text-based interface that transfers the command from the user to the OS.

CLI-Command Line Interpreter

The command-line interpreter (CLI) for windows is CMD.EXE. Also, you can use PowerShell which can automize many system administration tasks and these tasks are not approachable in command prompt. With the help of the CLI, you can communicate with your system by typing the text in commands. As a result, your system answers to the text that you have typed previously, and then the user can proceed with the next command line that shows up.

To open the Run command Window

Run command window helps you to run programs, open the files and folders. And there are several ways to open up the run command dialog box. This run command box is useful for you to head straight to the destined program easily.

By using the Keyboard shortcut Key:

WINDOWS+R

The shortcut key method to the run command box is the most commonly used one. Also, this method is in all versions of Windows. You need to press the Windows+ R key concurrently to open the Run command dialog box.

Run command box
Run command box

What does CHDIR command do?

As we have mentioned previously in this article, this CHDIR command displays the name of or changes the current directory. Also, the CHDIR is an internal command.

Syntax:


CD [/D] [drive:][path]
CD [..]

Enter fullscreen mode Exit fullscreen mode

Parameter explanation:

  • /D
    • To change the current DRIVE in addition to changing folder.

Tab completion:

By entering simply the part of the path and pressing TAB , Tab completion allows modifying the current folder.

Tab Completion is disabled by default, it has been known to create difficulty when using a batch script to process text files that contain TAB characters.


C:> CD Prog [PRESS TAB] 
Will go to C:\Program Files\

Enter fullscreen mode Exit fullscreen mode

And the Tab Completion is switched on by setting the registry value shown below


REGEDIT4
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"CompletionChar"=dword:00000009

Enter fullscreen mode Exit fullscreen mode
  • In MS-DOS , each drive had its own current directory , for multiple reasons.
  • In win32 there is one global current directory, but at the command line the appearance is still maintained that each drive has its own current directory, this is a fake-out by cmd.exe. The location for each drive is stored using the undocumented environment variables =A:, =B:, =C: etc.
  • GUI Windows applications may have a different current directory than the command prompt. Similarly, two CMD sessions can each have a different current directory.

Error levels:

%Error level% =0 – If the Current directory was changed.

%Error level% =1 – In case if the Directory does not exist or is not available or if a bad switch is given.

Examples:

To change to the parent directory


C:\Work> CD ..

Enter fullscreen mode Exit fullscreen mode

To modify to the grant-parent directory


C:\Work\backup\January> CD ..\..

Enter fullscreen mode Exit fullscreen mode

In order to change to the ROOT directory


C:\Work\backup\January> CD \

Enter fullscreen mode Exit fullscreen mode

To display the current directory in the specified drive


C:\> CD D:

Enter fullscreen mode Exit fullscreen mode

To display the current drive and directory


C:\Work> CD

Enter fullscreen mode Exit fullscreen mode

Display the current drive and directory


C:\Work> ECHO "%CD%"

Enter fullscreen mode Exit fullscreen mode

In a batch file to display the location of the batch script file (%0)


C:\> ECHO "%~dp0"

Enter fullscreen mode Exit fullscreen mode

In a batch file to CD to the location of the batch script file (%0)


C:\> CD /d "%~dp0"

Enter fullscreen mode Exit fullscreen mode

To move down the folder tree with a full path reference to the ROOT folder


C:\windows> CD \windows\java
C:\windows\java>

Enter fullscreen mode Exit fullscreen mode

For to move down the folder tree with a reference RELATIVE to the current folder


C:\windows> CD java
C:\windows\java>

Enter fullscreen mode Exit fullscreen mode

To move up and down the folder tree in one command


C:\windows\java> CD ..\system32
C:\windows\system32>

Enter fullscreen mode Exit fullscreen mode

If the command extensions are enabled, the CHDIR command is enhanced as follows:

  • The current directory string is not CASE sensitive.
  • So CD C:\windows will set the current directory to C:\Windows

CHDIR does not treat spaces as delimiters , so it is possible to CHDIR into a subfolder name that contains a space without surrounding the name with quotes.

Example


cd \My folder
cd "\My folder"

Enter fullscreen mode Exit fullscreen mode

An asterisk can be used to complete a folder name


Example: C:> CD pro* will move to C:\Program Files

Enter fullscreen mode Exit fullscreen mode

To change the Current drive:

To enter the drive letter followed by a colon


C:> E:
E:>

Enter fullscreen mode Exit fullscreen mode

In order to change the drive and directory at the same time, use CD with the /D switch


C:> cd /D E:\utils
E:\utils\>

Enter fullscreen mode Exit fullscreen mode

Verdict:

In this article, we have seen about the CHDIR command which Change Directory. If you have any suggestions or queries, let us know in the comment section.

Related Articles

  • CD This windows command-line tool changes the directory and moves to the specific folder. CHDIR is the equivalent word of CD.
  • CMD CMD windows command is to start a new shell in a command or executable program.
  • Attrib The Attrib command-line tool is to display or change the file attributes.
  • CHDIR The CHDIR command Changes the Directory and moves to the specific folder.
  • CLS CLS command is to erase all characters and graphics from the screen. To be more precise, this command clears the screen.

The post CHDIR appeared first on Windows Commands- SS64 Commands.

Discussion (0)