For instance :-
For/F "delims =" % A in (input.txt) Do...
For/F "delims =" % A in ('Type input.txt ') Do...
For/F "delims =" % A in ('more ^ <input.txt ') Do...
However, only the last method (using the more command) will give consistent results into SS Windows NT, 2000, XP and 2003. the first method does not recognize Unicode files. also, the usebackq switch must be used if the input filename contains spaces. the second method, using the type command, also fails to recognize Unicode files on Windows 2000, XP and 2003 if the input file does not begin with a bit order mark (BOM ).
In all the examples, assume the contents of the file numbers.txt to be :-
One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Displaying the first line
This example prints one.
@ Echo off & setlocal enableextensions
Set "first ="
For/F "delims =" % A in ('more ^ <numbers.txt ') Do (
If not defined first set first = %
)
Echo/% first %
Displaying the first x lines
This example prints one, two and three.
@ Echo off & setlocal enableextensions
Set "lines = 3"
Set I =-1
Set "OK ="
For/F "delims =" % A in ('more ^ <numbers.txt ') Do (
Set/a I + = 1 & for/F % Z in ('echo/% I % ') Do (
If "% Z" = "% lines %" set OK = 1
)
If not defined OK echo/%
)
Displaying the last line
This example prints ten.
@ Echo off & setlocal enableextensions
For/F "delims =" % A in ('more ^ <numbers.txt ') do set "last = %"
Echo/% last %
Displaying the last x lines
This example prints nine and ten.
@ Echo off & setlocal enableextensions
Set "lines = 2"
For/F % A in ('Find/C/V "" ^ <numbers.txt ') do set/a skip = % A-Lines
For/F "delims =" % A in ('more/e + % skip % ^ <numbers.txt ') Do (
Echo/%
)
Displaying the nth line
This example prints three. note that instead of using the more Command's/e switch, the Skip option cocould have been used with the for/F command, however, this fails is it is set to any number less than one.
@ Echo off & setlocal enableextensions
Set lineno = 3
Set "line ="
Set/A lineno-= 1
For/F "delims =" % A in ('more/e + % lineno % ^ <numbers.txt ') Do (
If not defined line set "line = %"
)
Echo/% line %
Displaying the nth line plus X number of lines
This example prints five and six.
@ echo off & setlocal enableextensions
set start = 5
set "lines = 2"
set/A I =-1, start-= 1
set "OK ="
for/F "delims =" % A in ('more/e + % start % ^ set/a I + = 1 & for/F % Z in ('echo/% I % ') do (
If "% Z" = "% lines %" set OK = 1
)
if not defined OK echo/% A
)