Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 29th, 2007, 10:25 AM
Authorized User
 
Join Date: Dec 2006
Location: , , .
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default find number of lines in .txt file

hello,
I have a file C:\test\test.txt that I'd like to, using a Function where you pass in the Directory and Filename that you want to utilize, in this case C:\test and test.txt, calculate the number of lines in the text file and return that value from the Function.
Could someone share with me the code that would do this?

ex. if test.txt looked like

lskdjflskjf
lksdflksdj
slkdjf
lksjdf

the Function would return the value 4.

thanks in advance,
david


Reply With Quote
  #2 (permalink)  
Old January 29th, 2007, 09:48 PM
Registered User
 
Join Date: Jan 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

anyone solve this problem yet. it would be nice to know

Reply With Quote
  #3 (permalink)  
Old January 30th, 2007, 07:33 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,190
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

can you just open the file and read line by line???

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
Reply With Quote
  #4 (permalink)  
Old February 2nd, 2007, 02:09 PM
Registered User
 
Join Date: Feb 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

David, do you want to count physical lines (i.e. a long line that wraps counts as two lines) or logical lines (i.e. a long line that wraps counts as one line)?

If you want to count physical lines, try this:

Code:
Private Function CountLines() As Integer
    Dim myLines() As String

    myLines = Split(Text1.Text, vbCrLf)
    CountLines = UBound(myLines) + 1
End Function
Reply With Quote
  #5 (permalink)  
Old February 2nd, 2007, 02:17 PM
Authorized User
 
Join Date: Dec 2006
Location: , , .
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

BBcasey,

thanks for the code, it looks helpful......now what would the code be to open the Text1 file from
the hard drive to then use in the code you supplied?

thanks in advance,
david

Reply With Quote
  #6 (permalink)  
Old February 5th, 2007, 04:50 PM
Authorized User
 
Join Date: Dec 2006
Location: , , .
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

can anyone help me with my question?

thanks,
david

Reply With Quote
  #7 (permalink)  
Old February 5th, 2007, 05:10 PM
Authorized User
 
Join Date: Dec 2006
Location: , , .
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

just found some help on another VB forum....

Private Function LineCount(FileName As String) As Long
Open FileName For Input As #1
Do While Not EOF(1)
I = I + 1
Line Input #1, D
Loop
Close #1
LineCount = I
End Function

Private Sub Command1_Click()
MsgBox LineCount(Environ$("WinDir") & "\Win.INI")
End Sub


Reply With Quote
  #8 (permalink)  
Old February 29th, 2012, 09:38 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Talking Very Easy way to find number of lines in a text file

Open text file. Click on Edit>Go To. Give a huge number than expected number of lines. say if you expect to have 2000 lines, give 5000 or just give 200000. It gives an eror "The Line number is beyond the total number of lines" and shows up the the immediately available line. If you have 2379 lines it gives 2380. So whatever it shows up minus 1 is the actual number of lines in the text file. Cheers!!
Reply With Quote
  #9 (permalink)  
Old July 16th, 2014, 05:11 AM
Registered User
Points: 6, Level: 1
Points: 6, Level: 1 Points: 6, Level: 1 Points: 6, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default find number of lines in .txt file

I think TextFinding.com may help you! It is a useful tool to find multiple text in all types of files.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList and number of lines snufse1 ASP.NET 2.0 Basics 0 September 19th, 2008 02:52 PM
Need to Count number of lines in a file lawsoncobol Access VBA 2 July 19th, 2007 04:28 AM
Count the number of output lines joyces XSLT 7 July 6th, 2005 11:49 AM
Paged Invoices - Dynamic Number Detail Lines Marga XSLT 1 May 10th, 2005 09:28 AM



All times are GMT -4. The time now is 01:24 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.