PDA

View Full Version : Auto move files/folders older than 2 weeks?




techmonkey
May 9, 2008, 01:42 PM
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.



pilotError
May 9, 2008, 01:50 PM
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.


#!/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

sorryiwasdreami
May 9, 2008, 01:57 PM
I think you can set up a smart folder to do that.

ChrisA
May 9, 2008, 03:54 PM
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.

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

HiRez
May 9, 2008, 04:00 PM
I think you can set up a smart folder to do that.
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.

lee1210
May 9, 2008, 04:54 PM
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

HiRez
May 9, 2008, 05:21 PM
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).