Link to home
Start Free TrialLog in
Avatar of charast
charastFlag for United States of America

asked on

VB script to Delete folders with a name that contains a date

Object:
I need a script that will allow me to remove folders older than 1 year according to the date string in the NAME of the folder (not the actual date of the folder). The script needs to be run daily and I need to be able to remove the folder and any sub-folders/files under it.

overview:
I have some logs that need to be retained for one year.  They are contained in folders. These folders have been moved to a location on the network in one batch move (they all now have the same creation date). The folders have a naming structure as follows:  APP09_01012008.


I have scripts that will allow me to delete folders according to the date using the creation date time stamp but I cant figure how to use a date format in a folder name string and increment it.

Thanks!
Charast
ASKER CERTIFIED SOLUTION
Avatar of Shift-3
Shift-3
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Shift-3 beat me to it, but since I wrote it, here's what I came up with.

You would just need to change the values of the objStartFolder an dthe intDaysOld variables.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\LogFiles"
intDaysOld = 365
 
Set objFolder = objFSO.GetFolder(objStartFolder)
 
Set colFiles = objfolder.Files
For Each subfld In objFolder.SubFolders
	FolderName = subfld.Name
	FolderDate = Right(FolderName,8)
	DateMonth = Left(FolderDate,2)
	DateDay = Mid(FolderDate,3,2)
	DateYear = Right(FolderDate,4)
	FormattedDate = DateMonth & "/" & DateDay & "/" & DateYear
 
	If DateDiff("d", FormattedDate, Now) > intDaysOld Then
		'WScript.Echo "Older"
		objfso.DeleteFolder objStartFolder & "\" & FolderName,True
	Else
		'WScript.Echo "Newer"		
	End If
Next

Open in new window

Avatar of charast

ASKER

script worked perfectly and even allowed use of a UNC path.