Auto move files/folders older than 2 weeks?

Discussion in 'Mac Programming' started by techmonkey, May 9, 2008.

  1. techmonkey macrumors 6502a

    Jun 8, 2007
    Anyone have or seen a script that scans a directory and moves files/folders that are older than X number of days? I would like to declutter my downloads folder and only keep the last week worth of downloads in the top folder.
  2. pilotError macrumors 68020


    Apr 12, 2006
    Long Island
    I have a linux shell script that will do it. I don't know if there's an applescript that can do it. You may need to do a little work to get it to run on the Mac.

    # This script purges Files and Directories that are older than <days to purge>
    # Usage: purgeFiles <directory> <days to purge>
    # Un-Comment for Debugging
    #set -xv
    # Check the number of parameters
    if [ $# -lt 1 ]; then
            echo "Invalid Call to purgeFiles, no parameters specified"
            echo "Usage: purgeFiles <directory path> [<# of days old>]"
            echo "  Purges files or entire directories older than the parameter"
            echo "  specified (default is 30 days) located in the directory path"
            exit 1
    # Directory Path
    if [ ! -d $purgePath ]; then
            errWarn purgeFiles: Invalid Directory Path Specified, $purgePath
            echo "Invalid Directory Path Specified, $purgePath"
            exit 1
    # If there were 2 param's, the second param is the number of day's to purge
    if [ $# -eq 2 ]; then
    echo purgeFiles: Purging $purgePath, removing anything older than $purgeTime days old
    function doPurge {
    if (test -f $1) then
            fileCount=$(($fileCount + 1))
            echo "   Purging File: $1"
            rm -f $1
            if [ $? -eq 0 ]; then
                    echo "     Successfully Purged File: $1"
                    echo "     Failed to Purge File: $1"
    elif (test -d $1) then
            dirCount=$(($dirCount + 1))
            echo "   Purging Directory: $1"
            rm -fr $1
            if [ $? -eq 0 ]; then
                    echo "     Successfully Purged Directory: $1"
                    echo "     Failed to Purge Directory: $1"
    for oldFile in `find $purgePath -mtime +$purgeTime -prune -print` ; do
    doPurge $oldFile
    echo purgeFiles: Successfully Purged $fileCount files, and $dirCount directories
    exit 0
  3. sorryiwasdreami macrumors 6502a


    Apr 24, 2004
    way out in the sticks
    I think you can set up a smart folder to do that.
  4. ChrisA macrumors G4

    Jan 5, 2006
    Redondo Beach, California
    You can do it in one line with a shell script. In terminal type "man find" to read about ho the "find" command works. Find accepts a "condition" and an "action" to take and will apply the action to all fines where the condidtion is true. It will start scanning at a given location and will can the entire file tree under it. So as the condition you say "older than two weeks" and as the ction you'd say "rm" for remove. Details are on the man page..

    Typically people use the "crontab" command to schedule scripts like to above to run periodically, like maybe every night at 3:00am
  5. HiRez macrumors 603


    Jan 6, 2004
    Western US
    You could certainly set a smart folder to find those files based on date, but it doesn't look like you can then apply an action (or run a script) periodically based on what is found in the search (at least in Tiger, not sure about Leopard). That's a good idea for smart folder functionality though, I'd like to see it. You could just set up rules like in Mail and how often or under what conditions to apply the rules.

    Anyway, careful about setting up auto-delete scripts, that sounds potentially dangerous.
  6. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Just a note, a lot of the suggestions are for deletion. I think the OP just wants to move them to a subfolder. This makes it slightly trickier for 'find' (you could just set it to only go one folder deep, though). It seems like if applescript can inspect a smart folder like any other folder it could just move the items that qualify for the smart folder to the subdirectory.

  7. HiRez macrumors 603


    Jan 6, 2004
    Western US
    Another low-tech solution is to simply go to list view in your finder window and sort by date, that's what I do in my Downloads folder. Then all the recent stuff shows up on top (although I think that may depend on what you're downloading, I think some sites preserve the original file's modification date while some will create files or copy them with new dates when you download).

Share This Page