Solved

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

Posted on 2009-04-03
3
511 Views
Last Modified: 2013-11-10
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
0
Comment
Question by:charast
3 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
ID: 24063658
Paste the script below into a text file with a .vbs extension.  Customize the value of the strRoot variable on line 1 with the location of the folder containing the folders to be checked.  

Running the script will echo the names of the folders to be deleted.  Once you have tested it successfully and are certain it is finding the right folders, remove the apostrophe from line 15 to execute the delete commands.


strRoot = "c:\files"

intDays = 365
 

On Error Resume Next
 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objRoot = objFSO.GetFolder(strRoot)
 

For Each objFolder in objRoot.SubFolders

    dtmDate = Mid(objFolder.Name, 7, 2) & "/" & _

        Mid(objFolder.Name, 9, 2) & "/" & Mid(objFolder.Name, 11, 4)

    

    If DateDiff("d", dtmDate, Date) > intDays Then

        WScript.Echo objFolder.Path

        'objFolder.Delete True

    End If

Next

Open in new window

0
 
LVL 17

Expert Comment

by:Jared Luker
ID: 24063837
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

0
 

Author Closing Comment

by:charast
ID: 31566384
script worked perfectly and even allowed use of a UNC path.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
base64 decode encode 12 100
If not exist vbs error 4 28
Have information from one page show up on another 24 20
recursion example 16 70
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 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

11 Experts available now in Live!

Get 1:1 Help Now