# Need Help With Vb6



## Learn2b1 (Aug 2, 2004)

Hi all,
I found this on the web and want to use it but I dont understand how to make the file1/file2 full paths?? Can someone help. 

It compares 2 files for being identical


Public Function AreTheyTheSame(ByVal File1 As String, _
ByVal File2 As String, Optional StringentCheck As _
Boolean = False) As Boolean
'**********************************************************
'PURPOSE: Check to see if two files are identical
'File1 and File2 = FullPaths of files to compare
'StringentCheck (optional): If false (default),
'will only compare file lengths. If true, a
'byte by byte comparison is conducted if file lengths are
'equal
'**********************************************************

On Error GoTo ErrorHandler

If Dir(File1) = "" Then Exit Function
If Dir(File2) = "" Then Exit Function

Dim lLen1 As Long, lLen2 As Long
Dim iFileNum1 As Integer
Dim iFileNum2 As Integer
Dim bytArr1() As Byte, bytArr2() As Byte
Dim lCtr As Long, lStart As Long
Dim bAns As Boolean

lLen1 = FileLen(File1)
lLen2 = FileLen(File2)
If lLen1 <> lLen2 Then
Exit Function
ElseIf StringentCheck = False Then
AreTheyTheSame = True
Exit Function
Else
iFileNum1 = FreeFile
Open File1 For Binary Access Read As #iFileNum1
iFileNum2 = FreeFile
Open File2 For Binary Access Read As #iFileNum2

