vba check two files are identical

MetLife

Active Member
Joined
Jul 2, 2012
Messages
343
Office Version
  1. 365
Hi,

Is there a way to check if two text files are identical using VBA?

c:\file1.txt
c:\file2.txt

?
 
You can use something like:

VBA Code:
Dim FirstFile As String, SecondFile As String
Open "c:\file1.txt" For Binary As #1
FirstFile = Space(LOF(1))
Get 1, , FirstFile
Open "c:\file2.txt" For Binary As #2
SecondFile = Space(LOF(2))
Get 2, , SecondFile
If FirstFile = SecondFile Then
    MsgBox "Files are the same."
Else: MsgBox "Files are NOT the same."
End If
Close

Hope I understand what you want.

Regards,
GB
 
Upvote 0
Solution
You can use something like:

VBA Code:
Dim FirstFile As String, SecondFile As String
Open "c:\file1.txt" For Binary As #1
FirstFile = Space(LOF(1))
Get 1, , FirstFile
Open "c:\file2.txt" For Binary As #2
SecondFile = Space(LOF(2))
Get 2, , SecondFile
If FirstFile = SecondFile Then
    MsgBox "Files are the same."
Else: MsgBox "Files are NOT the same."
End If
Close

Hope I understand what you want.

Regards,
GB
Thanks!

That works. Do you know what the function
Space does to check?
 
Upvote 0

Forum statistics

Threads
1,226,831
Messages
6,193,206
Members
453,779
Latest member
C_Rules

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top