By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,745 Members | 1,897 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,745 IT Pros & Developers. It's quick & easy.

vb6.0

chandru8
100+
P: 145
hi all,
is it possible to count the number of lines in a flat file by using v b6.0
Sep 20 '07 #1
Share this Question
Share on Google+
6 Replies


QVeen72
Expert 100+
P: 1,445
Hi,

here are many ways to do it.
First open the File using "Open" statemnet, Increment a counter by looping thru the file till u reach EOF.
OR
Use File System Object's "ReadAll" Method,
Readall it into a Variable, USe "Split" with vbCrLF (NewLine) and populate an Array. UBound of the Array, will give u number of lines..

REgards
Veena
Sep 20 '07 #2

chandru8
100+
P: 145
Hi veena,
thanks for your reply. i tried with the first method as you said its working fine but its taking a time lot
can you please give the code for the second one

thanks
Sep 20 '07 #3

QVeen72
Expert 100+
P: 1,445
Hi,

Add this reference to ur project :
"Microsoft Scripting Run Time"
Check this code:

Expand|Select|Wrap|Line Numbers
  1.     '
  2.     Dim FSO As New Scripting.FileSystemObject
  3.     Dim TArr
  4.     Dim fsStr As TextStream
  5.     '
  6.     Set fsStr = FSO.OpenTextFile("C:\MyText.txt", ForReading)
  7.     TArr = Split(fsStr.ReadAll, vbCrLf)
  8.     MsgBox "Number Of Lines : " & UBound(TArr)
  9.     fsStr.Close
  10.     Set FSO = Nothing
  11.     '
  12.  
Code not tested , u can reifne it..

Regards
Veena
Sep 20 '07 #4

chandru8
100+
P: 145
hi veena,
simply superb working fine
is there any explanations on this code.
thank you very very much for the timely help
Sep 20 '07 #5

QVeen72
Expert 100+
P: 1,445
Hi,

Thnx,

Set fsStr = FSO.OpenTextFile("C:\MyText.txt", ForReading)
Opens TextFile

TArr = Split(fsStr.ReadAll, vbCrLf)

ReadAll : Gets/Reads All the Contents of the File , Split is VB Function to Split
the Contents when it encounters a Chararcter. here vbCrLf (NewLine)
TArr is declared as a Variant (as we are not sure abt number of lines), When we split, it is converted to Array, will all the data.
To access Contents of the text Line by line, u can Loop thru TArr

Regards
Veena
Sep 20 '07 #6

chandru8
100+
P: 145
hi,
thanks a lot for you explanation
Sep 20 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.