1.

Solve : To count the no.of lines in a text file?

Answer»

Hi,

I have many source text files and I would LIKE to count the no. of lines in each text file. My output text file should contain the source text file name and the count of rows in each source file.

The operating system is 'Microsoft Windows XP'....

Plz help in doing this..

Regards,
Moorthy.Try this
find /v /c "" filename

---------- filename: 166

however it will not count lines that are truly blank, so PUT some long RANDOM string into the quotes that you know will not appear in the file

GrahamThanks for your suggestion!! But is there any other possible way to do this? Since, in this case some of the blank lines MAY be omitted in counting.

Regards,
Moorthy.Hi,

I got one more command for doing this.

wc

For getting line count in a file,
wc -l

For getting word count in a file,
wc -w

For getting character count in a file,
wc -c

Note that the character count includes garbage values...

Regards,
Moorthy.It might be easier to use a script where you can count line feeds/carriage returns.

Code: [Select]Const ForReading = 1

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("[highlight]c:\scripts[/highlight]")
Set fc = f.Files

For Each fs In fc
If UCase(fso.GetExtensionName(fs)) = "TXT" Then
Set objFile = fso.OpenTextFile(fs, ForReading)
strText = objFile.ReadAll
objFile.Close

arrWords = Split(strText, vbCrLf)
Wscript.Echo fs & " " & Ubound(arrWords) + 1
End If
Next

After saving the script with a VBS extension, run as cscript scriptname.vbs

Change the highlighted text for your local machine. Script presumes all the TXT files are in the same directory.here's a Python script you can use in Windows as well as Unix.

Code: [Select]f = open("test.txt").readlines()
PRINT "Number of lines in test.txt is %d" % len(f)



Discussion

No Comment Found