100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Batch

Creating and Running .bat Files

A practical guide to creating .bat files and running them correctly, including passing arguments.

FoundationsBeginner8 min readJul 10, 2026
Analogies

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.

batch
@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 Python

Never 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

Was this page helpful?

Topics covered

#Batch#WindowsBatchScriptingStudyNotes#MicrosoftTechnologies#CreatingAndRunningBatFiles#Creating#Running#Bat#Files#StudyNotes#SkillVeris