Batch Script Reading from Files
Reading from Files
Reading files in a batch script is done by using the for
loop command to step through each line defined in the file to be read.
note
Since there is no direct command to read text from a file into a variable, it is necessary to use the for
loop for this purpose.
Example
Let's see an example:
@echo off
FOR /F "tokens=* delims=" %%x in (new-file.txt) DO echo %%x
note
The delims
parameter is used to split the file text into several tokens or words. Each word or token is then stored in the x
variable. For each word read from the file, an echo
is executed to print the word in the console output.
If the file new-file.txt
is not present in C:\
, it will be created with the previous command.