Counting number of Lines in text file

Is there any way to count the number of lines in a text file?

Page 1 of 1

10 Replies - 68357 Views - Last Post: 27 July 2010 - 09:38 AM Rate Topic: -----

#1 demonzrulaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-September 06

Counting number of Lines in text file

Posted 03 December 2006 - 04:00 PM

Is there any way to get the number of lines in a text file because the text file I need changes and I need to store every line into an array. I can't redimension the array if I don't know the number of lines in the file

Open "GRADES.TXT" For Input As #1 ' Opens file for input
	ReDim grades(1 To numGrades) As Integer ' Redims array to hold the grades
	Dim i As Integer ' dims counter variable
	For i = 1 To numGrades
		Input #1, grades(i)
	Next i
	Close #1 ' Closes file



Is This A Good Question/Topic? 0
  • +

Replies To: Counting number of Lines in text file

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Counting number of Lines in text file

Posted 03 December 2006 - 05:45 PM

http://www.vb-helper...file_lines.html

Another method is to place the contents of the file into a string variable, and then count them number of new line characters.
Was This Post Helpful? 0
  • +
  • -

#3 A S H  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-December 06

Re: Counting number of Lines in text file

Posted 08 December 2006 - 10:23 PM

I think Amadeus gave u the answer
Was This Post Helpful? 0
  • +
  • -

#4 pathuerter  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 3
  • Joined: 31-January 09

Re: Counting number of Lines in text file

Posted 31 January 2009 - 06:45 PM

Ok I know this thread was started eons ago, but I feel this is an easier way to solve this issue

 
Set FSO = CreateObject("Scripting.FileSystemObject")

Set file = 
	FSO.OpenTextFile("C:\Griffin\Stewie\sexy_party.txt", 1)
	 file.ReadAll
	 
	 'subtract one ReadAll stops at the first blank line
	 msgbox (file.line)-1



Was This Post Helpful? 3

#5 tension  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-January 09

Re: Counting number of Lines in text file

Posted 31 January 2009 - 11:35 PM

View Postpathuerter, on 31 Jan, 2009 - 05:45 PM, said:

Ok I know this thread was started eons ago, but I feel this is an easier way to solve this issue

 
Set FSO = CreateObject("Scripting.FileSystemObject")

Set file = 
	FSO.OpenTextFile("C:\Griffin\Stewie\sexy_party.txt", 1)
	 file.ReadAll
	 
	 'subtract one ReadAll stops at the first blank line
	 msgbox (file.line)-1




This is better, thanks! And sex_party.txt...? xD
Was This Post Helpful? 0
  • +
  • -

#6 pathuerter  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 3
  • Joined: 31-January 09

Re: Counting number of Lines in text file

Posted 01 February 2009 - 01:50 PM

On Family Guy, Stewie at random times says...you know what this is? A good time for a sexy party and about 8 women in lingerie run out in the family room of the Griffin household and they just kind of run around.

a feeble attempt at humor!
Was This Post Helpful? 0
  • +
  • -

#7 emmerson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-February 09

Re: Counting number of Lines in text file

Posted 01 February 2009 - 02:57 PM

Am I right that redim doesn't lose any data while you are increasing the size of the array? You could continue reading line-by-line then every fifty lines or so redim your array bigger again.

ReDim grades(1 To numGrades) As Integer ' Redims array to hold the grades

	Dim i As Integer ' dims counter variable
   
	 do
		 Input #1, grades(i)
		 i=i+1
		 if i=numgrades then 
			  numgrades=numgrades+50
			  redim grades (1 to numgrades) 
			  end if
		 loop until eof #1
	
	numgrades=i

	Close #1 ' Closes file



#2 loop through the file without storing the data just to count
#3 (my favorite) store the number of grades as the first line of the file
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Nestnie*


Reputation:

Re: Counting number of Lines in text file

Posted 13 July 2010 - 01:45 AM

Thanks. it helps me a lot.. Thank you very much :bigsmile:
Was This Post Helpful? 0

#9 Fib  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 161
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Re: Counting number of Lines in text file

Posted 13 July 2010 - 12:34 PM

View Postemmerson, on 01 February 2009 - 02:57 PM, said:

Am I right that redim doesn't lose any data while you are increasing the size of the array?


Yes, but you have to use the keyword Preserve with it in order for it to retain it's data.

I like to do it like this:
ReDim Preserve grades(UBound(grades) + 1)



That will add one more element to the array while retaining all of it's original data
Was This Post Helpful? 0
  • +
  • -

#10 gen009  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 15-May 09

Re: Counting number of Lines in text file

Posted 26 July 2010 - 11:35 PM

you can use "vbcrlf" for your comparison. it is equivalent to "\n" in java. ^_^
Was This Post Helpful? 0
  • +
  • -

#11 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Counting number of Lines in text file

Posted 27 July 2010 - 09:38 AM

You'll have a better understanding of File handling in VB6 if you read my tutorial on Sequential File Handling in VB6 hosted right here on dream.in.code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1