CompareDirectories – Find different files in two directories

CompareDirectories – Find different files in two directories

Enum CompareDirectoryEnum    cdeSourceDirOnly = -2   ' file is present only in the source directory    cdeDestDirOnly = -1     ' file is present only in the dest directory    cdeEqual = 0            ' file is present in both directories,                            '  with same date, size, and attributes    cdeSourceIsNewer = 1    ' file in source dir is newer    cdeSourceIsOlder = 2    ' file in source dir is older    cdeDateDiffer = 3       ' date of files are different    cdeSizeDiffer = 4       ' size of files are different    cdeAttributesDiffer = 8 ' attributes of files are differentEnd Enum' Compare files in two directories'' returns a two-dimensional array of variants, where arr(0,'  n) is the name of the N-th file' and arr(1, n) is one of the CompareDirectoryEnum values'' NOTE: requires a reference to the Microsoft Scripting Runtime type library'' Usage example:'   ' compare the directories C:DOCS and C:BACKUPDOCS'   Dim arr() As Variant, index As Long'   arr = CompareDirectories("C:DOCS", "C:BACKUPDOCS")'   ' display files in C:DOCS that should be copied into the backup directory'   ' because they are newer or because they aren't there'   For index = 1 To UBound(arr, 2)'       If arr(1, index) = cdeSourceDirOnly Or arr(1, index) = cdeSourceIsNewer ' Then'           Print arr(0, index)'   NextFunction CompareDirectories(ByVal sourceDir As String, ByVal destDir As String) _    As Variant()    Dim fso As New Scripting.FileSystemObject    Dim sourceFld As Scripting.Folder    Dim destFld As Scripting.Folder    Dim sourceFile As Scripting.File    Dim destFile As Scripting.File    Dim col As New Collection    Dim index As Long    Dim FileName As String        ' get a reference to source and dest folders    Set sourceFld = fso.GetFolder(sourceDir)    Set destFld = fso.GetFolder(destDir)        ' ensure that destination path has a trailing backslash    If Right$(destDir, 1) <> "" Then destDir = destDir & ""        ' prepare result array - make it large enough    ' (we will shrink it later)    ReDim res(1, sourceFld.Files.Count + destFld.Files.Count) As Variant        ' we need to ignore errors, in case file doesn't exist in destination dir    On Error Resume Next        ' load files of source directory into result array    For Each sourceFile In sourceFld.Files        ' this is the name of the file        FileName = sourceFile.Name                ' add file name to array        index = index + 1        res(0, index) = FileName                ' add file name to collection (to be used later)        col.Add FileName, FileName                ' try to get a reference to destination file        Set destFile = fso.GetFile(destDir & FileName)                If Err Then            Err.Clear            ' file exists only in source directory            res(1, index) = cdeSourceDirOnly                    Else            ' if the file exists in both directories,            '  start assuming it's the same file            res(1, index) = cdeEqual                        ' compare file dates            Select Case DateDiff("s", sourceFile.DateLastModified, _                destFile.DateLastModified)                Case Is < 0                    ' source file is newer                    res(1, index) = cdeSourceIsNewer                Case Is > 0                    ' source file is newer                    res(1, index) = cdeSourceIsOlder            End Select                        ' compare attributes            If sourceFile.Attributes <> destFile.Attributes Then                res(1, index) = res(1, index) Or cdeAttributesDiffer            End If                        ' compare size            If sourceFile.Size <> destFile.Size Then                res(1, index) = res(1, index) Or cdeSizeDiffer            End If        End If    Next        ' now we only need to add all the files in destination directory    ' that don't appear in the source directory    For Each destFile In destFld.Files        ' it's faster to search in the collection        If col(destFile.Name) = "" Then            ' we get here only if the filename isn't in the collection            ' add the file to the result array            index = index + 1            res(0, index) = destFile.Name            ' remember this only appears in the destination directory            res(1, index) = cdeDestDirOnly        End If    Next        ' trim and return the result    If index > 0 Then        ReDim Preserve res(1, index) As Variant        CompareDirectories = res    End IfEnd Function

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing