ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   need to delete files (https://www.excelbanter.com/excel-programming/421156-need-delete-files.html)

dan

need to delete files
 
Hi,
I need to delete some files in a specific folder.
Some files are resaved with brackects and I need to keep the file with the
highest number in the brackect and delete the one that existed before.

For example If I have the following files in folder "Myfolder"

Myfile1.xls
Myfile2.xls
Myfile3.xls
Myfile2(2).xls
Myfile3(2).xls
Myfile3(3).xls

I should end up with
Myfile1.xls
Myfile2(2).xls
Myfile3(3).xls


Any idea?
Dan

Bernie Deitrick

need to delete files
 
Dan,

Try the macro below on a COPY of your folder - just to make sure that it works the way you expect.

Put the macro into a standard codemodule of an otherwise blank workbook, and save that file the
folder with the files to delete - in this case, the copy of MyFolder. Then run the macro "FindKill".

HTH,
Bernie
MS Excel MVP


Sub FindKill()
Dim i As Integer
Dim j As Integer
Dim sName As String
Dim myFind As Integer
Dim myIndex As Integer
Dim sName2 As String
Dim myFind2 As Integer
Dim myIndex2 As Integer
Dim Killed() As Boolean


With Application.FileSearch
.NewSearch
.LookIn = ThisWorkbook.Path
.SearchSubFolders = False
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
MsgBox "There were " & .FoundFiles.Count & " file(s) found."
ReDim Killed(1 To .FoundFiles.Count)
For i = 1 To .FoundFiles.Count
Killed(i) = False
Next i
For i = 1 To .FoundFiles.Count - 1
myFind = InStr(1, .FoundFiles(i), "(")
If myFind = 0 Then
sName = Left(.FoundFiles(i), Len(.FoundFiles(i)) - 4)
myIndex = 0
Else
sName = Left(.FoundFiles(i), myFind - 1)
myIndex = Mid(.FoundFiles(i), myFind + 1, _
InStr(myFind, .FoundFiles(i), ")") - myFind - 1)
End If

For j = i + 1 To .FoundFiles.Count
myFind2 = InStr(1, .FoundFiles(j), "(")
If myFind2 = 0 Then
sName2 = Left(.FoundFiles(j), Len(.FoundFiles(j)) - 4)
myIndex2 = 0
Else
sName2 = Left(.FoundFiles(j), myFind2 - 1)
myIndex2 = Mid(.FoundFiles(j), myFind2 + 1, _
InStr(myFind2, .FoundFiles(j), ")") - myFind2 - 1)
End If

If sName = sName2 Then
If myIndex < myIndex2 Then
If Not Killed(i) Then
'Kill .FoundFiles(i)
Killed(i) = True
End If
Else
If Not Killed(j) Then
'Kill .FoundFiles(j)
Killed(j) = True
End If
End If
End If
Next j
Next i
Else
MsgBox "There were no files found."
End If

For i = 1 To .FoundFiles.Count
If Killed(i) Then Kill .FoundFiles(i)
Next i

End With

End Sub




"Dan" wrote in message
...
Hi,
I need to delete some files in a specific folder.
Some files are resaved with brackects and I need to keep the file with the
highest number in the brackect and delete the one that existed before.

For example If I have the following files in folder "Myfolder"

Myfile1.xls
Myfile2.xls
Myfile3.xls
Myfile2(2).xls
Myfile3(2).xls
Myfile3(3).xls

I should end up with
Myfile1.xls
Myfile2(2).xls
Myfile3(3).xls


Any idea?
Dan





All times are GMT +1. The time now is 01:20 PM.

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