Batch scripting, while often considered a simpler scripting language, provides robust mechanisms for file processing. A common task is reading a text file line by line and performing operations on each line. This tutorial will detail how to achieve this in Windows batch scripting.
Basic Line-by-Line Iteration
The core mechanism for iterating through a text file in batch scripting is the FOR /F
command. This command is specifically designed for parsing files. Here’s the basic syntax:
FOR /F "options" %%variable IN (filename) DO command
Let’s break down the components:
FOR /F
: Indicates a file parsing loop."options"
: A string containing various options to control parsing. We’ll cover these in detail shortly.%%variable
: This is a variable that will hold the current line of the file during each iteration of the loop. Important: Inside a batch script (a.bat
or.cmd
file), you must use double percent signs (%%
). If you’re running the command directly in the command prompt, use single percent signs (%
).(filename)
: The path to the text file you want to iterate through.DO command
: The command to execute for each line of the file.
The simplest form to read each line is:
FOR /F "tokens=* delims=" %%A IN (myfile.txt) DO ECHO %%A
This will print each line of myfile.txt
to the console. Let’s break down the options:
tokens=*
: This option specifies which "tokens" (words) from each line to extract. The asterisk (*
) means "all tokens" – effectively taking the entire line as a single token.delims=
: This option specifies the delimiters – characters used to separate tokens. Leavingdelims=
empty means that no characters are considered delimiters, and the entire line is treated as a single token. This is crucial for processing lines with spaces.
Handling Files with Spaces in Path Names
If your file path contains spaces, you need to either enclose the path in double quotes, or use the usebackq
option.
- Double Quotes:
FOR /F "tokens=* delims=" %%A IN ("My File.txt") DO ECHO %%A
- usebackq:
FOR /F "usebackq tokens=* delims=" %%A IN ("My File.txt") DO ECHO %%A
The usebackq
option allows you to use double quotes around the filename without the batch interpreter interpreting them as part of a string literal. It’s particularly helpful when the filename is constructed dynamically.
Advanced Options: Delimiters and Tokens
While tokens=*
is suitable for reading entire lines, you can use more specific tokens
and delims
options to extract specific parts of each line. For example:
FOR /F "tokens=1,2 delims=," %%A IN (data.csv) DO ECHO %%A, %%B
This example assumes data.csv
is a comma-separated value (CSV) file. It extracts the first two "tokens" (values) separated by commas and assigns them to the variables %%A
and %%B
. The DO
command then echoes these values.
Example: Processing a List of Files
Let’s say you have a file named mylist.txt
containing a list of executable files, one per line. You want to run each executable with an argument. Here’s how you can do it:
FOR /F "tokens=* delims=" %%A IN (mylist.txt) DO CALL %%A ARG1
This code iterates through each line of mylist.txt
, assigns the line (the executable filename) to %%A
, and then calls the executable with the argument ARG1
.
Changing Directories and Relative Paths
If your text file is located in the same directory as your batch script, you can improve portability by using %~dp0
. This variable represents the drive and path of the batch file itself.
cd /d %~dp0
FOR /F "tokens=* delims=" %%A IN (myfile.txt) DO ECHO %%A
The cd /d %~dp0
command changes the current directory to the directory containing the batch script. This ensures that the FOR
loop can find myfile.txt
regardless of where the script is executed from.
Important Considerations
- Variable Scope: Variables defined within a
FOR
loop (like%%A
) are typically local to the loop. - Error Handling: Batch scripting provides limited error handling capabilities. Consider adding checks to verify file existence and handle potential errors gracefully.
- Performance: For very large files, batch scripting may not be the most performant solution. Consider using more robust scripting languages like PowerShell or Python for complex file processing tasks.