Read first n lines with MS-DOS bat file

Dell Inspiron 11z notebook - customizabl...
September 16, 2009 at 02:30:19
Specs: Windows XP, Intel Core2 Quad CPU
I need to read first n lines from a .txt file using ms dos batch file. The file looks like:

Anybody knows how to write the batch file? Thanks.

See More: Read first n lines with MS-DOS bat file

Report •

September 16, 2009 at 02:49:28
Post this in the Programming Forum

Report •

September 16, 2009 at 02:50:26
Replace N with the number of lines to be accessed. By the way this NT batch NOT DOS.

@echo off & setlocal EnableDelayedExpansion
set max=N
set cnt=0
for /F "delims=" %%j in (text.txt) do (
  set /A cnt+=1
  if !cnt! grt %max% goto :DONE

Report •

September 16, 2009 at 17:30:01
What is the value of having a Programming Forum?

Report •

Related Solutions

September 16, 2009 at 20:17:26
Thanks IVO. Actually I tried the similar script with ms-dos batch file. But it doesn't work because cnt value is not updated until out of the for loop. Thát's, when executing "if %cnt% grt %max% goto :DONE", cnt is always "0" :(

Report •

October 2, 2009 at 07:28:22
To list first n lines of a text file, use the following script.

# Script listn.txt
var str file
var int n

# Read file into a variable.
var str content ; cat $file > $content

# List first $n lines to screen.
lex (makestr(int($n))+"]") $content

Script is in biterscripting. Save the script as C:/Scripts/listn.txt. Run the script as

script "C:/Scripts/listn.txt" file("C:/somefolder/somefile.txt") n(2)

Above will list the first 2 lines of file C:/somefolder/somefile.txt. Always include files and paths in double quotes. Similarly, the following command will give you the number of lines present in the file.

len $content

The documentation for these commands is at .


Report •

Ask Question