ACShell
ACShell

Reputation: 11

Find duplicate files selected in FileDialog

I am creating a function that finds for duplicate files selected with FileDialog, this is what I did:

Since the Collection FileDialogSelectedItems is read-only I search for a function to get a hash and converted the collection to an Array

Dim FilesArray(), ArraySHA(), FilteredArray() as String 
set fdg = Application.FileDialog(3)

With fdg
.....
If .show = -1 then
   ReDim ArraySHA(fdg.SelectedItems.Count)
   For i = 1 to fdg.SelectedItems.Count
      ArraySHA(i) = FileToSHA256(fdg.SelectedItems.Item(i)) '' New array with hashes
   Next i

Then I used another function to filter duplicates in the ArraySHA

   FilteredArray = FilterWords(ArraySHA)

Now I have an array with the unique hashes, but I need to have the selectedItems (filepaths) so I can import them without duplicates.

Thank you very much


Edit: I created a dictionary based on the SelectedItems Collection, adding each item only if it is not already there

    For i = 1 To fdg.SelectedItems.Count
        SHA = FileToSHA256(fdg.SelectedItems.Item(i))
        If Not dict.Exists(SHA) Then
                dict.Add SHA, fdg.SelectedItems.Item(i)
        End if
    Next i

Now I have the hashes that correspond to only one file, which is its key value

   For Each key In dict.keys
        UniqueValue = dict(key)
        '
        'do something with each unique value (which is the filepath)
        '
   Next key

Upvotes: 0

Views: 97

Answers (1)

ACShell
ACShell

Reputation: 11

I created a dictionary based on the SelectedItems Collection, adding each item only if it is not already there

    For i = 1 To fdg.SelectedItems.Count
        SHA = FileToSHA256(fdg.SelectedItems.Item(i))
        If Not dict.Exists(SHA) Then
                dict.Add SHA, fdg.SelectedItems.Item(i)
        End if
    Next i

Now I have the hashes that correspond to only one file, which is its key value

   For Each key In dict.keys
        UniqueValue = dict(key)
        '
        'do something with each unique value (which is the filepath)
        '
   Next key

Upvotes: 1

Related Questions