Computing.Net > Forums > Programming > Batch - Getting the number of lines

Batch - Getting the number of lines

Reply to Message Icon

Original Message
Name: Tom Schroeder
Date: September 10, 2007 at 23:09:25 Pacific
Subject: Batch - Getting the number of lines
OS: windows
CPU/Ram: -
Model/Manufacturer: -
Comment:

The question is how do I get the number of lines in a txt file useing a batch command(or just the number of the last line, which is the same)?

My problem is that I`m talking about files that are 20MB big, so I prefer not to scan all the "\n" in the file, or something like that.
I thought of maybe, is there a way of getting the Whole last line(with its line number) and from that getting just the line number.


Report Offensive Message For Removal


Response Number 1
Name: Razor2.3
Date: September 11, 2007 at 00:55:16 Pacific
Reply: (edit)

*NIX has the wc command, but that's no help to us. Instead, we must use for to walk though the file, counting as we go.
SET count=0
FOR /F "usebackq delims=" %a IN (yourFile.txt) DO SET /A count+=1
SET count

Oh, that'll also skip completely blank lines. Such is the nature of FOR.


Report Offensive Follow Up For Removal

Response Number 2
Name: IVO
Date: September 11, 2007 at 03:10:41 Pacific
Reply: (edit)

If you can set up a string that *absolutely* is never stored in the documents to be scanned, you can try

Find /V /C "nowhere string" your_file.txt

where "nowhere string" may be e.g. "$@$@".


Report Offensive Follow Up For Removal

Response Number 3
Name: ghostdog
Date: September 11, 2007 at 04:04:54 Pacific
Reply: (edit)

here's a vbscript alternative:
[code]
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
myFile = objArgs(0)
Set objFile = objFSO.OpenTextFile(myFile,1)
Do Until objFile.AtEndOfLine
line = objFile.Line
objFile.ReadLine
Loop
WScript.Echo "Line count of", myFile , "is", line
[/code]
usage:
cscript myscript myfile


Report Offensive Follow Up For Removal

Response Number 4
Name: Razor2.3
Date: September 11, 2007 at 04:42:37 Pacific
Reply: (edit)

ghost, wouldn't objFile.ReadAll work better here, or am I just too tired to think straight?


Report Offensive Follow Up For Removal

Response Number 5
Name: wizard-fred
Date: September 11, 2007 at 04:49:09 Pacific
Reply: (edit)

What is a "whole" last line?
If the last line is not terminated with a CR, is it considered a line?

I think you have to count the newline characters. There is no way without sequentially scanning the whole file, except if the file consists of known same length lines.

There are line count utilities available. Just GOOGLE.


Report Offensive Follow Up For Removal


Response Number 6
Name: ghostdog
Date: September 11, 2007 at 07:34:37 Pacific
Reply: (edit)

raz, thanks for the reminder.
[code]
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
myFile = objArgs(0)
Set objFile = objFSO.OpenTextFile(myFile,1)
objFile.ReadAll
line = objFile.Line
WScript.Echo "Line count of", myFile , "is", line
[/code]


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch - Getting the number of lines

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge