VB Script or Batch File to Delete Files and Folder Except for Two Specified Folders That Should not be Deleted

Posted on 2013-10-30
Last Modified: 2013-11-04
I'm looking for a VB script or batch file that can delete all files and folders in a nested directory structure except for two specific folders.  An example is:


Delete all files and folders except for:


I would appreciate any advice on how to do this.

Question by:COV-Webmaster
  • 3
  • 2
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
So, for the folders not to be deleted, do you want to specify just the folder name, or the full path to that folder? So in you example, do you want to specify


or just



Author Comment

Comment Utility
I would like to specify the full path to the folders that should not be deleted.

LVL 51

Accepted Solution

Bill Prew earned 500 total points
Comment Utility
Okay, give this a try, adjust paths as needed in the top section.

' Define the folder to scan, and the age of files to delete
Const strBaseDir = "C:\test\nextlevel\nextlevel"

' Create a dictionary to hold the list of folders to skip
Set dicSkip = CreateObject("Scripting.Dictionary")
dicSkip.CompareMode = vbTextCompare

' Add the folders to the dictionary
dicSkip.Add "C:\test\nextlevel\nextlevel\dontdelete1", 0
dicSkip.Add "C:\test\nextlevel\nextlevel\dontdelete2", 0

' Set up filesystem object for usage
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Look for files
PurgeFolder objFSO.GetFolder(strBaseDir)

' Subroutine (recursive) to search for files to delete
Sub PurgeFolder(objFolder)
   ' Skip any folders we don't want to process
   If Not dicSkip.Exists(objFolder.Path) Then

       ' Look at all files in this folder
       For Each objFile In objFolder.Files
            ' WScript.StdOut.WriteLine "Deleted:" & objFile.Path

       ' Recursively drill down into subfolder
       For Each objSubFolder In objFolder.SubFolders
           PurgeFolder objSubFolder

       ' If folder is now empty, remove it
       If objFolder.Files.Count = 0 And objFolder.Subfolders.Count = 0 Then
          ' Wscript.Echo "Deleted folder: " & objFolder.Path
       End If

   End If
End Sub

Open in new window


Author Closing Comment

Comment Utility
Thanks very much!  This works exactly the way I need it to.
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Great, glad that helped.


Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This is about my first experience with programming Arduino.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now