Discussion


Google Groups
Know Thy Tools
Visit this group

Notifications

I post notifications of new content on this site to my blog at http://www.anthonycramp.name.

Licence

cmd.exe


Overview

The default command line interface on Windows platforms is cmd.exe. It command line manipulation of the filesystem, the ability to launch programs from the command line, interact with running programs, and other abilities.

In addition to the above, cmd.exe supports various programming constructs and utilities for making working with the command line a little easier (such as tab completion). This page documents these programming constructs and utilities. The commands provided by cmd.exe are reported on other pages. The list of commands is available on the Command Line Tools

cmd.exe is just a program and can be started in the manner of most other Windows programs. It can be found via the Start button through All Programs > Accessories and then Command Prompt. An alternative mechanism for starting the program is via the Run dialog box. This dialog box can be shown by selecting Run ... from the Start menu, or via the Windows+R keyboard shortcut. In the Run dialog box type cmd.exe and press return.

Related Command Lines

There are other command line interfaces available for Windows. Two that provide a more powerful, yet similar, experience to cmd.exe are PowerShell and TakeCommand. PowerShell can be downloaded for free from Windows. TakeCommand is available from JPSoft and comes in three versions: two commercial and one free.

It is also possible to get Unix like command lines on Windows. cygwin and GnuWin32 are two such command lines.

Examples

Getting Help

You can get help for any command supplied by cmd.exe by adding /? after the command name. For example, to get help on cmd.exe itself, type

cmd /?

(the .exe extension is optional).

Command Line Completion

Command line completion is the mechanism that allows you to type a few characters of a file or directory, hit a special key (usually the tab key), and cmd.exe completes the rest of the name. This can be a big time saver.

By default command line completion is NOT enabled. It can be turned on per invocation of cmd.exe (supply argument /F:ON) but it is such a useful tool that I would advise turning it on by default. This can be done by setting the values for the registry keys

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\CompletionChar

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\PathCompletionChar


or

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\CompletionChar

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\PathCompletionChar


to the hex value of the control character you wish to use for character completion. The tab character has hex value 9.

(The two sets of registry keys in that the HKEY_LOCAL_MACHINE keys set the value for all users on a Windows install, the HKEY_CURRENT_USER sets only for the current user and overrides HKEY_LOCAL_MACHINE. The difference between CompletionChar and PathCompletionChar is that the value for CompletionChar will cause files and directories to be completed; PathCompletionChar only expands directory names).

Registry keys can be edited via the Registry Editor which can be invoked from the Run ... dialog (Windows+R) and type regedit and hit return. Then navigate down through the hierarchy following the key path segments.

After setting these keys you can use tab to complete your file and directory names. A few further comments:
  • only file and directory names in the current directory are completed
  • if multiple names can be completed the first (lexicographically) will be completed, hitting tab again will proceed to the next completion, and so on. Shift-tab will cycle backwards.
  • completed directory names are not terminated with a trailing backslash (\). If you want to expand multiple directories in the tree you will need to add the interstitial backslashs yourself.
  • If a name to be completed contains spaces to will be completed inside double quotes. If the completion is a directory name (such as "Program Files") you don't need to delete the final double quote before adding the trailing backslash. Add the backslash after the quote and proceed as if the double quotes weren't present.