Delete files in a folder VBA

PATSYS

Well-known Member
Joined
Mar 12, 2006
Messages
1,750
Hi all,

I would like to include a line in my code to delete all .pdf files found in C:\Documents

How is this done?

Thanks
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this:

Code:
Sub del_pdf2()
Kill "C:\Documents\*.pdf"
End Sub

Hope it helps.

Dom
 
Last edited:
Upvote 0
Code:
Sub testit()
    Dim myvar As Variant
    Dim myFolder As String
    Dim i As Long
    
    myFolder = "c:\test"
    myvar = FileList(myFolder, "*.pdf")
    For i = LBound(myvar) To UBound(myvar)
        Kill myFolder & myvar(i)
    Next
End Sub

Function FileList(fldr As String, Optional fltr As String = "*.*") As Variant
    Dim sTemp As String, sHldr As String
    If Right$(fldr, 1) <> "\" Then fldr = fldr & "\"
    sTemp = Dir(fldr & fltr)
    If sTemp = "" Then
        FileList = Split("No files found", "|")  'ensures an  array is returned
        Exit Function
    End If
    Do
        sHldr = Dir
        If sHldr = "" Then Exit Do
        sTemp = sTemp & "|" & sHldr
     Loop
    FileList = Split(sTemp, "|")
End Function

Office 2007 compliant , just change the test it code to your needs.
 
Upvote 0
Try this:

Code:
Sub del_pdf2()
Kill "C:\Documents\*.pdf"
End Sub

Hope it helps.

Dom

Hi,

I got a runtime error 53 "File not found" although I am sure the files are there.

Any idea why the code does not seem to locate the pdf files?
 
Upvote 0
Is this really c:\documents? Or the documents folder for the user? If so, its actual location depends on the OS. It could be c:\documents and settings\{user}\my documents -- XP -- or c:\users\{user}\documents -- Vista.

Of course, if you have a Mac, * is not a wildcard character in the Kill statement. See the Excel VBA help for more.

Hi,

I got a runtime error 53 "File not found" although I am sure the files are there.

Any idea why the code does not seem to locate the pdf files?
 
Upvote 0
I just tested it and it works fine in Excel 2003 for me, am guessing it would be okay in 2007 but not certain. Is that definitely the path that you have for the documents?

Dom
 
Upvote 0
Hi Tushar and Dom,

Sorry, there are actually no files, the folder is just not refreshing immediately, as it is really a folder in the network drive, I just used C:\Documents to simplify.

Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,020
Members
448,939
Latest member
Leon Leenders

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top