ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Finding and deleting duplicate files (https://www.excelbanter.com/excel-programming/320752-finding-deleting-duplicate-files.html)

General

Finding and deleting duplicate files
 
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


K Dales[_2_]

Finding and deleting duplicate files
 
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


"General" wrote:

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




All times are GMT +1. The time now is 12:11 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com