Using switches with commands

5 users found this article helpful

A command line switch (also known as an option, a parameter, or a flag) acts as a modifier to the command you are issuing in the Command Prompt window, in a batch file, or in other scripts. Usually, a switch is a single letter preceded by a forward slash. For an example, we will look at the DIR command and its switches.

The DIR command is used to display a listing of files and folders in the current folder. For example, if you open a Command Prompt window, use "CD \" to move to the Documents folder, type DIR, and press Enter, you may see something similar to the picture below.

DIR command - contents of drive C

The items are listed alphabetically by name (far right column), regardless of whether they are files or directories. Each line contains the item's timestamp, &ltDIR&gt in the next column (if the item is a directory), the size in bytes (if the item is a file), and then the name. Totals are displayed at the bottom, and there is some introductory information at the beginning (not displayed in the picture).

If you now display the help text for this command by typing DIR /? you will see a brief sentence describing what DIR does followed by the syntax for the command with brief descriptions for each option. The syntax for DIR should look like this:

DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N] [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

Each option for the DIR command is in brackets. As already demonstrated, you can use DIR all by itself. However, you may want to display this list in a particular format, in a particular order, and/or you may want to display only particular information. You can even display a listing from another directory.

For example, if you wanted to:

you could type:

DIR Users\Name /s /b /o:-n

and then press Enter. The output of your command would begin with the subfolder that is last alphabetically (possibly the Video subfolder) listing the path to this folder first (C:\Users\Logon\Video) and then paths to each file. The output should then work backwards through the alphabet, ending with the subfolder that is first alphabetically.

By experimenting with the various switches, you may find a result that would be particularly useful. You may want to record the result of the command you run in a log file, which you can do by redirecting the display output.

If you would like more information, you may want to do an internet search for command line, command prompt, DOS commands, batch files, or some other topic of interest. You may also want to review the information in the Related Articles.

Was this article helpful?

Tell us how we can improve it.