PDA

Click to See Complete Forum and Search --> : How can I count how many rows inside a .csv file?


craigheartley
Apr 19th, 2006, 01:35 PM
Hello,

I need help. How can I count how many rows inside a .csv file? Also is there a easy way to compare a .csv file and have it output the results to a file?

Thanks!

DKenny
Apr 19th, 2006, 02:14 PM
You can achieve this by using a FileSystemObject object. Here's a sample function that will return the line count for a file. You will need to add areference to the Microsoft Scripting Runtime library in order for this to run.
Function Count_Lines(FilePath As String) As Long
Dim fsoFile As Scripting.FileSystemObject
Dim fsStream As Scripting.TextStream

'Create a new File system Object
Set fsoFile = New Scripting.FileSystemObject

'Open the file for reading
Set fsStream = fsoFile.OpenTextFile(Filename:=FilePath, IOMode:=ForReading)

'Move to the bottom of the file
Do Until fsStream.AtEndOfStream
fsStream.SkipLine
Loop

'Return the current line number,
'Which will equal the count as
'we are at the end of the file
Count_Lines = fsStream.Line

'Clear Object Variables
Set fsStream = Nothing
Set fsoFile = Nothing
End Function

craigheartley
Apr 20th, 2006, 06:34 AM
Hey thanks for the reply. I'll see if this does the trick. 1 last question... how can I compare 2 excel files..? Do I compare row by row?

DKenny
Apr 20th, 2006, 09:13 AM
That depends, what do you want to compare?

craigheartley
Apr 20th, 2006, 09:50 AM
OK.

Heres the deal... I dont have good VB and I'm trying to write a utility that takes 2 excel files (.CSV) and compare them to make sure they are the same. And then write the diffs out to a txt file as a report. Noone here has VB skills and I have very little... PLEASE HELP. Thanks!

VBAhack
Apr 20th, 2006, 04:29 PM
OK.

Heres the deal... I dont have good VB and I'm trying to write a utility that takes 2 excel files (.CSV) and compare them to make sure they are the same. And then write the diffs out to a txt file as a report. Noone here has VB skills and I have very little... PLEASE HELP. Thanks!

Since .csv is just a text file, one option is to read each file into byte arrays and then compare the bytes in a loop. Also, seems to me there used to be a DOS function to do binary compare of two files.............

EMtixis
Apr 21st, 2006, 09:48 AM
A DOS functgion wich is called FC
Type help FC for more info