Finding and deleting duplicate files

  • Thread starter Thread starter General
  • Start date Start date
G

General

Hi,

I have a bunch of files in folder A, that get distributed to about 10
other folders. Each file gets placed in only a single folder. All that
is done via a Macro,

I am fine with the macros to sort the files and distribute to the
various folders. BUT, what I'd like to do is before a file from A gets
placed into the destination folder, I'd like to scan that destination
folder to see if any filenames have the same 15 characters as the file
currently in A. If I do find that, it means that it is a duplicate
file (note that for legacy reasons the first 15 chars will be the same,
but after that it could be different - even for duplicate files).

So if I find a match with the first 15 characters, then I want to
delete the smaller file and end up with the bigger file in the
destination folder.

I hope this is clear..

Thanks!

phil
 
Before saving in the new folder you could run a FileSearch:

Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = DestinationFolderPath
.Filename = Your15Characters & "*.xls"
' Below sets seach to only find files same size or smaller than current:
' You could also test by date using similar PropertyTest
.PropertyTests.Add Name:="Size", Condition:=msoConditionAtMost, _
Value:=FileLen(ThisWorkbook.FullName)
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
Kill .FoundFiles(i)
MsgBox "Deleted " & .FoundFiles(i)
Next i
End If
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top