Monday, November 4, 2013

How to delete files with Visual Basic (VBA)

dim folderName as String
folderName = "\\Server Name\Share Name\Some Folder\Old Files\"    

On Error Resume Next
        Kill folderName & "*.*" 
On Error GoTo 0


To delete a specific file type, replace the second "*" wildcard with the file extension of the files you want to delete:

On Error Resume Next
        Kill folderName & "*.xlsx" 
On Error GoTo 0

Be careful with the Kill function as you can inadvertently delete valuable files. Make sure you specify the folder from which you want to delete files. And it's best to specify the file type or the file name itself. Be as specific as possible with the Kill command.

No comments:

Post a Comment