Batch File Example to Read Text File Line by Line

Batch File Example to Read Text File Line by Line

  • Blog
  • 1 min read

The following is an example of the Windows batch file to read a text file line by line.

Windows Batch File Example: Read Text File

The below batch program will read the text file (dummy.txt) from the current directory and will put the line into a variable, then will echo (print) the line on the screen.

Contents of the dummy.txt file.

This is the first line.
This is the second line.
This is the third line.
This is the forth line.
This is the fifth line.

readfile.bat

@echo off
for /f "tokens=*" %%s in (dummy.txt) do (
  echo %%s
)
pause

Test

To test create and put both the files into the same directory. Then run the file by typing its name at the Windows command prompt.

readfile.bat

Output

This is the first line.
This is the second line.
This is the third line.
This is the forth line.
This is the fifth line.
Press any key to continue . . .

See also:

  • How To Get Short Directory Name In Windows (8.3 File/Directory Name Format)

This Post Has One Comment

  1. D. GEORGE

    I have several folders listing show name and episode numbers. The show names are different lengths so I can not use TOKENS as a locator. The episode description is always 6 characters ie S01E01. I would like to run a batch which would check each folder for the sequence of episodes to see if any were missed or double entry of episode

Comments are closed.