none
Exclude Subfolder from Batch Deletion - VBscript RRS feed

  • Question

  • Hello, a couple days ago I received some great help fixing a VB script that deletes any file older than 90 days and then goes through and deletes an empty directory. The original script runs without any issues, however now I have identified a single subfolder that needs to be excluded from this deletion and I am unable to get the script to function properly. All files and folders get deleted as expected however I can't figure out how to exclude this folder. I'm not the greatest at scripting and this is my first visualbasic script any assistance would be greatly appreciated. Here is the script I am currently using:

    DIM objFSO, oShell
    Set oShell = CreateObject("WScript.Shell")
    strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
    strPath = strHomeFolder & "\OneDrive - City of Bellevue"
    ExdPath = strPath & "\Shared With Everyone"          <------- Folder I want Excluded
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Call Search (strPath)
    WScript.Echo"Done."

    Sub Search(str)
    Dim objFolder, objSubFolder, objFile
    Set objFolder = objFSO.GetFolder(str)
    For Each objFile In objFolder.Files
    If objFile.DateLastModified < (Now() - 90) Then
    objFile.Delete(True)
    End If
    Next
    For Each objSubFolder In objFolder.SubFolders
    Search(objSubFolder.Path)
    ' Files have been deleted, now see if
    ' the folder is empty.
    If (objSubFolder.Files.Count = 0) Then
    objSubFolder.Delete True
    ElseIF (objFSO.Folder = ExdPath) Then     <----- Code I'm using to try to exclude the specified folder
       objFolder.Delete False 
    End If
    Next
    End Sub



    Kevin Craig A+ | Net+ | Sec+ | MSCA | CCNA

    Thursday, March 26, 2015 3:46 PM

Answers

  • This modification (untested) should do the trick. To make the code more robust I would make the comparison of the folder name case-insensitive. Note that indentation will greatly improve the readability of your code.

    Set oShell = CreateObject("WScript.Shell")
    strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
    strPath = strHomeFolder & "\OneDrive - City of Bellevue"
    ExdPath = strPath & "\Shared With Everyone"  <------- Folder I want Excluded
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Search (strPath)
    WScript.Echo"Done."

    Sub Search(str)
        Set objFolder = objFSO.GetFolder(str)
        For Each objFile In objFolder.Files
            If objFile.DateLastModified < (Now() - 90) Then objFile.Delete(True)
        Next
        
        For Each objSubFolder In objFolder.SubFolders
            If objSubFolder.Path = ExdPath Then Exit Sub
            Search(objSubFolder.Path)
            If (objSubFolder.Files.Count = 0) Then objSubFolder.Delete True
        Next
    End Sub


    • Edited by Frederik Long Thursday, March 26, 2015 5:01 PM
    • Marked as answer by Intheory87 Thursday, March 26, 2015 8:30 PM
    Thursday, March 26, 2015 5:00 PM
  • All you need is an "and" clause in this statement so that does not take effect if the folder's name matches your exclusion.

    If objSubFolder.Files.Count = 0 and . . . Then objSubFolder.Delete True

    • Marked as answer by Intheory87 Thursday, March 26, 2015 8:30 PM
    Thursday, March 26, 2015 7:01 PM

All replies

  • This modification (untested) should do the trick. To make the code more robust I would make the comparison of the folder name case-insensitive. Note that indentation will greatly improve the readability of your code.

    Set oShell = CreateObject("WScript.Shell")
    strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
    strPath = strHomeFolder & "\OneDrive - City of Bellevue"
    ExdPath = strPath & "\Shared With Everyone"  <------- Folder I want Excluded
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Search (strPath)
    WScript.Echo"Done."

    Sub Search(str)
        Set objFolder = objFSO.GetFolder(str)
        For Each objFile In objFolder.Files
            If objFile.DateLastModified < (Now() - 90) Then objFile.Delete(True)
        Next
        
        For Each objSubFolder In objFolder.SubFolders
            If objSubFolder.Path = ExdPath Then Exit Sub
            Search(objSubFolder.Path)
            If (objSubFolder.Files.Count = 0) Then objSubFolder.Delete True
        Next
    End Sub


    • Edited by Frederik Long Thursday, March 26, 2015 5:01 PM
    • Marked as answer by Intheory87 Thursday, March 26, 2015 8:30 PM
    Thursday, March 26, 2015 5:00 PM
  • Thank you Fredrick, your suggestion worked great in excluding the subfolder I needed. However the files older than 90 days within this sub-folder still exist.  I don't think I was very clear in my initial report but I still need the files older than 90-days to be deleted in this subfolder, I just don't want the subfolder to delete once the folder becomes empty. Case-sensitivity shouldn't be an issue with these directories as they are automatically created on the client computer when the user connects Onedrive to their PC. 



    Kevin Craig A+ | Net+ | Sec+ | MSCA | CCNA

    Thursday, March 26, 2015 6:33 PM
  • All you need is an "and" clause in this statement so that does not take effect if the folder's name matches your exclusion.

    If objSubFolder.Files.Count = 0 and . . . Then objSubFolder.Delete True

    • Marked as answer by Intheory87 Thursday, March 26, 2015 8:30 PM
    Thursday, March 26, 2015 7:01 PM
  • Thank You Frederik, ended up using a not-equals to get the results I wanted. Final script is a follows:

    Set oShell = CreateObject("WScript.Shell")
    strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
    strPath = strHomeFolder & "\OneDrive - City of Bellevue"
    ExdPath = strPath & "\Shared with Everyone"
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Search (strPath)
    WScript.Echo"Done."

    Sub Search(str)
        Set objFolder = objFSO.GetFolder(str)
        For Each objFile In objFolder.Files
            If objFile.DateLastModified < (Now() - 90) Then objFile.Delete(True)
        Next
        
        For Each objSubFolder In objFolder.SubFolders
            Search(objSubFolder.Path)
    ' Files have been deleted, now see if
    ' the folder is empty.
    ' Exclude "Shared with Everyone" sub-folder deletion
            If objSubFolder.Path <> ExdPath and (objSubFolder.Files.Count = 0) Then objSubFolder.Delete True
        Next
    End Sub



    Kevin Craig A+ | Net+ | Sec+ | MSCA | CCNA


    • Edited by Intheory87 Thursday, March 26, 2015 8:29 PM
    Thursday, March 26, 2015 8:29 PM
  • Thank You Frederik, ended up using a not-equals to get the results I wanted.


    This is exactly what you needed to do. Thanks for the feedback.
    Thursday, March 26, 2015 9:17 PM