• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Custom delete files in a folder

Mr.Karr

Member
Hello

Can anyone please provide a code snippet or module to custom delete excel files which are saved in a folder.

In detail: I have an activity that consolidates files from a folder. Extracts file names. Now I need to delete specific excel files post found them unnecessary for consolidation. Once I have all those files deleted from folder, I use another macro to consolidate.

Below you can find the full file name. Is it possible to delete them one by one reading its file name? Please help.

UK 20150318103250.xlsx
UK 20150318092932.xlsx
UK 20150318075439.xlsx
UK 20150318075343.xlsx
UK 20150317142505.xlsx
UK 20150317130000.xlsx
UK 20150317125952.xlsx
UK 20150317125726.xlsx
UK 20150317122350.xlsx
UK 20150317115516.xlsx
UK 20150316133056.xlsx
UK 20150316091003.xlsx
UK 20150313162155.xlsx
UK 20150312194150.xlsx

Many thanks in advance
Karthik
 
If you have all the names, all you really need to do still is give the folder path. Here's the mock-up of the code you could use.
Code:
Sub KillFiles()
Dim myRange As Range
Dim c As Range
Dim fPath As String


'Where are the files located?
fPath = "C:\Users\MyDocumets"

'Where is the list of file names?
Set myRange = Range("A2:A10")

'Make sure there's a trailing slash mark
If Right(fPath, 1) <> "\" Then
    fPath = fPath & "\"
End If

'This is what will delete the files
For Each c In myRange
    Kill fPath & c.Name
Next

End Sub
 
My guess is that the error is due to file not being found. Verify that the folder path is correct. Next, check the file name. Is is spelled exactly right, including extension?
 
Pls change this line & check.

Code:
For Each c In myrange
    If Len(Dir$(fPATH & c)) > 0 Then Kill fPATH & c
Next
 
Back
Top