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(onprintson(also works foroffand/?)< nul (set /p _=hello)printshellowithout a trailingCR,LF%var:~n,m%substring starting at indexnand ending at indexn+m(negative numbers count from the end of the string)%var:find=replace%replaces all occurrences offindwithreplace(case-insensitive,findmay start with the wildcard*)if <cond1> if <cond2> (<commands>)execute<commands>ifcond1 AND cond2(does not supportelse)- Variable substitution:
%~1removes surrounding double quotes (C:\Program Files\Git\bin\git.exe)%~f1fully qualified path (C:\Program Files\Git\bin\git.exe)%~d1drive letter (C:)%~p1path (\Program Files\Git\bin\)%~n1file name (git)%~x1file extension (.exe)%~s1replace names with short names (C:\PROGRA~1\Git\bin\git.exe)%~a1file attributes (--a------,)%~t1date/time (12/21/2022 01:47 PM)%~z1file size in bytes (45104)%~$var:1expand file name to first path found invar(C:\Program Files\Git\cmd\git.exe)%~ftza1same as output ofdircommand
for /l %%a in (start,step,end) do commandwhilestartdoes not equalend, executecommandand addsteptostartfor /f %%l in ('command')executescommandin a separate instance ofcmd.exefor /f %%e in ('echo prompt $E^| cmd') do (set esc=%%e)setsescto^[(by reading the last output ofcmdafter 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