Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
dan dan is offline
external usenet poster
 
Posts: 866
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default 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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot Delete Archived TMP Files Bob English[_2_] Excel Discussion (Misc queries) 0 March 3rd 09 08:13 PM
How to delete archived files Bob English[_2_] Excel Discussion (Misc queries) 1 January 29th 09 03:42 AM
Delete all files except... David Johnston[_2_] Excel Programming 1 April 24th 07 06:35 PM
Delete files with VBA BEEJAY Excel Programming 1 May 12th 06 07:01 PM
Delete All Files in a folder Michael Smith Excel Programming 1 June 8th 05 05:32 PM


All times are GMT +1. The time now is 03:17 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"