Paypal Donate

Delete folders matching given name within entire folder structure

File
del.vbs (7 views)

This script will go through the entire folder structure from the top level folder given, and delete any folders that match the given folder name (which can include wildcards, e.g. "temp*" ). Due to the destructive nature of the script, the example only runs in reporting mode (it creates a log of the folders it would delete, but doesn't actually do it).  Just change the Report Only parameter to carry out the actions.

The script contains two procedures, one of which being a helper procedure (which is called recursively to walk the directory tree). Simply change the example line at the beginning of the script to serve your purposes.

Syntax:

SearchAndDeleteFoldersBySpec strRootFolder, strMatch, blnReportOnly

strRootFolder: (string) the top level folder from which to search (and delete) .
strMatch: (string) the name of the folders to be deleted (wildcards * and ? are allowed)
blnReportOnly: (boolean) if true, only the log is created.  Set to false to delete folders.

' Script written by David Barrett
' Copyright 2009
' http://www.cedit.biz/
'
This script is licensed under the Creative Commons
' Attribution 2.5 Licence
' http://creativecommons.org/licenses/by/2.5/
'

' You are free to use it for both personal and
' commercial purposes, so long as full attribution
' is given to the author (David Barrett).
'
' This notice must not be removed
'
'

' Delete folder contents, but run in report only mode (will only show what would be deleted)
' Change true to false to actually delete the files

SearchAndDeleteFoldersBySpec "E:\test", "temp*", true

sub DeleteFoldersBySpec(strFolder, strSpec, blnReportOnly)
    dim objShell, objFSO, objF
    dim arrFolders
    dim objFile, objSubFolder
    
    Set objShell = CreateObject("WScript.Shell")
    set objFSO=CreateObject("Scripting.FileSystemObject")

    ' Get folders matching spec
    strCommand = "cmd /C dir """ & strFolder & "\" & strSpec & """ /AD /B"
    strResults=""
    Set objExecObject = objShell.Exec(strCommand)
    Do
        WScript.Sleep 100
    Loop Until objExecObject.Status 0
    strResults = objExecObject.StdOut.ReadAll()
    set objExecObject=Nothing
    
    ' Now process folder list
    on error resume next ' Need to ignore any errors for this bit
    arrFolders=Split(strResults, vbCrLf)
    for i=0 to UBound(arrFolders)
        if arrFolders(i)"" then ' Needed as blank line created by split statement above
            set objF=objFSO.GetFolder(strFolder & "\" & arrFolders(i))
            ' Now delete the folder
            wscript.echo "Deleting folder: " & strFolder & "\" & objF.Name
            if not blnReportOnly then objF.Delete true
            set objF=Nothing
        end if
        Err.Clear
    next
    set objFSO=Nothing
end sub

sub SearchAndDeleteFoldersBySpec(strRootFolder, strSpec, blnReportOnly)
    ' Recursively search for folders matching the given spec and delete them
    dim objFSO, objF, objSubF
    ' Process this folder first to ensure we don't needlessly process subfolders that would be deleted
    DeleteFoldersBySpec strRootFolder, strSpec, blnReportOnly
    
    set objFSO=CreateObject("Scripting.FileSystemObject")
    set objF=objFSO.GetFolder(strRootFolder)
    for each objSubF in objF.SubFolders
        SearchAndDeleteFoldersBySpec strRootFolder & "\" & objSubF.Name, strSpec, blnReportOnly
    next
end sub
Copyright © 2009 www.cedit.biz. All rights reserved.