Commands Associated With Files And Folders: Batch File Programming

Google+ Pinterest LinkedIn Tumblr +

This chapter will cover all the commands associated with the files and folder for performing

operations such as creating a new file, folder, renaming it, displaying it, copying it, moving it and deleting



The ‘dir’ command is used to display the contents in a directory. Likewise other commands, this too have

few switches available to narrowing down the result i.e. displaying the file in various other ways than that

of the usual way.

When the ‘dir’ command is used alone without any switches, it will display the contents of the current

directory. The following screenshot shows how this works,

I have executed the ‘dir’ command in my C: drive, and it displayed the files and folders available in the C

drive. The indicates that it is a folder or a Directory, where as the rest of them are just file, even

the file extensions are displayed at the end of the file name, so that it is easy to identify what kind of file it

is and also displays the size of the file and free memory space available on the drive.


Every directory will have any one of the attribute set, the commonly used attributes are Readonly,

Directory, Hidden files, Archived file, system file, indexed and so on. By default the ‘dir’ command

wont display the system files, sometimes you have to revoke the read-only permission to modify a file, to

maintain a little bit of privacy you may also have to hide a directory, all these can be done with the help

of the ‘/A’ switch along with the ‘dir’ command. When the ‘dir /a’ command is executed, it will display

all the files and folders in a directory, no matter what attribute is set. The ‘dir /a’ can be optimized

completely by using few parameters available for the ‘/a’ switch, which is very useful in narrowing down

the result that we are looking for.

The ‘dir /a’ command alone will display all kinds of files, directories and sub-directories that reside in the

current directory. Here is a screenshot that displays all the files including the system files.

If you notice the above screenshot, it was displayed as , which is nothing but a

folder which is common for all the users, and where their documents reside.


Further if we want to narrow down the result, we can use the following available parameters for the ‘/a’

switch, they were,

‘D’ for Directories, ‘R’ for Read-only files, ‘H’ for Hidden files, ‘A’ for Files ready for archiving, ‘S’

for System files and ‘I’ for Not content indexed files.

If I want to view all the files hidden by the user alone, then I can use the command ‘dir /ah’, likewise if I

want to view all the read-only files then I have to use the command ‘dir /ar’ and so on. The following

screenshot displays the system files alone.

All the files and folders displayed in the above screenshot were operating system file, these system files

are hidden by default.

The ‘dir /b’ command is used to perform the operation similar to the ‘dir’ command, but it will display

bare information i.e. the directory name alone, which doesn’t contain any further info such as the file size,

date, file and free space available. The below screenshot will shows how it will display the output,

This format doesn’t even have any kind of header information.


The ‘dir’ command by default will include the separators between the integers while displaying the file

sizes, the ‘/c’ switch when used with the ‘dir’ command will perform the same operation. To avoid the

‘dir’ command displaying the separator in between the integers while displaying the file sizes, you may

use the ‘/-c’ switch for revoking separator. The below screenshots reveals the difference between the ‘dir

/c’ command and ‘dir /-c’ command,

The above screenshot contains the separator in between the integers, where as the below screenshot

doesn’t include any separators,

The ‘dir /d’ command will display the contents of a directory by simply displaying the file names with its

extensions alone, the below screenshot shows how the ‘dir /d’ displays the output,

The ‘/l’ switch when used with the ‘dir’ command will display the output in lowercase.


The ‘dir /o’ command is used to sort or order the way it displays the output, we can sort the output based

on the following criteria,

N – Name

S – Size

E – Extension

D – Date / Time

G – Group directories first

By default the ‘dir’ command sorts the output in terms of name. Incase if I wish to sort the output in

terms of size then I can use the command ‘dir /OS’, likewise you may use any of the above parameters.

The ‘dir /p’ command will pause displaying the output, if there is a screen full of information, then it

prompts the user to interact by pressing any key to display the rest of the information.

The ‘dir /q’ command will display the owner of the file, the below screenshot shows how it displays the

output when used with the /q switch.

The ‘dir /s’ command operates just like the ‘tree’ command, but will give more detailed info rather than

the ‘tree’ command, since it will display all the contents of the directories and its sub-directories.

The ‘/T’ switch for the ‘dir’ command is really helps in logging, where as you can log the file and folder

activities such as the creation of file, file when was last accessed and modified. The ‘/T’ switch does have

a set of parameters that is used to narrow down the usage, for making the ‘dir’ command to display when

a file was created, then we have to use the ‘dir /TC’ command, to know when a file was last access then


we have to use the ‘dir /TA’ command, likewise to know when a file was last modified, then we have to

use the ‘dir /TW’ command.

The ‘dir /w’ command is used for displaying the same in a wide list.

The ‘dir /x’ command is used to display short names for a non-8dot3 file names. If you are not aware of

the non08dot3 files, here is an example,’ Program Files’ is a non8dot3 file, where it can be shortly

written by the OS as ‘PROGRA~1’

Here is another crap done by Microsoft, that ‘dir’ command by default will display the date in a four digit

year, but there is an additional switch ‘dir /4’ which is used to display four digit year, I don’t know

whether it has something to deal with the Y2K bug or something like that.


The ‘mkdir’ command is short for the make directory, which in turn is used to create new

directories in the specified location also the ‘mkdir’ command is a replacement of the ‘md’ command. We

can create multiple directories just by using a single ‘mkdir’ command.

If I wish to create a new directory named ‘pictures’, then I can use the command ‘mkdir pictures’ , if

there is a space in between the folder name then we can use the Double quotes surrounded by the

directory name that contains the space in between them, for example if I want to create a directory name

‘My Collections’, then I can use the use the command ‘mkdir “My Collections”’.

If I wish to create 3 different directories that reside one into one, namely ‘a’,’b’ and ‘c’ then I can use the

command ‘mkdir a\b\c’, where as the directory ‘b’ resides inside ‘a’ and ‘c’ resides inside ‘b’.


About Author

Leave A Reply