compare files in 2 folders

enerks

New Member
Joined
Sep 20, 2006
Messages
13
I like to compare two folders. Folder2 contains the backupfiles and are used as basis. In folder1 the (new) files will be added. Before I can start updating I need to check if folder2 = folder1. And I need to list which files are missing.

So basicaly: Compare 2 folders and list the missing files.

Can someone help me out?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
enerks,
Are you referring to just filenames? or contents of the files?

Cal
 
Upvote 0
enerks,
Here's some code you will need to revise for your needs. If will check the contents of the backupfolder vs the prodfolder and the prodfolder vs the backup and message box the results.

Code:
Private Sub CommandButton1_Click()
Dim FSO As Scripting.FileSystemObject, f As Scripting.File, Path As String
Dim BackupFiles() As String, ProdFiles() As String, index As Integer
Dim count1 As Integer, count2 As Integer, ProdFolder As String, BackupFolder As String

BackupFolder = "C:\"
ProdFolder = "C:\test"


Set FSO = CreateObject("Scripting.FileSystemObject")

ReDim BackupFiles(FSO.GetFolder(BackupFolder).Files.Count - 1, 1)
index = 0
'Get list of backup files
For Each f In FSO.GetFolder(BackupFolder).Files
    BackupFiles(index, 0) = f.Name
    index = index + 1
Next f

'Get list of Production files
index = 0

ReDim ProdFiles(FSO.GetFolder(ProdFolder).Files.Count - 1, 1)
For Each f In FSO.GetFolder(ProdFolder).Files
    ProdFiles(index, 0) = f.Name
    index = index + 1
Next f

'compare lists backupfolder to prodfolder
For count1 = 0 To UBound(BackupFiles)
    For count2 = 0 To UBound(ProdFiles)
        If BackupFiles(count1, 0) = ProdFiles(count2, 0) Then
            BackupFiles(count1, 1) = "Found"
        End If
    Next count2
Next count1

'compare lists prodfolder to backupfolder
For count1 = 0 To UBound(ProdFiles)
    For count2 = 0 To UBound(BackupFiles)
        If ProdFiles(count1, 0) = BackupFiles(count2, 0) Then
            ProdFiles(count1, 1) = "Found"
        End If
    Next count2
Next count1


'Output results
For count1 = 0 To UBound(BackupFiles, 1)
    If BackupFiles(count1, 1) = "" Then
        MsgBox "BackupFolder:" & BackupFiles(count1, 0) & " Not Found in ProdFolder"
    Else
        MsgBox "Backup Folder:" & BackupFiles(count1, 0) & " Found in Prodfolder"
    End If
Next count1

For count1 = 0 To UBound(ProdFiles, 1)
    If ProdFiles(count1, 1) = "" Then
        MsgBox "ProdFolder:" & ProdFiles(count1, 0) & " Not Found in backupFolder"
    Else
        MsgBox "ProdFolder:" & ProdFiles(count1, 0) & " Found in backupfolder"
    End If
Next count1

End Sub

You can condition the file check to look for "" in the 2nd element of the array and set a varilable based on finding anything missing.

HTH
Cal
 
Upvote 0
Hi Cal,

This is great. For now I don't have your skills yet. I did a excel vba training last week and am still learning. I would never have come up with this. My first project is ready now and I can use you're code in several other projects.

I made some changes for my needs for the rest it´s perfect!

10x a million!

Kind regards,

Rene
 
Upvote 0

Forum statistics

Threads
1,214,814
Messages
6,121,711
Members
449,049
Latest member
THMarana

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