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?
 

Some videos you may like

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Cbrine

Well-known Member
Joined
Dec 2, 2003
Messages
3,196
enerks,
Are you referring to just filenames? or contents of the files?

Cal
 

enerks

New Member
Joined
Sep 20, 2006
Messages
13
Hi Cbrine,

Thanks for your prompt reply.

I'm referring just to filenames.

10x

Rene
 

Cbrine

Well-known Member
Joined
Dec 2, 2003
Messages
3,196
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
 

enerks

New Member
Joined
Sep 20, 2006
Messages
13
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
 

Watch MrExcel Video

Forum statistics

Threads
1,113,850
Messages
5,544,650
Members
410,627
Latest member
georgealice
Top