Microsoft Excel

Ron de Bruin
Excel Automation

Microsoft MVP Program

Use VBA to Delete all files in folder

The following code deletes every file in a folder named Test in your Documents folder.
Note: You can use any path to a folder inside the FolderWithFiles string. You should also note the use of wildcard character (*) in the macro. You can use it to search for all files of a specific type: *.pdf only for PDF files or *.docx to delete only Microsoft Word 2007 or later files.

Note: The macro will not work in Mac Excel 2016

Sub DeleteFilesInFolder()
    Dim FolderWithFiles As String
    Dim scriptToRun As String

    FolderWithFiles = MacScript("return (path to documents folder) as string") & "Test:"
    
    ' Or enter the full path to the folder.
    ' FolderWithFiles = "Macintosh HD:Users:YourUserName:Desktop:Test:"

    If Right(FolderWithFiles, 1) <> ":" Then
        FolderWithFiles = FolderWithFiles & ":"
    End If
    
    scriptToRun = scriptToRun & "tell application " & Chr(34) & _
                  "Finder" & Chr(34) & Chr(13)
    scriptToRun = scriptToRun & _
               "do shell script ""rm "" & quoted form of posix path of " & _
               Chr(34) & FolderWithFiles & """ & " & Chr(34) & "*" & Chr(34) & Chr(13)
    scriptToRun = scriptToRun & "end tell"

    On Error Resume Next
    MacScript (scriptToRun)
    On Error GoTo 0
End Sub

On this page you find information for Excel for Windows
http://www.rondebruin.nl/win/s4/win004.htm