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

Reading and Writing Text Files

Learn how Batch scripts write to files with redirection and read them back using FOR /F, SET /P, and FINDSTR.

File OperationsIntermediate10 min readJul 10, 2026
Analogies

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.

batch
@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.txt

Reading 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

Was this page helpful?

Topics covered

#Batch#WindowsBatchScriptingStudyNotes#MicrosoftTechnologies#ReadingAndWritingTextFiles#Reading#Writing#Text#Files#StudyNotes#SkillVeris