Solved

deleting files older than x amount of days

Posted on 2013-05-28
7
340 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 51

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 78
What is Python programming? 3 68
If not exist vbs error 4 28
Protecting vb6 & .Net code Obfuscation 18 56
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…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

747 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

12 Experts available now in Live!

Get 1:1 Help Now