Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 81
Default number of lines and characters in a text file

Is there a way to get the number of lines and characters in a text file
without opening it in Excel or via reading each line until EOF is reached?

Bob


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default number of lines and characters in a text file

There's LOF for characters (or FileLen if closed) but I don't know a direct
way to get the line count. Unless the file is very large I wouldn't think
using EOF would take so long but have a go with this -

Sub test()
Dim cntLen As Long, cntChars As Long, cntLines As Long
Dim sFile As String
Dim FF As Integer

sFile = "C:\temp\test.txt"
cntLen = FileLen(sFile)

FF = FreeFile
Open sFile For Input As #FF
Seek FF, 1
cntChars = LOF(FF)
cntLines = UBound(Split(Input(cntChars, FF), vbCrLf)) + 1
Close #FF

' cntChars will include returns
Debug.Print cntLines, cntChars, cntLen
End Sub

Regards,
Peter T


"Bob Flanagan" wrote in message
...
Is there a way to get the number of lines and characters in a text file
without opening it in Excel or via reading each line until EOF is reached?

Bob




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default number of lines and characters in a text file

Peter T wrote:
There's LOF for characters (or FileLen if closed) but I don't know a direct
way to get the line count. Unless the file is very large I wouldn't think
using EOF would take so long but have a go with this -

Sub test()
Dim cntLen As Long, cntChars As Long, cntLines As Long
Dim sFile As String
Dim FF As Integer

sFile = "C:\temp\test.txt"
cntLen = FileLen(sFile)

FF = FreeFile
Open sFile For Input As #FF
Seek FF, 1
cntChars = LOF(FF)
cntLines = UBound(Split(Input(cntChars, FF), vbCrLf)) + 1
Close #FF

' cntChars will include returns
Debug.Print cntLines, cntChars, cntLen
End Sub

Regards,
Peter T


"Bob Flanagan" wrote in message
...
Is there a way to get the number of lines and characters in a text file
without opening it in Excel or via reading each line until EOF is reached?

Bob


This approach uses the scripting FileSystemObject, instead ...

Const ForAppending = 8
Dim sFile, nLines

sFile = "D:\Someplace\Filename.txt"
with CreateObject("Scripting.FileSystemObject")
nLines = .OpenTextFile(sFile, ForAppending).Line
nChrs = .GetFile(sFile).Size
end with

This approach doesn't actually count the number of characters (new
characters omitted), but rather the file's size. This, BTW, is not the
size of the file on the disk, since that is in increments of the disk's
'cluster block size'.

And, I am told some unusual file conditions (empty file and files of
blank lines only) can yield inaccurate results for the line count (off
by one line).

Tom Lavedas
===========
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to increase number of characters of file name in excel Manish Excel Discussion (Misc queries) 2 November 10th 08 10:24 AM
Counting the number of specified text characters within a cell FlipperT Excel Worksheet Functions 3 December 1st 06 07:50 PM
How can I increase the number of text characters in a cell Preadno Excel Discussion (Misc queries) 2 November 6th 06 05:25 PM
number of lines of wrapped text Fred[_22_] Excel Programming 2 May 19th 05 01:37 PM
excel97 vba to append lines to text file overwriting last 2 lines Paul Excel Programming 1 November 6th 04 08:11 PM


All times are GMT +1. The time now is 10:17 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"