Jump to content

Add features to this script


Guest Msmith06

Recommended Posts

Guest Msmith06

Hello,I would like to add two features to this cleanup script.1) fix the script, so that it does not delete folders, that are past the set date, because as of now it will delete both folders and files, I'd like to make this an option.2) write to a log file all files and folders if I can get question # 1 fixed. so with this feature will log all files and folder that have been deleted.3) if anyone has a idea that will make this script even better, i would like to know of any suggestions.thanks,Mike'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''DESCRIPTION: This script will clean up any files that have a last modified ' date greater than or equal to the number specified. 'CREATED BY: Brian Plexico - microISV.com'CREATE DATE: 07/19/2005''INSTRUCTIONS: 1. Enter the path to the directory you'd like to clean on line 28.' Make sure to only change the text that currently reads:' "C:\DIRECTORY_TO_CLEAN". The path must be surrounded by quotes.' 2. Enter the number of days that must have passed for a file to be deleted' on line 38. The default is 30 but can be changed to any number.''LEGAL MUMBO JUMBO: -THIS SCRIPT HAS THE POTENTIAL TO BE VERY DAMAGING TO YOUR COMPUTER. ' -USE THIS SCRIPT AT YOUR OWN RISK.' -I'M NOT RESPONSIBLE FOR ANY BAD EFFECTS OF THIS SCRIPT SUCH AS DIZZINESS,' NOSE BLEEDS, OR A COMPLETELY DESTROYED OPERATING SYSTEM. ' -IF YOU HAVE DOUBTS AS TO WHAT YOU'RE DOING, THEN DON'T DO IT. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Option ExplicitOn Error Resume NextDim fso, PathToClean, numberOfDays, folder, rootFolder, objFolder, objSubfolders, objFiles, folderToClean, folderToCheckSet fso = CreateObject("Scripting.FileSystemObject")'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''ENTER THE PATH THAT CONTAINS THE FILES YOU WANT TO CLEAN UP'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Path to the root directory that you're cleaning upPathToClean = "C:\Gina\junkdrawer"'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''ENTER THE NUMBER OF DAYS SINCE THE FILE WAS LAST MODIFIED''ANY FILE WITH A DATE LAST MODIFIED THAT IS GREATER OR EQUAL TO 'THIS NUMBER WILL BE DELETED.'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Specify the how many days old a file must be in order to be deleted.numberOfDays = 7'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'Check to make sure path is not a drive rootIf Right(PathToClean, 2) = ":\" or Right(PathToClean, 1) = ":" Then msgbox "Whoa Nelly! Its best not to run this on a drive root.", vbOkOnly, "Don't Do That!"End If'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Start at the folder specified and walk down the directory tree''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Set rootFolder = fso.GetFolder(PathToClean)If Err.Number > 0 Then msgbox "It appears that you have not entered a valid directory path. Please correct the path and run the script again.", vbOkOnly, "Path Not Found" Wscript.QuitEnd IfGetSubfolders(rootFolder)CleanupFiles(rootFolder)'Let person know when the cleanup is completeMsgBox "The directory has been cleaned up!", vbOkOnly, "Cleanup Complete"'Clean upSet fso = NothingWscript.Quit''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Sub GetSubfolders(folder) Dim oSubfolder Set objFolder = fso.GetFolder(folder) Set objSubfolders = objFolder.Subfolders Set objFiles = objFolder.Files For Each oSubfolder in objSubfolders 'Recursively go down the directory tree GetSubfolders(oSubfolder.Path) 'Cleanup any files that meet the criteria CleanupFiles(oSubfolder.Path) 'Delete the folder if its empty CleanupFolder(oSubfolder.Path) NextEnd SubSub CleanupFiles(folderToClean) dim objFile set objFolder = fso.GetFolder(folderToClean) set objSubfolders = objFolder.SubFolders set objFiles = objFolder.Files For Each objFile in objFiles If DateDiff("d", objFile.DateLastModified, Now) > numberOfDays Then objFile.Delete End If Next Set objFolder = Nothing Set objSubfolders = Nothing Set objFiles = NothingEnd SubSub CleanupFolder(folderToCheck) Set objFolder = fso.GetFolder(folderToCheck) Set objSubfolders = objFolder.Subfolders Set objFiles = objFolder.Files If objFiles.Count = 0 and objSubfolders.Count = 0 Then objFolder.Delete End If Set objFolder = Nothing Set objSubfolders = Nothing Set objFiles = NothingEnd Sub

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...