Automatically delete old files from a specified folder?

2

I want to set up an automatic task that will automatically delete files older than X days from a specific folder. I'd prefer to do it without having to install any 3rd party software, but I'll be ok if it's a very small and simple utility.

I want this to force me to keep my files organized. I am setting my browser to download to a standard folder, and then with this script to automatically delete files older than a week, I will be forced to move & organize downloaded files I want to keep.

davr

Posted 2010-04-07T02:58:57.103

Reputation: 4 809

Answers

2

How about Belvedere from one of Lifehacker's editors.

alt text

outsideblasts

Posted 2010-04-07T02:58:57.103

Reputation: 6 297

1

This is pretty easy with Windows Scripting Host. A sample script looks something like this:

numDays = 7

Set objfso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.GetFile("C:\test.txt")

Sub DeleteOldFile(objFile, numDays)
    dateFile = objFile.DateLastModified
    dateToday = Now()

    If dateFile <= dateToday Then
        daysOld = dateToday - dateFile
        If daysOld > numDays Then
            objFile.Delete
        End If
    Else
        WScript.Echo "Incorrect date stamp in", strFile
    End If

End Sub

You would need to write your script then run it as a scheduled task. Maybe thirty minutes after you log on on Friday mornings to give yourself one last chance to get those files you want to keep moved. The complete source for this example.

Beaner

Posted 2010-04-07T02:58:57.103

Reputation: 3 193

1

I coded myself a solution for this a while back right before I found DelOld. It's a very small tool which does exactly what you want, and it is available as a .jar file, wrapped java executable, Visual Basic executable (MSVBVM60.DLL required), and of course as source code.

Simply supply it 2 command-line arguments -- the path of the folder, and the number of days before a file should be deleted. It can also be run through a batch script via Task Scheduler.

example:

DelOld.exe "C:\Downloads" 30

This will delete all files older than 30 days under C:\Downloads.

John T

Posted 2010-04-07T02:58:57.103

Reputation: 149 037

1

There's a windows command called forfiles. I think it was on the windows 2000 resource kit. I found a link to it from petri's web site: http://www.petri.co.il/download_free_reskit_tools.htm It has a lot you can do with it including removing individual files based on how old they are or whole directories.

Nixphoe

Posted 2010-04-07T02:58:57.103

Reputation: 566