Home > Commands A-M > Commands Ch-Cz

CMD


Description | Syntax | Parameters | Switches | Related | Notes | Examples | Errorlevels | Availability

Starts a new instance of the NT command interpreter.

A command interpreter is a program that lets you type commands. Use the EXIT command to stop the new command interpreter and return control to the old one.


Syntax

CMD /?

CMD [/A | /U] [/D] [/F:ON | /F:OFF] [/Q] [/T:fg] [/V:ON | /V:OFF] [[/X | /E:ON] | [/E:OFF | /Y]] [[/S] [/C command] | [/K command]]


Parameters

none.


Switches
/? (NTXP)
Display help.
/A (NT3.5)
Output ANSI Characters.
/C command (NT3.1)
Specifies that the command interpreter is to perform the specified command and then exit. This switch must be the last switch on the command-line.
/R same as /C.
/D (NT2000)
Ignore registry AUTORUN commands (HKLM | HKCU \Software\Microsoft\Command Processor\AutoRun).
/F: [ON | OFF] (NT2000)
Enable/disable auto-completion of pathnames entered at the CMD prompt. /F:OFF is the default.
At the command prompt CTRL-D (^D) gives folder name completion and CTRL-F (^F) gives file and folder name completion.
These CTRL keys build up a list of paths that match and display the first matching path. Thereafter, repeated pressing of the same control key will cycle through the list of matching paths. Pressing SHIFT with the control KEY will move through the list backwards.
/K command (NT3.1)
Runs the specified command, program, or batch program and then displays the NT command prompt. This switch must be the last switch on the command-line.
/Q (NT3.5)
Turns echo off.
/S (NT2000)
Strips quotes from command string.
/T:fg (NT4)
Sets the foreground/background colours. See COLOR for a list of colour codes.
/U (NT3.51)
Output UNICODE Characters.
/V: [ON | OFF] (NT2000)
Enable/disable delayed environment variable expansion. (This allows a FOR loop to specify !variable! instead of %variable% expanding the variable at execution time instead of at input time).
Environment expansion preference can be set permanently in the registry HKLM | HKCU \Software\Microsoft\Command Processor\DelayedExpansion Set to either 0x1 or 0x0
/X (NT4 ONLY)
/Y (NT4 ONLY)
/E: [ON | OFF] (NT2000)
Enable/disable extensions to CMD.EXE.
/X same as /E:ON.
/Y same as /E:OFF.
Command Extensions are enabled by default unless you are running a 16-bit script (command.com or a .BAT file) in which case they are not available at all.
Command extensions are controlled by setting a value in the registry (HKCU \Software\Microsoft\Command Processor\EnableExtensions) 0 = disable extensions, 1 = enable extensions

Related

EXIT - Use this to close a CMD shell and return.
CALL - Call one batch program from another
START - Start a separate window to run a specified program or command
DOSKEY Edit command-line, recall commands
Equivalent Linux BASH commands:
builtin - Run a shell builtin
bash - run the bash shell
chroot - Run a command with a different root directory
csh - run the C shell
exec - Execute a command
ksh - run the Korn shell
sh - run the Bourne shell


Notes

CMD.exe is the NT equivalent of Command.com in previous operating systems. The older 16-bit command processor command.com is supplied to provide backward compatibility for 16-bit DOS applications.

To ensure that an NT batch file will not run if accidentally copied to a Windows 95/98/ME machine you should use the extension .CMD rather than .BAT.

The %COMSPEC% environment variable will show if you are running CMD.EXE or command.com.

It is possible to run the Windows 2000 CMD.EXE under NT 4.0.

The environment Variable %CMDCMDLINE% will expand into the original command-line passed to CMD.EXE.

Under Windows NT, the command-line is limited to 256 characters. The command processor was patched from NT 4 Service Pack 4 to prevent this limitation from causing a stack overflow.

Multiple commands separated by the command separator '&&' are accepted if surrounded by quotes.

