Solved

deleting files older than x amount of days

Posted on 2013-05-28
7
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 5

Expert Comment

by:djbaum
ID: 39200555
0
 
LVL 11

Assisted Solution

by:John Easton
John Easton 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
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!

 
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 54

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

Technology Partners: 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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

751 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