PDA

View Full Version : Delete files from a list in excel



radam
06-14-2011, 03:59 AM
I have a list of path names (column A) and file names (column B) looking for a macro that will delete all files on this list.

PathFile NameC:\Users\john.doe\Desktopa.pdfC:\Users\john.doe\Desktopb.xlsmC:\Users\j ohn.doe\Desktop\other stuffc.xlsxC:\Users\john.doe\Desktop\other stuffa.xlsxC:\Users\john.doe\Desktop\other stuffb.xlsx

Looking for a macro that will delete all files in this list where the number of files will vary. If the files are in use the macro should skip that particular file and continue to delete the rest.

Kenneth Hobs
06-14-2011, 06:17 AM
Welcome to the forum!
Sub DeleteFiles()
Dim c As Range, f As String
For Each c In Range("A2", Range("A" & Rows.Count).End(xlUp))
f = c.Value2 & c.Offset(0, 1).Value2
If IsFileWriteable(f) Then Kill f
Next c
End Sub

'RichardSchollar, http://www.ozgrid.com/forum/showthread.php?t=79132
Function IsFileWriteable(StrFilePath As String) As Boolean
Dim FileNum As Integer
IsFileWriteable = False
FileNum = FreeFile
On Error Resume Next
Open StrFilePath For Input Lock Read Write As #FileNum ' Open file and lock it.
If Err.Number = 0 Then IsFileWriteable = True 'Can write to file
Close FileNum
End Function