Jump to content

Deleting old files


SpookMeister
 Share

Recommended Posts

This is probably too simple to be of use to anyone, but maybe it will help somebody out.

I was making a maintenance script to do a lot of specialized things. You know... move this file there, clean out this folder here etc. The part that got interesting was where I wanted to delete files in a particular folder that were more than a year old.

Here's the relevant parts:

#include <date.au3>

$ipath="C:\Insert\Your\Path\Here\"
$JulianDate = _DateToDayValue (@YEAR, @MON, @MDAY)

$search = FileFindFirstFile($ipath & "*.*")  
While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    ; Avoid all subdirectories as well as "." and ".."
    $attrib = FileGetAttrib($ipath & $file)
    If Not StringInStr($attrib , "D") Then
    ; Get the last modified time of this file
        $mod = FileGetTime($ipath &  $file, 0, 0)
        $fileJulDate= _DateToDayValue ($mod[0], $mod[1], $mod[2])
        $datediff= $JulianDate-$fileJulDate
    ; If the file is more than 365 days old then delete it
        If $datediff > 365 Then FileDelete($ipath & $file)
    EndIf
WEnd
; Close the search handle
FileClose($search)

As you can see most of this is straight out of the help files, but knowing what to look for in the help file.. now there lies the tricky part for most of us newbs to autoit.

Hope it helps

-Spook

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

Here it is in function form... I thought it was cleaner this way. It returns the number of days since the file was last modified.

;You must #include <date.au3>
Func _FileAge($FilePathAndName)
    $iToday = _DateToDayValue (@YEAR, @MON, @MDAY)
    $iTime = FileGetTime($FilePathAndName,0,0)
    $iJulian = _DateToDayValue($iTime[0], $iTime[1], $iTime[2])
    $iAge = $iToday - $iJulian
    If $iAge < 0 Then $iAge = 0
    Return ($iAge)
EndFunc
Edited by SpookMeister

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

Here it is in function form... I thought it was cleaner this way. It returns the number of days since the file was last modified.

;You must #include <date.au3>
Func _FileAge($FilePathAndName)
    $iToday = _DateToDayValue (@YEAR, @MON, @MDAY)
    $iTime = FileGetTime($FilePathAndName,0,0)
    $iJulian = _DateToDayValue($iTime[0], $iTime[1], $iTime[2])
    $iAge = $iToday - $iJulian
    If $iAge < 0 Then $iAge = 0
    Return ($iAge)
EndFunc
@SpookMeister, nice work. I was thinking about a function like this for a project I am currently writing. Your code just came at the correct time. Thanks!!! :o

Cheers.. B)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...