Computing.Net > Forums > Windows XP > Read first n lines with MS-DOS bat file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Read first n lines with MS-DOS bat file

Reply to Message Icon

Name: petal
Date: September 16, 2009 at 02:30:19 Pacific
OS: Windows XP
CPU/Ram: Intel Core2 Quad CPU
Product: Dell Inspiron 11z notebook - customizable
Subcategory: Software Problems
Tags: MS Dos
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Ewen
Date: September 16, 2009 at 02:49:28 Pacific
Reply:

Post this in the Programming Forum

http://ewen.uuuq.com/


0

Response Number 2
Name: IVO
Date: September 16, 2009 at 02:50:26 Pacific
Reply:

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


0

Response Number 3
Name: Ewen
Date: September 16, 2009 at 17:30:01 Pacific
Reply:

What is the value of having a Programming Forum?

http://ewen.uuuq.com/


0

Response Number 4
Name: petal
Date: September 16, 2009 at 20:17:26 Pacific
Reply:

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


0

Response Number 5
Name: Sen Hu
Date: October 2, 2009 at 07:28:22 Pacific
Reply:

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 http://www.biterscripting.com/helpp... .

Sen


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Read first n lines with MS-DOS bat file

Cannot Open MS-DOS Batch File www.computing.net/answers/windows-xp/cannot-open-msdos-batch-file/112694.html

Problem with MS-DOS www.computing.net/answers/windows-xp/problem-with-msdos/58441.html

Dos .bat on xp need help www.computing.net/answers/windows-xp/dos-bat-on-xp-need-help/80418.html