Batch files: put commands in a file and run them
A batch file is a plain text file containing commands, one per line, saved with a name ending in .bat. Double-clicking it, or typing its name, runs the commands in order. Anything you find yourself typing more than twice is a candidate for one.
Making one
Open Notepad, type the lines below, and save the file as hello.bat. In the Save dialog, set "Save as type" to All Files, or Notepad adds .txt to the name.
@echo off
echo Hello, %USERNAME%.
echo Today is %DATE%.
pause@echo offstops each command being printed before it runs, so that only the results appear.echoprints a line.pausewaits for a key, which keeps the window open long enough to read when the file was started by double-clicking.
Comments
A line starting with rem is a note to yourself. So is a line starting with two colons. Windows ignores both.
rem This file makes a dated copy of my notes
:: written in November 2026Variables
Set a variable with set and read it between percent signs.
set folder=C:\Users\asha\Documents
echo Backing up %folder%Values passed when the file is run are available as %1, %2 and so on. A file run as greet.bat Asha sees Asha in %1. %0 is the name of the batch file itself.
Decisions with if
if exist notes.txt (
echo The file is there.
) else (
echo The file is missing.
)Other tests: if "%1"=="" echo No name given checks whether an argument was supplied. if errorlevel 1 checks whether the previous command reported failure. if not exist backup mkdir backup makes a folder only when it is absent.
The opening bracket must be on the same line as if, and else on the same line as the closing bracket.
Loops with for
for repeats a command for each item of a list. Inside a batch file, the loop variable is written with two percent signs.
for %%f in (*.txt) do echo Found %%fTyped directly at the prompt, the same loop uses one percent sign. The examples below were run that way.
C:\Users\asha\demo>for %f in (*.txt) do @echo Found %f
Found log.txt
Found notes.txt
Found report.txtfor /l counts: start, step, end.
C:\Users\asha\demo>for /l %n in (1,1,3) do @echo Line %n
Line 1
Line 2
Line 3for /d goes through folders, and for /r through a folder and everything below it.
Jumping with goto
A label is a line beginning with a colon. goto jumps to it. A simple menu is built this way.
:start
set /p answer=Type Y to continue, N to stop:
if /i "%answer%"=="N" goto end
echo Carrying on...
goto start
:endA small, useful example: a dated backup
This file copies a folder into a new folder named with today's date.
@echo off
rem Copy my notes into a folder named by date
set source=C:\Users\asha\Documents\Notes
set target=D:\Backups\Notes-%DATE:/=-%
if not exist "%source%" (
echo Cannot find %source%
pause
exit /b 1
)
xcopy "%source%" "%target%" /e /i /y
echo.
echo Copied to %target%
pause%DATE:/=-% takes the date and replaces each slash with a dash, because slashes are not allowed in folder names. How %DATE% looks depends on the computer's regional settings, so check with echo %DATE% on your own machine.
exit /b 1 leaves the batch file and reports failure, without closing the Command Prompt window.
Writing safely
Quote every path. "%source%" works whether or not the path has spaces. Without quotation marks, a space splits it in two.
Test with echo first. Before a batch file deletes or overwrites anything, put echo in front of the dangerous command. The file then prints what it would do. When the printed lines are right, remove the echo.
Check that things exist. if not exist before using a folder prevents a command from running in the wrong place.
Never let an empty variable reach a delete. del "%folder%\*" with folder unset becomes del "\*", the top of the drive. Test the variable first: if "%folder%"=="" exit /b.
Keep a copy. Test new batch files on a copy of your data in a practice folder.
Danger. A batch file runs with your full permissions and asks no questions. Do not run one you received by email, messaging app or download unless you have read it in Notepad and understand each line. Right-click and choose Edit, never double-click, to look inside. Files that end in .bat or .cmd attached to unexpected messages are a common way of spreading harmful software.
Running one automatically
- Put a shortcut to the file in the Startup folder (press Win + R and type
shell:startup) to run it each time you sign in. - Use Task Scheduler to run it at a set time, such as a backup every evening.
Where next
Batch files are old and limited. For anything beyond simple sequences, PowerShell is the modern tool on Windows, with proper variables, loops and error handling. The guide on PowerShell for Command Prompt users shows the first steps.
Examples shown with output were run on 12 November 2026 in a practice folder: Windows commands in cmd.exe on Windows 11, Linux commands with the GNU tools of Git Bash. The folder path and user name were replaced with made-up ones. Commands marked "not run here" are shown as you would type them.
This is an independent learning site. It is not connected with Microsoft, Apple or any Linux distribution. Windows, macOS and other names belong to their owners. Try commands first in a practice folder, never on files you cannot afford to lose.