Solved

deleting files older than x amount of days

Posted on 2013-05-28
7
342 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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 …
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…

932 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