Auto move files/folders older than 2 weeks?

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

  1. techmonkey macrumors 6502a

    Joined:
    Jun 8, 2007
    #1
    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

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #2
    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.

    Code:
    #!/bin/bash
    
    #
    # 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
    fi
    
    # Directory Path
    purgePath=$1
    
    if [ ! -d $purgePath ]; then
            errWarn purgeFiles: Invalid Directory Path Specified, $purgePath
            echo "Invalid Directory Path Specified, $purgePath"
            exit 1
    fi
    
    # If there were 2 param's, the second param is the number of day's to purge
    if [ $# -eq 2 ]; then
            purgeTime=$2
    else
            purgeTime=30
    fi
    
    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"
            else
                    echo "     Failed to Purge File: $1"
            fi
    
    elif (test -d $1) then
            dirCount=$(($dirCount + 1))
            echo "   Purging Directory: $1"
            rm -fr $1
            if [ $? -eq 0 ]; then
                    echo "     Successfully Purged Directory: $1"
            else
                    echo "     Failed to Purge Directory: $1"
            fi
    
    fi
    
    }
    
    
    fileCount=0
    dirCount=0
    
    for oldFile in `find $purgePath -mtime +$purgeTime -prune -print` ; do
    doPurge $oldFile
    done
    
    echo purgeFiles: Successfully Purged $fileCount files, and $dirCount directories
    
    exit 0
    
    
     
  3. sorryiwasdreami macrumors 6502a

    sorryiwasdreami

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

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #4
    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

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #5
    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

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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.

    -Lee
     
  7. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #7
    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