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
Medium Priority
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 61

Expert Comment

by:Bill Prew
ID: 39613181
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

ID: 39614432
I would like to specify the full path to the folders that should not be deleted.

LVL 61

Accepted Solution

Bill Prew earned 2000 total points
ID: 39615714
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

ID: 39622756
Thanks very much!  This works exactly the way I need it to.
LVL 61

Expert Comment

by:Bill Prew
ID: 39622801
Great, glad that helped.


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

590 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