Solved

deleting files older than x amount of days

Posted on 2013-05-28
7
343 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 10

Assisted Solution

by:JEaston
JEaston earned 85 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:sedgwick
sedgwick earned 170 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 42

Expert Comment

by:sedgwick
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 53

Assisted Solution

by:Bill Prew
Bill Prew earned 85 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 85 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:
sedgwick earned 170 total points
ID: 39213284
@Jonathan
Any updates with the script?
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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

813 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

14 Experts available now in Live!

Get 1:1 Help Now