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.
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 nameCMD 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.
@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:
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:
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) |
@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 folderstep 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:
@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 |
@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)?
- With a dash: -b
- With a slash: /b
- With a plus: +b
- With a colon: :b
In mkdir logs && echo done, when is echo done run?
- Always
- Only if mkdir succeeded
- Only if mkdir failed
- Never
How do you print a literal & with echo?
- echo \&
- echo ^&
- echo '&'
- echo %&