How to Change the Current Directory in Batch Script

MD Aminul Islam Feb 02, 2024
How to Change the Current Directory in Batch Script

Sometimes we need to change the current working directory for various purposes. In Batch, there is a command CD, a shorted form of Change directory, and an internal command of Windows CMD.

This command is used to change the current working directory. This article will show how we can change the current working directory.

Also, we will see some examples with explanations regarding the topic.

Change the Current Directory in Batch Script

The general syntax for this command is CD /D [DRIVER:][PATH] or CD [YOUR_PATH]. Here, /D is used to change the current drive.

If you want to set a directory in the same drive as the current directory, then you can use the format CD [YOUR_PATH]. Let’s see an example.

C:> CD /D F:\DIRECTORY
F:\DIRECTORY\>

In the above example, we changed a directory currently located in C drive to the directory in F drive.

You can also change the drive like below.

C:> F:
F:>

Above shared an easy method to change the drive. Remember, if your directory contains any space, you have to put your directory in a double quote like below.

C:> CD /D "F:\DIRECTORY\NEW FOLDER"
F:\DIRECTORY\NEW FOLDER>

To change the current parent directory, you can follow the below example.

C:\CURRENT_DIRECTORY> CD ..

You can change the root directory by following the below example.

C:\CURRENT_DIRECTORY> CD ..\..

You can follow the below two commands to display the current drive and directory.

C:\CURRENT_DIRECTORY> CD

Or

C:\CURRENT_DIRECTORY> ECHO "%CD%"

Go to the specific folder of the current directory. To do that, you have to follow the below example.

C:> CD NEW_FOLDER
C:\NEW_FOLDER>

Important Note Regarding the Current Directory

Previously in MS-DOS, there was a current directory for each drive. But the updated Win32 has only one global current directory.

Windows GUI applications can have a different current directory. Even two different command prompt sessions can have a different current directory.

And CHDIR command is the command CD synonym.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Batch Directory