Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

deleting files older than x amount of days

Posted on 2013-05-28
7
Medium Priority
?
357 Views
Last Modified: 2013-06-12
Hi guys,

I am looking to have a script that deletes files after x amount of days?
0
Comment
Question by:jonathanduane2010
7 Comments
 
LVL 5

Expert Comment

by:djbaum
ID: 39200555
0
 
LVL 11

Assisted Solution

by:John Easton
John Easton earned 340 total points
ID: 39200559
I have a script which is set to run every weekend which does what you are looking for.  Please see the attached file.

You need to specify the folder and e-mail server details (as it sends a summary of what it has deleted).

Hope this helps.
del30day.vbs
0
 
LVL 42

Assisted Solution

by:Meir Rivkin
Meir Rivkin earned 680 total points
ID: 39200564
delete_old_files.log is the log file which is appended upon every run of the script, lists all files that were deleted.
here:
Dim fso, f, f1, fc
strDir = "c:\temp\delete"
strDays = 7
 
Set fso = CreateObject("Scripting.FileSystemObject")
set objLog = fso.CreateTextFile("c:\temp\delete_old_files.log",8)

Set f = fso.GetFolder(strDir)
Set fc = f.Files
objLog.WriteLine("Script run date: " & Date)
objLog.WriteLine("Deleted files:")
For Each f1 in fc
	  If DateDiff("d", f1.DateLastModified, Date) > strDays Then
			objLog.WriteLine(f1.name & ", DateCreated" & f1.DateLastModified)
			fso.DeleteFile(f1)
	  End If
Next
objLog.WriteLine("")
objLog.close

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 39200571
save it as delete_old_files.vbs and add it as task scheduler using the following command:
SCHTASKS /Create /SC daily /TN DeleteOldfiles /ST 00:00 /TR "cscript c:\scripts\delete_old_files.vbs

Open in new window

0
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 340 total points
ID: 39200965
If you can use a third party (free) utility there is a very useful one here:

http://www.horstmuc.de/win/delage.htm
http://www.horstmuc.de/wbat32.htm

If it must be VBS then here is a script I created a while back that does this.  You can configure it to specify the age of the files to remove, if it should look in subfolders as well, and if you want to remove any empty folders after the files have been deleted.  In addition you can specify which file date to use to decide to delete (created, modified, accessed).  This was a solution to this question: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_27188057.html#a36165857

' Set processing mode flags
Const ProcessSubFolders = True
Const RemoveEmptyFolders = False
Const DateToUse = "C"    ' C=created, M=modified, A=accessed

' Create file system object
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

' Remove old files as needed
RemoveOldFiles objFSO.GetFolder("d:\", 30)
RemoveOldFiles objFSO.GetFolder("e:\", 30)
RemoveOldFiles objFSO.GetFolder("f:\", 30)

Sub RemoveOldFiles(objFolder, intDays)
    ' Remove any files older than specified days from this folder
    For Each objFile In objFolder.Files
        
        ' Get the desired date for the age check        
        Select Case UCase(DateToUse)
           Case "C"
              datFile = objFile.DateCreated
           Case "M"
              datFile = objFile.DateLastModified
           Case "A"
              datFile = objFile.DateLastAccessed
           Case Else
              datFile = objFile.DateLastModified
        End Select

        If DateDiff("d", datFile, Now) > intDays Then 
            Wscript.Echo "Deleted file: " & objFile.Path
            objFile.Delete
        End If
    Next

    If ProcessSubFolders Then
        ' Remove all older files in any subfolders of this one
        For Each objSubFolder In objFolder.Subfolders
            RemoveOldFiles objSubFolder, intDays
        Next
    End If

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

Open in new window

~bp
0
 
LVL 15

Assisted Solution

by:ZabagaR
ZabagaR earned 340 total points
ID: 39213200
You can do that with 1 line of text in a batch file.

For instance the below deletes all *.bkf files older than 7 days inside c:\myfiles

Forfiles -p C:\myfiles -m *.bkf -d -7 -c "CMD /C del @file"

Just substitute with your own needs.

You could wrap that 1 command inside VBscript too

dim objshell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run ("cmd.exe /c Forfiles -p C:\myfiles -m *.bkf -d -7 -c " & chr(34) & "CMD /C del @file" & chr(34))
0
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 680 total points
ID: 39213284
@Jonathan
Any updates with the script?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Simple Linear Regression
Six Sigma Control Plans

916 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