Batch File Folder Commands

Till now we haven’t discussed about the most important commands that provide actual functionality to the batch files, which are none other than file and folder manipulation commands. So, in this post we will be seeing all the commands associated with folder manipulation operations like creating a new folder, how to rename it, display it, copy it, move it and delete the folder. The following is a list of commands that you can read about in this post. Just have a look at the list of them.

Batch File Folder Commands

1. Dir
2. Mkdir
3. Rmdir
4. Chdir
5. Ren
6. Pushd
7. Popd

Dir Command

The dir command serves to print the contents in a directory. As usual like all other commands, this also has some switches that narrow down the results according to the switches used.

Eg: C:Usersuser1>dir

Executing the dir command without any switches gives the following output.

Volume in drive C is OS
Volume Serial Number is C484-3F97
Directory of C:Usersuser1
03/04/2014  07:40 AM    <DIR>          dwhelper
03/12/2015  05:57 PM    <DIR>          Favorites
0 File(s)              0 bytes
31 Dir(s)  137,566,859,264 bytes free

Initially it prints the drive details and then the contents in a table format. The first column gives the created date of either the file or the folder, Next column indicates the time of creation, then follows the <DIR> that indicates it is a folder. Finally the last column displays the column name. At last the number of files, directories and free memory space available on the drive is displayed.

Flags in Dir Command
dir command with ‘/a’ switch will display all the files and folders in a directory, no matter what attribute is set.

‘dir /a[D|R|H|A|S|I]’ Switches description is as follows. ‘D’ to display Directories, ‘R’ to display Read-only files, ‘H’ to display Hidden files, ‘A’ to display Files ready for archiving, ‘S’ to display System files and ‘I’ to display not content indexed files.

‘dir /b’ Here ‘b’ stands for bare information. This serves similar purpose of dir command but only the folders and files are displayed in a single column without their extensions, date and time.

‘dir /d’ command simply displaying the file names with their extensions alone.

‘dir /l’ displays the output in lowercase.

‘dir /o’ sorts the order of files and folders.

Mkdir Command

The mkdir command stands for to make a directory, which is used to create new directories in the desired location. Also the mkdir command is a replacement of the ‘md’ command. We can create any number of directories just by using a single mkdir command. This command doesn’t have any switches.

Eg: mkdir My Programs (or) Md My Programs

The above two commands create a new directory named My Programs in the location where you are currently working. We can also create a folder inside another folder at the same time using a single command as the below one.

Eg: md new1\new2\new3

This command creates new1 folder then creates new2 inside new1 and at last new3 inside new2.

Rmdir Command

The rmdir command is short hand for the remove directory that is used to remove an already existing directory or folder, you can use the command as ‘rd’ also which serves for the same purpose. For deleting a folder using rd command we need to make sure that it does not contain any files or sub folders.

Eg: rd new1

The above command doesn’t delete the folder new1 as already their exits a folder new2 inside new1. So, we use two switches to solve this problem.
The rd command has two switches ‘/s’ and ‘/Q’. The ‘/s’ switch is used to delete all the directories, sub-directories and files inside the folder. So, you won’t have to manually delete all the sub folders and files inside them. The next switch ‘/Q’ stands for quiet mode, in quiet mode the command doesn’t prompt you for confirmation and does all the task of deleting silently.

Chdir Command

The chdir command is used to change the current working directory. It can also be used in short as ‘cd’. The small difference between cd and chdir is that the chdir command doesn’t require the path given to be in quotes when the folder names contains spaces. But in case of cd command, it is necessary to give the path in quotes.

Eg: chdir  programsstart menu
cd  “programsstart menu”

The above commands change the current working directory into start menu directory. Also we can use command ‘cd..’ to move one level up in the hierarchy of directories. And ‘cd’ to move into the current drive.

Ren Command

Rename command ‘ren’ is used to rename a directory on windows through the dos shell. ren can be replaced with its longer version as ‘rename’. The following example shows the syntax for renaming a folder.

Eg: rename programmer crazyprogrammer

The above command renames the directory ‘programmer’ to ‘crazyprogrammer’. This command doesn’t have any switches.

Pushd and Popd Commands

The pushd command is used to push the working directory or any specified directory. The current working directory is pushed into the stack and remains there until it is popped out. The popd command is used to pop out a directory that is pushed using the pushd command. Let us consider the below command to understand in detail.

Eg: Assume that I’m in ‘C:myfolder’ and I want to push directly to ‘C:windows’. I can do so using the following command

Pushd C:windows

Then my current working directory changes to ‘C:windows’ and the path ‘C:myfolder’ gets pushed into the stack. Next when I execute a popd command, my working directory changes to ‘C:myfolder’ as it pops out from the stack.

C:windows> popd

Try it yourself
Create a directory and apply all the commands over it, observe and note down the results.

2 thoughts on “Batch File Folder Commands”

Leave a Comment

Your email address will not be published. Required fields are marked *