'put contents of both into byte Array
bytArr1() = InputB(LOF(iFileNum1), #iFileNum1)
bytArr2() = InputB(LOF(iFileNum2), #iFileNum2)
lLen1 = UBound(bytArr1)
lStart = LBound(bytArr1)

bAns = True
For lCtr = lStart To lLen1
If bytArr1(lCtr) <> bytArr2(lCtr) Then
bAns = False
Exit For
End If

Next
AreTheyTheSame = bAns

End If

ErrorHandler:
If iFileNum1 > 0 Then Close #iFileNum1
If iFileNum2 > 0 Then Close #iFileNum2
End Function


----------



## AbvAvgUser (Oct 3, 2002)

Full paths mean "*C:\My Documents\Pictures\file01.doc*" and "*C:\My Documents\Pictures\file02.doc*".

Isn't it? I hope I am not underestimating your knowledge.


----------



## Learn2b1 (Aug 2, 2004)

AbvAvgUser
I understand that the full path needs to point to file. My problem is I dont know how to impliment it into this code segment...Thanks so much.
learn2b1


----------



## Mosaic1 (Aug 17, 2001)

Ok that code is going to return a true or false. Are the files the same? True or False? But what does that mean? It also includes a third argument to allow you to decide what the same means. 

For example: (add this to a command button click event )
MsgBox AreTheyTheSame("C:\windows\clouds.bmp", "C:\windows\bub.bmp", True)
This message will either pop up True or False

If the files are the same length note the true here, the function will do a Byte by Byte comparison to see if they are really the same or just the same length.


For example. I have two text files and they are the same length. If I choose False as then they might not be the same, but are the same length and will be judged to be the same. 

So using True would be more exact. This will compare the files' contents if the files are the same length. 

Create two text files which are exactly the same. Now change one letter in the first and compare them using 
MsgBox AreTheyTheSame("C:\text1.txt", "C:\text2.txt")

This is going to tell you they are the same. No need to add False as the third argument. False is the default. Unless you type True as the third argument this functoin just compares the length of each file and not its contents. 

If you do a closer comparison:
MsgBox AreTheyTheSame("C:\text1.txt", "C:\text2.txt",True)
This comparison will tell you they are not the same. 

This is comparing first the length, and then if that is equal, a comparison of the contents. 


Hope this helps.


----------



## Learn2b1 (Aug 2, 2004)

Thanks AbvAvgUser and Mosaic1..

Still have a problem appending to a 3rd file how ever.
Any suggestion how I can do this at the points indicated??

Also, I have tried to understand how modules work as I want to call different for different files I need to compare. The main code here is the template I just change the Sub name and call. But is there an easier way>> 

Here's what I have done:

I took the ByVal out for file1 and 2 also the dif(file1) lines.
Dim File1 As String
Dim File2 As String

File1 = "realfullpath"
File2 = "realfullpath"

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Public Function AreTheyTheSame(Optional StringentCheck As _
Boolean = False) As Boolean

On Error GoTo ErrorHandler

Dim File1 As String
Dim File2 As String
Dim lLen1 As Long, lLen2 As Long
Dim iFileNum1 As Integer
Dim iFileNum2 As Integer
Dim bytArr1() As Byte, bytArr2() As Byte
Dim lCtr As Long, lStart As Long
Dim bAns As Boolean

File1 = "C:\FileMaster.txt"
File2 = "C:\Filetocheck.txt"

lLen1 = FileLen(File1)
lLen2 = FileLen(File2)

If lLen1 <> lLen2 Then
Exit Function <Append
ElseIf StringentCheck = False Then
AreTheyTheSame = True <Append
Exit Function
Else
iFileNum1 = FreeFile
Open File1 For Binary Access Read As #iFileNum1
iFileNum2 = FreeFile
Open File2 For Binary Access Read As #iFileNum2

'put contents of both into byte Array
bytArr1() = InputB(LOF(iFileNum1), #iFileNum1)
bytArr2() = InputB(LOF(iFileNum2), #iFileNum2)
lLen1 = UBound(bytArr1)
lStart = LBound(bytArr1)

bAns = True
For lCtr = lStart To lLen1
If bytArr1(lCtr) <> bytArr2(lCtr) Then
bAns = False
Exit For
End If

Next
AreTheyTheSame = bAns
<Append
End If

ErrorHandler:
If iFileNum1 > 0 Then Close #iFileNum1
If iFileNum2 > 0 Then Close #iFileNum2
End Function


----------



## Mosaic1 (Aug 17, 2001)

What are you trying to accomplish exactly? Please don't post any more code yet. Just describe your project. Where are you getting these file names etc. Which are compared to which files? How are you sorting information and what do you want to do with that once you have it?


----------



## Learn2b1 (Aug 2, 2004)

Ok you asked for this.  

Firstly this is a personal thing I am trying to learn from and has a lot of things in it. I am learning this vb 6 by example and this is the best way for me. So here we go.

I used generic file names in my post as I thought the principle of the code was the same for any file. Sorry..

This is my project outline that I'm trying to write.

I have a program folder that has both program files and other folders in it.

I have another folder "Master Files" that contains all the correct data needed to compare the results of the creator program. 

This would be the paths.
c:\Master Files\ *.*
c:\program files\creator\Info\*.*

Inside this program folder (creator) there are many files and some folders that contain program output files.

These output files are in a folder named "Info". 

The folder (Info) I am working on is full of more different named folders each having it's on data.

For example: There is a folder named out.txt in each of the differently named folders.
and another named hrt.cfg in each of the folders. However there are other files with different names in different named folders too.

My program would first detect if creator was running. 
Then it would check to see what folder if any was being used by the creator.
If it found that creator was using folder "Info" it would then check to see which folder inside "Info" was being accessed. 
When it determined what folder ( let's say the meg named folder) was being accessed it would then start comparing the master files for "mig" which are template files and correct, with the newly made creator files that had been placed into the creator\info\mig folder.
If my program found an error with any of the newly made files it would append to a error.log file within the "mig folder) located in the master folder.

As the error logs are a appened to the actual error is displayed in my listbox that my program would have telling me what folder and file the error.occurred.

I have thought about trying to place all my master files into some sort of resource file or something for a smaller program size, or possible the names and paths that can be easily returned after my program detected what files were needed. 

Anyway, thats all it does. Simply compares files and appends to error logs in different folders. As far as sorting I know nothing about that.
Did'nt mean to cause any problems just asking some questions. 



I am in the middle staged of this and don't have all the code down yet. As I was running into problems I was asking a question on the forum.I wont place anymore code there.
Thanks for your help  
Learn2b1


----------



## Mosaic1 (Aug 17, 2001)

The code comes last. First you have to figure out what it is you want to do and then see if you can create an algorithm. The actual code is down the road. If you jump ahead you get confusion.

This looks like the heart of your particular question:



> When it determined what folder ( let's say the meg named folder) was being accessed it would then start comparing the master files for "mig" which are template files and correct, with the newly made creator files that had been placed into the creator\info\mig folder.


You have a group of templates. And one file created per template? How are these named? Or are you comparing all the files against this one template? This can be done using arrays. But first we have to pinpoint exactly what you need to do.

If you have one folder full of templates to compare to another folder of files how are you comparing? Are the files in folder two named a certain way so they can be identified to compare to the files in folder 1? 
C:\templates\myfile.txt to C:\output\myfile.txt
C:\templates\myfile2.txt to C:\output\myfile2.txt

---------


----------



## Learn2b1 (Aug 2, 2004)

Yes Mosaic
Your answer is correct. I dont know that much about res,mod,arrays yet so i'm writing each procedure out for each file processed. It the hard way but the only way I know at present. Sorry I can't express myself better..


----------

