2 Replies Latest reply: Mar 3, 2009 2:23 PM by BenChase
BenChase Level 1 Level 1 (10 points)
I have a script that I want to use to check the number of days old a file is. 2 things happen, I check to make sure the file is more then 5 days old and then I set the number of days old as a variable.

This is what I have but my problem is that it doesn't work if the file's mod date is in a different month.

How can I get this to work taking into account month and probably year?

property NumDays : 0 --Number of Days to check
property ProofsOutFolder : "my:location"

set dateNow to current date

tell application "Finder"
set theFiles to every file of folder ProofsOutFolder
repeat with aFile in theFiles
set fileModDate to modification date of aFile
if ((day of dateNow) - (day of fileModDate)) ≥ NumDays then
set DaysOut to ((day of dateNow) - (day of fileModDate))
  • red_menace Level 6 Level 6 (14,720 points)
    When you subtract two dates, the result is the difference in seconds. There are built-in constants for the number of seconds in various time elements, such as hours and days, so you can subtract one date from the other and divide by the days constant (or the number of seconds in a day) to get the number of days.

    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px;
    color: #000000;
    background-color: #FFEE80;
    overflow: auto;"
    title="this text can be pasted into the Script Editor">
    property NumDays : 5

    set DateNow to (current date)
    set FilterTime to (NumDays * days) -- the number of seconds

    set TheFIle to (choose file)
    tell application "Finder"
    set FileModDate to modification date of TheFIle
    log "modification date:  " & FileModDate
    set TimeDiff to (DateNow - FileModDate) -- the difference in seconds
    if TimeDiff ≥ FilterTime then
    set DaysOut to (TimeDiff div days) -- the number of days
    log DaysOut
    end if
    end tell
  • BenChase Level 1 Level 1 (10 points)