Hi all,
I have the following wee script which deletes pdf files in a folder.
Is there a way i can customize so that it check all subfolders within folder?
Thanks in advance!!Code:Function DeletePDFS() Dim fso As Object, file As Object, folder As Object, subfolder As Object, s As String Set fso = CreateObject("Scripting.FileSystemObject") Set folder = fso.GetFolder("C:\Documents and Settings\c.connor\My Documents\deletePDFS\filesToDelete\") For Each file In folder.Files fso.DeleteFile "C:\Documents and Settings\c.connor\My Documents\deletePDFS\filesToDelete\*.pdf" Next file End Function
C
Last edited by cjconnor24; 05-12-2009 at 12:00 PM. Reason: pasted wrong macro
The Folders object has a SubFolders property.
Thanks Stephen,
Any idea how i would work that into my macro?
Does your code work as is? Why is it a function? Try this:
Code:Sub DeletePDFS() Dim FSO As Object, file As Object, folder As Object, SubFolder As Object, s As String, FolderFile As Object Set FSO = CreateObject("Scripting.FileSystemObject") Set folder = FSO.GetFolder("H:\VBA test\") For Each file In folder.Files FSO.DeleteFile folder.Path & "\*.xls" Next file For Each SubFolder In folder.subfolders For Each FolderFile In SubFolder.Files FSO.DeleteFile FolderFile.parentfolder & "\*.xls" Next FolderFile Next SubFolder End Sub
Thanks for you help Stephen but that doesn't work either.
It will delete the files in
But doesn't touch any files of subfolders within there.Code:Set folder = FSO.GetFolder("C:\Documents and Settings\c.connor\My Documents\deletePDFS\filesToDelete\")
Thanks again
Try adding first and last lines below:
Code:On Error Resume Next FSO.DeleteFile folder.Path & "\*.xls" On Error GoTo 0
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks