Programming Field

Cmd - DOS/Command Prompt Reference

It is the command interpreter (‘Command Prompt’) for Windows NT series/XP and later. You can open the Command Prompt window by typing ‘cmd’ and executing it through ‘Run’ (opened with Windows key + R).

(For usage, please see ‘Details’.)

Syntax

cmd[.exe] [/A | /U] [/Q] [/D]
  [/E:ON | /E:OFF | /X | /Y] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
  [[/S] /C <command> | /K <command>]

Options

/A | /U
Specifies whether to output in ANSI (/A) or Unicode (UTF-16) (/U) encoding.
/Q
Launches the prompt with echo turned off (assuming that ‘echo off’ has been executed beforehand).
/D
Disables the AutoRun setting in the registry, preventing the execution of commands specified in it.
/E:ON | /E:OFF | /X | /Y
Sets whether to enable or disable command extensions when launching the prompt. Setting "/E:ON" makes it active, while "/E:OFF" makes it inactive. If not specified, the setting from the registry EnableExtensions is used, and if these are also not present, it defaults to being enabled (unverified).
Command extensions can be dynamically changed within a batch file using the Setlocal command. Settings made through this command take precedence, although changes made with Setlocal on the prompt itself do not have an effect.
‘/X’ is equivalent to ‘/E:ON’, and ‘/Y’ is equivalent to ‘/E:OFF’, having the same meaning respectively.
/F:ON | /F:OFF
Sets whether to enable or disable filename and directory name completion functionality. If enabled with ‘/F:ON’ when launching the prompt, you can use ‘Ctrl+F’ (inputting the character with the code 0x06) to complete file names and ‘Ctrl+D’ (inputting the character with the code 0x04) to complete directory names. In this case, registry settings are ignored.
If this option is not specified, the settings from the registry's CompletionChar/PathCompletionChar are used.
/V:ON | /V:OFF
Sets whether to enable or disable the delayed environment variable expansion feature when launching the prompt. If not specified, the setting from the registry's DelayedExpansion is used, and if these are also not present, it defaults to being disabled (unverified).
Enabling this feature allows the use of the ‘!’ character.
The delayed environment variable expansion feature can be dynamically changed within a batch file using the Setlocal command. Settings made through this command take precedence, although changes made with Setlocal on the prompt itself do not have an effect.
/S
Changes the handling of double quotation marks ‘" "’ in <command>. For more details, refer to the explanation.
/C <command>
Executes the specified content in <command>. After execution, ‘the cmd.exe that ran this command’ will exit. Compare with the /K option.
Since the string following /C (or /K), including spaces, is considered the entire command line, when specifying other options for cmd.exe, /C (or /K) must be placed at the end.
Note that ‘/R’ has the same meaning as ‘/C’.
/K <command>
Executes the specified content in <command>. After execution, ‘the cmd.exe that ran this command’ will not exit and will continue to display the prompt. When creating a shortcut for a batch file on Windows, using cmd.exe /K allows you to perform ‘Run the batch file’ followed by ‘Command Prompt’ consecutively. This is convenient when you want to display the command prompt in a specific environment (such as resident programs or environment variables).

Details

Usage of Cmd

Cmd is a program that controls the prompt in the Windows NT family. When executing batch files in the Windows NT family, Cmd.exe, not Command.com, performs the parsing of batch files unless explicitly specified.

Usage of ‘Cmd’ / Using explicitly

As mentioned earlier, running ‘cmd’ as a program will execute the Command Prompt. In general, there is no need to explicitly use ‘Cmd’ if you already have the Command Prompt open and want to execute commands from there or directly run a batch file. However, in the following cases, it is necessary to explicitly use ‘cmd’.

  • When to launch a new command prompt environment / When to open a separate window
    • On the Command Prompt, using Start allows you to execute commands in a separate window.
    • If you run ‘cmd.exe’ within the command prompt without using Start (without using the /C option), you need to execute Exit twice to exit the prompt.
  • When to use a prompt environment with settings made using ‘/U’, ‘/D’, ‘/E’, ‘/F’, and ‘/V’
    • In many cases, changes to registry settings or the use of the Setlocal command (within batch files only) may be sufficient.
  • When to execute a batch file from an environment other than the command prompt and then use the command prompt in the environment where the execution is completed
    • You can achieve this by using the ‘/K’ option to execute the batch file.
  • When to execute internal commands from an environment other than the command prompt or a batch file
    • Internal commands like Mklink need to be interpreted by cmd.exe itself, so you should execute them as ‘cmd /C <command>’.
  • When to execute a child batch file from a parent batch file and separate the processing result (state) of the child from the parent
    • It is possible by explicitly executing the batch file with ‘cmd /C’. However, in most cases, using the Setlocal command (and Endlocal command) may be sufficient.

Usage of ‘cmd /C’

‘cmd /C’ is a format used to execute commands. It is used by specifying the command line following ‘cmd /C’, such as ‘cmd /C echo hello’.

Normally, when executing from within the command prompt or a batch file, there is no need to use ‘cmd /C’, but

  • when executing internal commands from an environment other than the command prompt, or
  • when executing a batch file and separating the processing result,

... it is necessary to use ‘cmd /C’.

A similar option is ‘cmd /K’. ‘cmd /K’ has the effect of ‘displaying the command prompt continuously after executing the command’. It is mainly used for scenarios like ‘setting environment variables in a batch file and then displaying the command prompt in that environment’ (see the sample as well).

