Creating and Running .bat Files
Creating a batch file requires nothing more than a plain text editor -- Notepad, Notepad++, or VS Code all work fine. Type your commands one per line and save the file with a .bat extension, such as backup.bat. It's important to avoid word processors like Microsoft Word, which save rich text formatting (fonts, hidden markup) that cmd.exe cannot parse as valid commands; the file must be pure, unformatted text.
Cricket analogy: Writing a .bat file in Notepad is like filling in a paper scorecard by hand instead of a fancy formatted PDF -- the scorer, cmd.exe, only understands plain, unformatted entries.
Running a Batch File
Once saved, you can run a batch file in two common ways: double-click it in File Explorer, or open cmd.exe and type its name (or full path) at the prompt. These aren't quite identical -- the current working directory when the script starts depends on how you launched it, and any command inside the script that references a relative path (like a subfolder or a file without a full path) resolves relative to that working directory, not necessarily the folder the script itself lives in.
Cricket analogy: Double-clicking a .bat file is like a substitute fielder walking straight onto the field and starting the pre-set drill without anyone briefing them -- cmd.exe just launches and executes top to bottom.
Running with Parameters
Batch files can accept command-line arguments, which are available inside the script as %1 for the first argument, %2 for the second, and so on up through %9. This lets a single script behave differently depending on what's passed to it -- for example, greet.bat Alice would make %1 equal to Alice inside the script. There's also a useful built-in reference, %~f0, which always expands to the full path of the currently running script itself, handy for self-locating logic.
Cricket analogy: %1 and %2 in a batch file are like a captain calling out 'bowler' then 'field' as two separate instructions handed to the twelfth man -- the script picks each one up in the order given.
@echo off
REM greet.bat - demonstrates positional parameters
echo Hello, %1!
echo You said your favorite language is %2.
echo Full path to this script: %~f0
pause
REM usage: greet.bat Alice PythonNever double-click a .bat file you downloaded from an untrusted source before reading its contents in a text editor first. Because batch scripts run with your full user privileges immediately on execution, a malicious script can delete files, exfiltrate data, or install software with no confirmation dialog at all.
Choosing an Editor and File Encoding
File encoding is a subtle but real gotcha. Batch files should be saved as plain ANSI text or UTF-8 without a byte-order-mark (BOM). Some editors, including older versions of Notepad, add an invisible BOM at the start of the file when you choose 'UTF-8' as the save format. That extra sequence of bytes can get prepended to your very first command, causing cmd.exe to fail to recognize it -- a confusing bug for anyone unaware the BOM is even there.
Cricket analogy: A BOM silently breaking the first line of a batch file is like a scorer accidentally writing an extra mark before the first ball of the innings, throwing off the whole scorecard reading.
- Create batch files with a plain text editor like Notepad or VS Code, then save with a .bat extension.
- You can run a batch file by double-clicking it in File Explorer or typing its name/path into cmd.exe.
- The current working directory when a script starts affects how relative paths inside it resolve.
- Arguments passed on the command line are available inside the script as %1, %2, %3, and so on.
- %~f0 expands to the full path of the currently running batch file, useful for self-referencing scripts.
- Save batch files as plain ANSI or UTF-8 without a byte-order-mark (BOM) to avoid the first command silently failing.
- Always inspect an unfamiliar .bat file's contents before running it, since it executes with your full user permissions.
Practice what you learned
1. How do you access the first argument passed to a batch file on the command line?
2. What does %~f0 expand to inside a running batch script?
3. Why can saving a .bat file with a UTF-8 byte-order-mark (BOM) cause problems?
4. What determines the working directory when you double-click a .bat file in File Explorer?
5. Why should you avoid running an unfamiliar downloaded .bat file without inspecting it first?
Was this page helpful?
You May Also Like
What Is Batch Scripting?
An introduction to what Windows batch scripting is, where it came from, and when to use it.
Batch Script Syntax Basics
The core syntax rules of batch scripting: variables, delayed expansion, conditionals, and loops.
Comments in Batch Files
How to write and use comments in batch files, and why documentation matters for long-lived scripts.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics
Microsoft TechnologiesMVVM Design Pattern Study Notes
.NET · 30 topics