A note to self by Michiel van Oosterhout, last updated on .
The official documentation covers all built-in commands and command-line programs, but does not cover the syntax. One of the best references for Windows Command Processor script syntax is Windows Batch Scripting. SS64.com contains useful how-to guides and examples.
Syntax
@
prevents echoing the command line's remaining commands.echo(
prints a blank lineecho(on
printson
(also works foroff
and/?
)< nul (set /p _=hello)
printshello
without a trailingCR
,LF
%var:~n,m%
substring starting at indexn
and ending at indexn+m
(negative numbers count from the end of the string)%var:find=replace%
replaces all occurrences offind
withreplace
(case-insensitive,find
may start with the wildcard*
)if <cond1> if <cond2> (<commands>)
execute<commands>
ifcond1 AND cond2
(does not supportelse
)- Variable substitution:
%~1
removes surrounding double quotes (C:\Program Files\Git\bin\git.exe
)%~f1
fully qualified path (C:\Program Files\Git\bin\git.exe
)%~d1
drive letter (C:
)%~p1
path (\Program Files\Git\bin\
)%~n1
file name (git
)%~x1
file extension (.exe
)%~s1
replace names with short names (C:\PROGRA~1\Git\bin\git.exe
)%~a1
file attributes (--a------
,)%~t1
date/time (12/21/2022 01:47 PM
)%~z1
file size in bytes (45104
)%~$var:1
expand file name to first path found invar
(C:\Program Files\Git\cmd\git.exe
)%~ftza1
same as output ofdir
command
for /l %%a in (start,step,end) do command
whilestart
does not equalend
, executecommand
and addstep
tostart
for /f %%l in ('command')
executescommand
in a separate instance ofcmd.exe
for /f %%e in ('echo prompt $E^| cmd') do (set esc=%%e)
setsesc
to^[
(by reading the last output ofcmd
after changing its prompt to^[
)
Other
- F7: show history
- Esc: dismiss history
- F9: select command-line by number
- Enter: execute command line
- ← or →: edit command line