TExcelTAD
Home › Windows CMD & Batch › CMD Syntax

CMD Syntax

Every line you type at the Command Prompt, or put in a batch file, follows the same basic shape: a command name, then arguments, often including switches that start with a slash.

text
 dir   /b /o:n   C:\Projects\*.txt
 ───   ───────   ─────────────────
  │       │              └── argument: what to work on
  │       └── switches: how to do it (/b = bare, /o:n = order by name)
  └── command name

CMD splits the line at spaces, so an argument that contains spaces must be wrapped in double quotes: cd "C:\Program Files". Single quotes have no special meaning in CMD.

Switches start with a slash

Where Linux tools use -l or --long, Windows commands use /x. Some switches take a value after a colon, such as /o:n (order by name) or /a:d (only directories). Switches are case-insensitive, and for most commands the colon is optional: /o:n, /on and /O:N mean the same thing.

cmd · try it yourself
@echo off
mkdir demo
cd demo
mkdir photos
echo draft> notes.txt
echo 1,2,3> data.csv
echo --- everything, by name:
dir /b /o:n
echo --- folders only:
DIR /B /ON /AD
echo --- files only, reverse order:
dir /b /o:-n /a:-d
--- everything, by name:
data.csv
notes.txt
photos
--- folders only:
photos
--- files only, reverse order:
notes.txt
data.csv

A minus sign inside a switch value means "not" or "reverse": /a:-d is "not directories" and /o:-n is "reverse name order".

Getting help: /?

Almost every command prints its own help page when you add /?. There is also a help command that lists the built-in commands:

cmd
C:\Users\ada>mkdir /?
Creates a directory.

MKDIR [drive:]path
MD [drive:]path

If Command Extensions are enabled MKDIR changes as follows:

MKDIR creates any intermediate directories in the path, if needed.
For example, assume \a does not exist then:

    mkdir \a\b\c\d

is the same as:

    mkdir \a
    chdir \a\b
    ...

(Shortened.) When a help page is longer than the window, pipe it into more to read it one screen at a time: xcopy /? | more.

Internal and external commands

Some commands are built into cmd.exe itself (internal commands); others are separate programs in C:\Windows\System32 (external commands).

Internal (part of cmd.exe) External (separate .exe files)
cd, dir, echo, set, if, for, copy, move, del, ren, mkdir, rmdir, type, cls, call, goto xcopy, robocopy, findstr, find, sort, more, attrib, ipconfig, ping, tasklist, where

The difference matters when something is not found. If you mistype a command, CMD searches the folders in PATH and then gives up with this message:

cmd
C:\Users\ada>dri
'dri' is not recognized as an internal or external command,
operable program or batch file.

Running several commands on one line

CMD has three operators for putting commands together:

Operator Meaning
a & b run a, then always run b
a && b run b only if a succeeded (exit code 0)
a || b run b only if a failed (non-zero exit code)
cmd · try it yourself
@echo off
mkdir demo & cd demo
echo step 1 & echo step 2
mkdir logs && echo created the logs folder
mkdir logs 2>nul && echo created it again || echo logs already exists
if exist logs\ echo logs is a folder
step 1
step 2
created the logs folder
logs already exists
logs is a folder

The second mkdir logs failed because the folder already existed, so the && part was skipped and the || part ran. 2>nul hid the error message; the Redirection lesson explains it. One subtle detail: echo step 1 & … actually prints step 1 followed by a space, because echo prints everything up to the &. Write echo step 1& echo step 2 when a trailing space would matter (for example when writing to a file).

Grouping with parentheses and continuing lines

Parentheses group several commands into one block. A block can span several lines, which is how if and for bodies are written:

cmd · try it yourself
@echo off
(
  echo first line
  echo second line
)
if 2 GTR 1 (
  echo two is greater than one
) else (
  echo this never prints
)
echo This command is long, so it ^
continues on the next line.
first line
second line
two is greater than one
This command is long, so it continues on the next line.

A caret ^ at the very end of a line joins it with the next line. The caret is CMD's escape character in general: ^& prints a literal &.

Special characters

These characters have a meaning to CMD, so they need quotes or a ^ when you want them as plain text:

Character Meaning
& && || run several commands
| pipe output into another command
< > >> redirect input/output
^ escape the next character / continue a line
% variables: %NAME%; in batch files %% is a literal percent sign
( ) group commands
" quote arguments containing spaces
cmd · try it yourself
@echo off
echo Tom ^& Jerry
echo 1 ^< 2 and 3 ^> 2
echo Progress: 100%%
echo "Quotes keep & and > as plain text"
Tom & Jerry
1 < 2 and 3 > 2
Progress: 100%
"Quotes keep & and > as plain text"

Note that echo prints the quotes too; other commands, such as cd or dir, remove them. The escaping lesson covers the tricky cases.

Learn more: The Prompt & Command Syntax · Bash Syntax

Test yourself

How do Windows commands usually mark a switch (option)?

  1. With a dash: -b
  2. With a slash: /b
  3. With a plus: +b
  4. With a colon: :b

In mkdir logs && echo done, when is echo done run?

  1. Always
  2. Only if mkdir succeeded
  3. Only if mkdir failed
  4. Never

How do you print a literal & with echo?

  1. echo \&
  2. echo ^&
  3. echo '&'
  4. echo %&