Dos - Read input from file

By xngo on August 11, 2019

Reading simple input from file

REM Create an input file with data.
ECHO A > filename_test.txt
ECHO B >> filename_test.txt

REM Read input file and display each line.
for /f %%L in (filename_test.txt) do (
    ECHO I found %%L
)

Reading input from file with a delimiter

REM Create an input file with data.
ECHO A,> filename_test.txt
ECHO B C, B>> filename_test.txt
ECHO IP, C, B>> filename_test.txt

REM Read input file and display data of each line up to the 1st occurrence of the delimiter.
REM  In this case, the delimiter is a comma(,).
for /f "delims=," %%L in (filename_test.txt) do (
    ECHO I found %%L
)

Read the complete line from input file

REM Create an input file with data.
ECHO A,> filename_test.txt
ECHO B C, B>> filename_test.txt
ECHO IP, C, B>> filename_test.txt

REM Read and display complete line from input file. 
REM  I define the delimiter to nothing. Therefore, it will read until the end of line of each line in the input file.
for /f "delims=" %%L in (filename_test.txt) do (
    ECHO I found %%L
)

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.