Introduction to Reading and Writing Text Files
Batch scripting has no dedicated file-I/O statements like Python's open() or C's fopen(); instead, it repurposes the shell's own redirection operators and the FOR /F command to read and write plain text files. Writing is done by redirecting the output of ECHO or other commands into a file with > or >>, while reading is done by looping over a file's lines with FOR /F, which parses each line into tokens you can capture as variables. This indirect approach is less elegant than a real scripting language but is sufficient for logging, configuration files, and simple data processing.
Cricket analogy: A scorer who has no dedicated scoring app and instead repurposes a plain notebook and tally marks to record every run mirrors Batch repurposing ECHO and redirection instead of dedicated file-I/O functions.
Writing to Files
ECHO Hello > output.txt creates (or completely overwrites) output.txt with the single line 'Hello', while ECHO Another line >> output.txt appends a new line to the end of the existing file instead of erasing it. This distinction between > (truncate/create) and >> (append) is one of the most common sources of bugs in Batch scripts, since accidentally using > inside a loop will wipe the file on every iteration and leave only the last line written.
Cricket analogy: Starting a fresh scorecard for a new match versus adding the next over's runs to today's existing scorecard mirrors the difference between > overwriting a file and >> appending to it.
@echo off
REM Overwrite the file with a fresh header line
ECHO Report generated on %DATE% %TIME% > report.txt
REM Append additional lines without erasing the header
ECHO ----------------------- >> report.txt
ECHO Section 1: Summary >> report.txt
REM Write a variable's value to the file
SET count=42
ECHO Total items processed: %count% >> report.txtReading Files Line by Line
FOR /F "tokens=* delims=" %%A IN (input.txt) DO ECHO %%A reads input.txt one line at a time, assigning each full line to %%A; using tokens=* captures the entire line rather than splitting on spaces, and delims= (empty) disables the default delimiter so lines with spaces aren't broken apart. Inside a .bat file the loop variable needs double percent signs (%%A), while typed directly at the command prompt it uses a single percent sign (%A), which trips up many beginners copying examples from the web.
Cricket analogy: Reading through an entire over-by-over commentary transcript line by line to extract each ball's outcome mirrors FOR /F processing a file one line at a time.
Parsing Delimited Data
FOR /F's options control how each line is split: tokens=1,2 captures the first and second whitespace-separated fields into %%A and %%B, delims=, changes the delimiter to a comma for CSV-style parsing, skip=1 skips the first line (handy for skipping a CSV header row), and eol=; treats semicolons as end-of-line comment markers so anything after them on a line is ignored. For example, FOR /F "skip=1 tokens=1,2 delims=," %%A IN (data.csv) DO ECHO Name=%%A Age=%%B reads a CSV, skips its header, and splits each remaining line on commas.
Cricket analogy: Skipping the header row of a scorecard sheet and reading only the batter's name and runs columns mirrors FOR /F's skip=1 combined with tokens=1,2.
By default, FOR /F uses space and tab as delimiters and reads only the first token of each line into the loop variable, which surprises many beginners expecting the whole line. Always specify tokens=* delims= explicitly when you want each entire line, and specify tokens=1,2,... delims=, (or whatever separator applies) when you want specific delimited fields.
Setting and Using Variables from File Content
SET /P variable=<file.txt> reads a single line from file.txt directly into a variable without a loop, which is a quick way to grab a version number or config value stored on its own line. Combining FINDSTR with FOR /F is a common pattern for extracting a specific piece of data from a larger file, for example FOR /F "tokens=2 delims==" %%V IN ('FINDSTR /B "version=" config.txt') DO SET AppVersion=%%V finds the line starting with 'version=' and captures everything after the equals sign into AppVersion.
Cricket analogy: Pulling just tonight's required run-rate figure off a full scoreboard rather than reading everything on it mirrors SET /P grabbing one specific line into a variable.
- Batch has no native file-I/O statements; it repurposes redirection (>, >>) and FOR /F for reading and writing.
- > creates or completely overwrites a file, while >> appends to the end of an existing file.
- FOR /F "tokens=* delims=" reads each entire line of a file into a loop variable.
- In .bat files loop variables use double percent signs (%%A); at the command prompt use a single sign (%A).
- FOR /F's tokens, delims, skip, and eol options control how each line is split and which lines are processed.
- SET /P variable=<file.txt> reads a single line directly into a variable without a loop.
- Combining FINDSTR with FOR /F is a common pattern for extracting one specific value out of a larger file.
Practice what you learned
1. What is the effect of using > instead of >> inside a loop that writes a line on each iteration?
2. Which FOR /F option is needed to capture an entire line rather than just the first whitespace-delimited token?
3. Inside a .bat file, how should the FOR /F loop variable be written?
4. What does SET /P variable=<file.txt> do?
5. Which combination is the standard pattern for skipping a CSV header row and reading two comma-separated fields?
Was this page helpful?
You May Also Like
Batch File Redirection and Pipes
Understand stdin, stdout, and stderr redirection, piping commands together, and silencing output with NUL in Batch scripts.
File and Folder Operations
Learn how to navigate, create, and remove directories and inspect file attributes using core Windows Batch commands.
Copy, Move, and Delete Commands
Master COPY, XCOPY, MOVE, REN, and DEL to duplicate, relocate, and remove files and folders safely in Batch 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