The command line specified after ‘cmd /C’ and ‘cmd /K’ can be enclosed in double quotation marks ‘" "’. However, whether double quotation marks are removed at runtime depends on how they are used and whether the "/S" option is present. For more details, please refer to the section on ‘Quotations and /S option’.

Delimiters and the command line

If the command line specified with /C or /K includes redirection (‘<’, ‘>’, ‘>>’), pipes (‘|’), and separators for two commands (‘&’, ‘&&’, ‘||’), it is interpreted in the same way as when using these in the command prompt or a batch file. If you attempt to execute ‘cmd.exe’ with these characters from the prompt or a batch file, they will be interpreted by the parent prompt/batch file. In such cases, you can either enclose the command line (<command>) in double quotation marks ‘" "’ or use the ‘^’ character to escape them. Note that if you enclose the command line in ‘" "’, you need to be aware of the considerations mentioned later.

Quotations and /S option

Quotations and /S option

When there are double quotation marks ‘" "’ in the command line specified with /C or /K, following two interpretations are used depending on the conditions.

  1. Remove a double quotation mark if there is at the beginning, and, if it is removed, remove the last double quotation mark (the rest remains unchanged)
  2. Use the double quotation mark as it is (without removing it) in the command line

The latter interpretation is adopted when all of the following conditions apply.

  • No /S option
  • There is only one set of ‘" "’ (only two ‘"’ characters).
  • There is a space character within ‘" "’
  • There are no special characters within ‘" "’ such as ‘&<>()@^|
  • The string within ‘" "’ is an existing executable file

* Among the ‘special characters’ mentioned here, the three characters ‘()@’ can be used in filenames. Please be aware of this if they are included in a filename.

Examples of which interpretation is adopted are as follows (for simplicity, the ‘/C’ switch is used).

cmd /c echo hello
Using interpretation 1
The actual command to be executed: ‘echo hello’
cmd /c "echo hello"
Using interpretation 1 (if the program ‘echo hello’ does not exist)
The actual command to be executed: ‘echo hello’ (‘" "’ will be removed)
cmd /c echo "hello"
Using interpretation 1 or 2 (Since ‘"’ is not at the beginning, it doesn't make a difference; it is the same in either case)
The actual command to be executed: ‘echo "hello"’ (‘"’ is not at the beginning, so it will not be removed)
cmd /c "app.exe" some parameter
Using interpretation 1 (no spaces within ‘" "’)
The actual command to be executed: ‘app.exe some parameter’ (‘" "’ will be removed)
cmd /c ""app.exe" some parameter"
Using interpretation 1 (there are not two ‘"’ characters)
The actual command to be executed: ‘"app.exe" some parameter’ (The leading and trailing ‘"’ will be removed)
cmd /c "C:\Program Files\Windows NT\accessories\wordpad.exe" hello.txt
Using interpretation 2 (if the program ‘C:\Program Files\Windows NT\accessories\wordpad.exe’ exists)
The actual command to be executed: ‘"C:\Program Files\Windows NT\accessories\wordpad.exe" hello.txt’
cmd /s /c "C:\Program Files\Windows NT\accessories\wordpad.exe" hello.txt
Using interpretation 1 (/S is specified)
The actual command to be executed: ‘C:\Program Files\Windows NT\accessories\wordpad.exe hello.txt’ (The leading and trailing (not at the end) ‘"’ will be removed, resulting in an attempt to execute the program ‘C:\Program’)
cmd /c "C:\Program Files\Windows NT\accessories\wordpad.exe" "hello world.txt"
Using interpretation 1 (there are not two ‘"’ characters)
The actual command to be executed: ‘C:\Program Files\Windows NT\accessories\wordpad.exe" "hello world.txt’ (The leading and trailing ‘"’ will be removed)
cmd /c "D:\Our Programs\SomeApp@v1.exe" data.xml
Using interpretation 1 (there is ‘@’ character)
The actual command to be executed: ‘D:\Our Programs\SomeApp@v1.exe data.xml’ (The leading and trailing (not at the end) ‘"’ will be removed, resulting in an attempt to execute the program ‘D:\Our’)
cmd /c ""D:\Our Programs\SomeApp@v1.exe" data.xml"
Using interpretation 1
The actual command to be executed: ‘"D:\Our Programs\SomeApp@v1.exe" data.xml’ (The leading and trailing ‘"’ will be removed, resulting in an attempt to execute the program ‘D:\Our Programs\SomeApp@v1.exe’)
cmd /c ""D:\Our Programs\SomeApp@v1.exe" data.xml
Using interpretation 1
The actual command to be executed: ‘"D:\Our Programs\SomeApp@v1.exe data.xml’ (The leading and trailing (not at the end) ‘"’ will be removed, resulting in an attempt to execute the program named ‘D:\Our Programs\SomeApp@v1.exe data.xml’)

If you always want the command line to be executed in the intended interpretation, it is advisable to always use the ‘/S’ option and add (possibly extra) ‘" "’ at the beginning and end of the command line (<command>).

Samples

Sample 1

cmd /S /C "mklink /S foo bar"

[Windows Vista or later] Executes the Mklink command. Since the Mklink command is an internal command, when executing it from outside the command prompt, you must always use ‘cmd /c’.

Sample 2

cmd /K D:\node\bin\nodevars.bat

Executes ‘D:\node\bin\nodevars.bat’ and continue to display the prompt. This is useful when you want to run the command prompt with specific environment variables set.

See also