+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    07-06-2007
    Posts
    70

    Question Deleting Files Using FSO

    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?

    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
    Thanks in advance!!
    C
    Last edited by cjconnor24; 05-12-2009 at 12:00 PM. Reason: pasted wrong macro

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,714

    Re: Deleting Files Using FSO

    The Folders object has a SubFolders property.

  3. #3
    Registered User
    Join Date
    07-06-2007
    Posts
    70

    Re: Deleting Files Using FSO

    Thanks Stephen,

    Any idea how i would work that into my macro?

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,714

    Re: Deleting Files Using FSO

    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

  5. #5
    Registered User
    Join Date
    07-06-2007
    Posts
    70

    Re: Deleting Files Using FSO

    Thanks for you help Stephen but that doesn't work either.

    It will delete the files in
    Code:
    Set folder = FSO.GetFolder("C:\Documents and Settings\c.connor\My Documents\deletePDFS\filesToDelete\")
    But doesn't touch any files of subfolders within there.

    Thanks again

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,714

    Re: Deleting Files Using FSO

    Try adding first and last lines below:
    Code:
    On Error Resume Next
        FSO.DeleteFile folder.Path & "\*.xls"
        On Error GoTo 0

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0