This logic is used to process quote (") characters:

  1. If all of the conditions are met, then quote characters on the command-line are preserved:
  2. Otherwise, old behavior is to see if the first character is a quote character and if so, strip the leading character and remove the last quote character on the command-line, preserving any text after the last quote character.

The key combination ALT-ENTER will switch a CMD window to full-screen mode. Press ALT-ENTER again to return to a normal Window.


Examples

Missing the /Y option of COMMAND.COM to step through (debug) a batch file line by line? Here is a replacement:

@ECHO OFF
 :: 2004Jan31 Rick Lively - Create
 :: 2004Feb03 Rick Lively - Add COMSPEC option
 :: 2005Mar23 Steve Rogan - Rework for long file names, to handle directory changes,
 ::			 delay envvar expansion, and handle multi-line source commands.
 :: 2005Apr14 Steve Rogan - Fix a long file name problem, allow multiple invocations
 ::			 using unique temp debug file names.

 :: Check for correct syntax.
 IF "%~1" == "" GOTO :rlCmd30

 :: Check if we're already running.
 IF EXIST %TEMP%\"dbg_%~n1.bat" GOTO :rlCmd10

 :: Hide some process variables and enable special env var processing.
 SETLOCAL ENABLEDELAYEDEXPANSION

 SET rlDbgFile="dbg_%~n1.bat"

 :: Check for possible extensions, and build a sub-batch file.
 IF EXIST "%~1.bat" (
  :: CALL %0 "%~1.bat"
  SET rlFileName=%~pn1.bat
  GOTO :BuildFile
 )
 IF EXIST "%~1.cmd" (
  :: CALL %0 "%~1.cmd"
  SET rlFileName=%~pn1.cmd
  GOTO :BuildFile
 )
 IF EXIST "%~1" (
  :: CALL %0 "%~1.cmd"
  SET rlFileName=%~pnx1
  GOTO :BuildFile
 )
 GOTO :rlCmd20

:BuildFile
 :: Create a sub-batch file that calls us back after each line where we have the opportunity to "rem"
 :: out the next executable line, execute it, or goto the end.  Don't export remarks and blank lines.
 SET rlLineOpen=0
 SET rlCmd=
 ECHO @ECHO OFF>%TEMP%\%rlDbgFile%
 FOR /F "tokens=* usebackq" %%F IN ("%rlFileName%") DO (
  SET rlCmdLine=%%F
  SET rlCmdLine=!rlCmdLine:"='!
  IF /I "!rlCmdLine:~0,4!" NEQ "REM " IF /I "!rlCmdLine:~0,2!" NEQ "::" IF /I "!rlCmdLine!" NEQ "" (
   CALL :ExportPrefix %0 "%~n1.bat"
   ECHO %%F>>%TEMP%\%rlDbgFile%
   CALL :ExportSuffix
   )
 )
 ENDLOCAL
 :: Invoke our sub-batch file, then clean up afterwards.
 CMD /C %TEMP%\"dbg_%~n1.bat" %2 %3 %4 %5 %6 %7 %8 %9
 ERASE "%TEMP%\dbg_%~n1.bat"
 GOTO :rlCmd40

:ExportPrefix
 :: Check if this is a line continuation.  If not, output the prefix.
 IF "%rlLineOpen%" LSS "1" (
  ECHO CALL %1 %2 "%rlCmdLine%">>%TEMP%\%rlDbgFile%
  ECHO %%rlCmd%% GOTO :rlCmdNext>>%TEMP%\%rlDbgFile%
 )
 GOTO :EOF

:ExportSuffix
 :: Check if this is the end of the continuation.  If so, clear the "open" flag.
 SET rlCmdLine="%rlCmdLine: =%"
 IF "%rlCmdLine:~1,1%" == ")" SET /A rlLineOpen=%rlLineOpen%-1
 :: Check if this line is continued.  If so, set the "open" flag.
 IF "%rlCmdLine:~-2,1%" == "(" SET /A rlLineOpen=%rlLineOpen%+1
 :: Check if the line is "open". If not, output the suffix.
 IF "%rlLineOpen%" LSS "1" ECHO :rlCmdNext>>%TEMP%\%rlDbgFile%
 GOTO :EOF

:rlCmd10
 :: This routine is called between every executable line in our sub-batch to give
 :: us the chance to control execution of the next line.
 SET rlCmd=y
 SET /P rlCmd=(%time%)-^>"%~2" [Yncq]?
 IF /I "%rlCmd%" EQU "c" (
  %COMSPEC% /K PROMPT=$T $P$_rlCmdY$G
  GOTO :rlCmd10
 )

 IF /I "%rlCmd%" NEQ "Y" IF /I "%rlCmd%" NEQ "q" IF /I "%rlCmd%" NEQ "n" GOTO :rlCmd10
 IF /I "%rlCmd%" EQU "n" SET rlCmd=
 IF /I "%rlCmd%" EQU "Y" SET rlCmd=::
 IF /I "%rlCmd%" EQU "q" SET rlCmd=GOTO :EOF
 GOTO :EOF

:rlCmd20
 ECHO.
 ECHO file '%1' '%1.bat' '%1.cmd' NOT found

:rlCmd30
 ECHO.
 ECHO To step through (2K or XP) NT batch file: Test
 ECHO prompts: [Yncq] Yes, No, Comspec, Quit
 ECHO.
 ECHO    %0 Test
 ECHO OR %0 Test.bat
 ECHO OR %0 Test.cmd
 ERASE "%TEMP%\dbg_*.bat">NUL

:rlCmd40
 SET rlCmd=
 SET rlCmdLine=
 SET rlFileName=
 SET rlDbgFIle=
 SET rlLineOpen=
 GOTO :EOF

Errorlevels

none.


Availability
External
DOS
none
Windows
none
Windows NT
NT3.1 NT3.5 NT3.51 NT4 NT2000 NTXP NT2003

Last Updated: 2006/12/01
Direct corrections or suggestions to: Rick Lively