Introduction
A batch file is a text file of commands for the DOS or Windows command interpreter, run
one line after another as if they had been typed. The .bat extension goes
back to MS-DOS, where COMMAND.COM ran them and AUTOEXEC.BAT
ran at every start.
Windows NT brought a new interpreter, cmd.exe, and the .cmd
extension, which OS/2 also used. cmd.exe runs both today. The difference is
small: a few built-in commands, such as set and path, reset
ERRORLEVEL on success in a .cmd file and leave it alone in a
.bat file. A third extension, .btm, belongs to 4DOS and Take
Command.
The pages listed below describe the batch language as COMMAND.COM and
cmd.exe read it. They follow Microsoft’s documentation of
the Windows commands
and say where behaviour comes from independent analysis because Microsoft does not
publish a grammar for it.
Structure
A batch file is plain text, normally with Windows line ends, and cmd.exe
reads it in the console’s code page, not as Unicode. Many files start with
@echo off, which stops each command being printed before it runs. Comments
use rem, or ::, which is really a label nothing jumps to.
%NAME%expands a variable, set withset NAME=value. Withsetlocal enabledelayedexpansion,!NAME!expands it when the line runs and not when it is read, which matters inside loops.%1to%9are the arguments,%*is all of them, and%~dp0is the folder holding the script.- A line starting with a colon is a label.
gotojumps to one andcall :labelruns it as a subroutine. - A
forloop variable is written%%Fin a batch file and%Fat the prompt. ^escapes the next character, and at the end of a line it continues the command on the next.
@echo off
rem Copy each file named on the command line into a backup folder.
if "%~1"=="" goto usage
for %%F in (%*) do (
copy "%%~F" "%USERPROFILE%\Backup" >nul
if errorlevel 1 echo Could not copy %%~F
)
exit /b 0
:usage
echo Usage: %~nx0 file [file ...]
exit /b 1
Language reference
The reference is split into these pages.
- File structure – a batch file as bytes: extensions,
.batagainst.cmd, code pages, UTF-8 and the byte order mark, line ends, Ctrl-Z, line length, how the file is found and read - Lexical structure – how a line is read: the phases, special characters, escaping with
^and%%, quotes, comments, labels and every kind of command separator - Variables –
set, expansion with%and!, substring and replacement,set /aarithmetic, the special variables andsetlocal - Parameters and labels –
%0to%9,%*, the%~modifiers,shift,goto,calland subroutines - Control flow – every form of
ifandfor, parenthesised blocks andelse - Redirection and pipes – handles,
>,>>,<,&-duplication,|,&&and|| - Commands – each command built into the interpreter, its switches, and the external commands batch files lean on
- Errorlevel and exit codes – how a command reports success,
exit /b, and what sets or leavesERRORLEVEL - Dialects and versions – COMMAND.COM, cmd.exe, command extensions, OS/2 CMD, 4DOS and Take Command, and ReactOS
- Idioms – patterns that batch authors rely on: arguments with defaults, functions, here-text, scripts that carry another language
Not described
These pages are partial. This is what is missing.
Known, not yet covered
- Reference: the external commands that batch files use (
xcopy,robocopy,findstr,choice,timeoutand the rest) are named on the Commands page but their switches are not described; Microsoft documents each one. - Reference: the 4DOS and Take Command language (
.btm, its extra commands, functions and variables) is only summarised on Dialects and versions; JP Software publishes its help. - Viewer: command switches (
/b,/f), operators and external command names are left plain, and the 4DOS and Take Command commands are not coloured. - Viewer: a batch file that carries another language is coloured as batch throughout, except the Perl layout that
pl2batwrites (a#!line up to the next label line); Python, PowerShell or other polyglot layouts are not recognised.
Unknown
- Reference: Microsoft publishes no grammar for
cmd.exe. The Lexical structure page describes the parsing phases from the Microsoft documentation and from widely reported behaviour, and none of it was run againstcmd.exeorCOMMAND.COMfor these pages. Odd corners (a caret at the end of aremline, labels with unusual characters,%insideforloop variables of more than one letter) are given as reported, not as proved. - Viewer: the colouring follows the same reading, so a rarely used construct may be coloured differently from the way
cmd.exereads it.
Links
Software
Reference
- Windows commands – Microsoft’s reference to the commands
- cmd – Microsoft’s page on the interpreter and its options
- Command-line syntax key – how Microsoft writes command syntax
- SS64 CMD reference – independent guide to commands and batch syntax