Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need to read first n lines from a .txt file using ms dos batch file. The file looks like:
abc
efge
123
...Anybody knows how to write the batch file? Thanks.

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 echo.%%j ) :RONE

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" :(

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))+"]") $contentScript 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 $contentThe documentation for these commands is at http://www.biterscripting.com/helpp... .
Sen

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |