Batch Files
A batch file is a plain text file full of CMD commands, saved with the extension .bat or .cmd. When you run it, cmd.exe reads it line by line and executes each line as if you had typed it. Every runnable example in this course is a batch file.
Your first batch file
Open Notepad (or VS Code), type these lines and save the file as hello.bat in your user folder. In Notepad, choose Save as type: All files so it doesn't become hello.bat.txt.
@echo off
echo Hello from a batch file!
echo Today is a good day to automate something.Now run it by typing its name at the prompt. The .bat extension is optional:
C:\Users\ada>hello
Hello from a batch file!
Today is a good day to automate something.You can also double-click a batch file in File Explorer. Its window closes as soon as the script ends, so scripts meant for double-clicking often end with pause, which prints Press any key to continue . . . and waits.
Running scripts by name or path
CMD looks for a command in this order: built-in commands, then the current folder, then every folder listed in the PATH environment variable. For each name it tries the extensions in PATHEXT (.COM;.EXE;.BAT;.CMD;…). So:
| You type | What CMD runs |
|---|---|
hello |
hello.bat (or .cmd, .exe…) in the current folder or a PATH folder |
tools\build.bat |
a path relative to the current folder |
"C:\My Scripts\build.bat" |
an absolute path, quoted because it contains a space |
The example below builds a tiny batch file with echo, shows it with type, then runs it:
@echo off
mkdir demo & cd demo
(
echo @echo off
echo echo Hi, I am %%~nx0 and I live in %%~dp0
) > greet.bat
type greet.bat
call greet.bat@echo off
echo Hi, I am %~nx0 and I live in %~dp0
Hi, I am greet.bat and I live in C:\Users\learner\demo\
Inside a batch file %% stands for a single %, so the outer script wrote %~nx0 into greet.bat. When greet.bat runs, %0 is the script's own name and the modifiers pick parts of it: ~n name, ~x extension, ~d drive, ~p path. %~dp0 ("the folder this script lives in") is the standard way for a script to find files next to itself, wherever it is run from.
call: run another batch file and come back
If a batch file runs another batch file by name alone, control transfers to the second one and never comes back. Use call to run it like a subroutine:
@echo off
mkdir demo & cd demo
> step.bat echo @echo step.bat is running
echo 1. calling step.bat
call step.bat
echo 2. back in main.bat, now without call
step.bat
echo 3. this line never runs1. calling step.bat
step.bat is running
2. back in main.bat, now without call
step.bat is running
Line 3 was never printed: after step.bat finished, CMD did not return to the script that started it. Always write call when one batch file runs another.
Ending a script: exit /b and exit codes
exit /b stops the current batch file (or subroutine) and returns to whoever called it. An optional number becomes the exit code, which the caller reads from %errorlevel%:
@echo off
mkdir demo & cd demo
(
echo @echo off
echo if "%%~1"=="" ^(
echo echo usage: check NAME
echo exit /b 2
echo ^)
echo echo Checking %%1... OK
echo exit /b 0
) > check.bat
call check.bat server01
echo exit code: %errorlevel%
call check.bat
echo exit code: %errorlevel%Checking server01... OK
exit code: 0
usage: check NAME
exit code: 2
%1 is the first argument passed to the script; %~1 is the same without surrounding quotes. Arguments get their own lesson, Arguments: %1 & %~, and exit codes are covered in Exit Codes & errorlevel.
Warning: plain
exit(without/b) closes the wholecmd.exewindow, not just the script. That is rarely what you want in a script that someone runs from a prompt.
.bat or .cmd?
Both extensions run with cmd.exe and behave the same for almost every script. The one historical difference: in a .cmd file, a few commands (set, path, assoc…) always reset errorlevel to 0 on success, while in a .bat file they leave it unchanged. New scripts can use either; .bat is the more familiar choice.
cmd /c and cmd /k
You can start a new Command Prompt that runs one command or script:
| Command | Meaning |
|---|---|
cmd /c script.bat |
run, then close the new shell (used by shortcuts, schedulers and other programs) |
cmd /k script.bat |
run, then keep the shell open at a prompt |
cmd /c "dir /b & pause" |
run a whole command line |
This is how other programs, such as Task Scheduler or a build tool, run batch files, and how this site's examples were recorded (cmd /c C:\tad\main.bat).
Characters and encodings
Notepad saves files as UTF-8 by default, while CMD reads batch files using the console's code page (on many systems 437 or 850). Plain English text works everywhere, but accented letters such as é may come out garbled. Either keep batch files ASCII-only, or switch the console to UTF-8 at the top of the script:
C:\Users\ada>chcp 65001
Active code page: 65001A practical template
Most real-world batch files share this shape:
@echo off
setlocal
rem ---- settings ----
set "APP=Inventory"
set "VERSION=1.4"
rem ---- work ----
echo Building %APP% %VERSION%...
call :log "compiling"
call :log "packaging"
echo Done.
exit /b 0
:log
echo [%APP%] %~1
exit /b 0Building Inventory 1.4...
[Inventory] compiling
[Inventory] packaging
Done.
setlocal keeps the variables private to this script, call :log jumps to the :log label as a subroutine, and exit /b 0 stops the main part before it falls through into the subroutine. These ideas are covered in setlocal & Delayed Expansion and call & Subroutines.
Learn more: Your First Script on Each OS · Bash Scripts
Test yourself
Script A runs other.bat without call. What happens after other.bat finishes?
- Script A continues on the next line
- Control does not return to script A
- An error is shown
- other.bat runs twice
What does %~dp0 give you inside a batch file?
- The current folder
- The drive and folder of the batch file itself
- The first argument
- The user's home folder
Which command ends a batch file with exit code 3 without closing the Command Prompt window?
- exit 3
- exit /b 3
- return 3
- quit 3