Solved

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
5
431 Views
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:

C:\test\nextlevel\nextlevel\

Delete all files and folders except for:

C::\test\nextlevel\nextlevel\dontdelete1
C::\test\nextlevel\nextlevel\dontdelete2

I would appreciate any advice on how to do this.

Thanks.
0
Comment
Question by:COV-Webmaster
  • 3
  • 2
5 Comments
 
LVL 53

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

C:\test\nextlevel\nextlevel\dontdelete1
C:\test\nextlevel\nextlevel\dontdelete2

or just

dontdelete1
dontdelete2

~bp
0
 

Author Comment

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

Thanks.
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 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
            objFile.Delete
            ' WScript.StdOut.WriteLine "Deleted:" & objFile.Path
       Next

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

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

   End If
End Sub

Open in new window

~bp
0
 

Author Closing Comment

by:COV-Webmaster
ID: 39622756
Thanks very much!  This works exactly the way I need it to.
0
 
LVL 53

Expert Comment

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

~bp
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …
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…

816 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

8 Experts available now in Live!

Get 1:1 Help Now