I want to make a script that easily deletes material older then x days from folders.
My idea was to create a folder on the desktop and make aliases from other folder which i want to clean up and put them in this folder.
the idea was to clean up the folder on my desktop and thus all aliases of the other folders also.
but offcourse it doesn’t work like that
my problem is that the folders i want to clean up change so now and then. i could make a list, add the folders and clean 'm all up but
i thought my idea would be quicker option.
any suggesttions ?
Kemalski
Hi,
is this a point to start?
set aliasFolder to ((path to desktop as text) & "aliasFolder:")
tell application "Finder"
set theAliases to every alias file of folder aliasFolder
repeat with oneAlias in theAliases
set theOriginal to original item of oneAlias
my deleteItems(theOriginal, 7) -- delete items older than 7 days
end repeat
end tell
on deleteItems(theItem, d)
if class of theItem is folder then
tell application "Finder" to delete (items of theItem whose creation date < ((current date) - d * days))
else
tell application "Finder" to if creation date of theItem < ((current date) - d * days) then delete theItem
end if
end deleteItems
Stefan,
to be honest this is exact what i need
i did not know the set to original item syntax. nice
I got an error running it ‘Can’t get folder of folder’
I changed the script to:
set aliasFolder to ((path to desktop as text) & "aliasFolder:") as text
tell application "Finder"
set theAliases to every alias file of folder aliasFolder
repeat with oneAlias in theAliases
set theOriginal to original item of oneAlias
tell application "Finder" to delete (every file of entire contents of theOriginal whose modification date is less than ((get current date) - 7 * days))
end repeat
end tell
works great!
again thank you so much!
Glad that it helped
a note:
as text is useless, the result of the concatenation is text
thanks.
i forgot that to delete after trialing where the error came from.
thanks again!
I stumbled on a problem today with some files which I could not delete because i am not the owner.
These files are on a network volume but have to bo deleted
I guess
tell application "Finder" to delete (every file of entire contents of theOriginal whose modification date is less than ((get current date) - cleanup_ratio_days * days))
has to be changed to a do shell script which deletes the file from the folder & subfolders regardless of who the owner is. At least that is what I want
I find shell scripting rather difficult so if you could please help me out on this one.
thanks!
Kemal
found and tried
do shell script (("cd " & theOriginal & " && rm -rf *") as text) password admin_password with administrator privileges --tell application "Finder" to delete (every file of entire contents of theOriginal whose modification date is less than ((get current date) - cleanup_ratio_days * days))
but that gives an error
do shell script (("cd " & theOriginal & " && rm -rf *") as text) password admin_password with administrator privileges)
You can delete files in a restricted area only with a shell script
Caution: the following line deletes files permanently
do shell script "find " & quoted form of POSIX path of theOriginal & " -type f ! -name '.*' ! -mtime -" & cleanup_ratio_days & " -exec rm {} \\;" with administrator privileges
Description:
-type f - searches only for files
! -name ‘.*’ - excludes invisible files (which start with a dot)
! -mtime [d] - excludes files newer than d days
-exec rm - deletes the found files
your can pass also user name and password
do shell script "find " & quoted form of POSIX path of theOriginal & " -type f ! -name '.*' ! -mtime -" & cleanup_ratio_days & " -exec rm {} \\;" user name "username" password "¢¢¢¢¢" with administrator privileges
thanks again
i got this error
Can’t make quoted form of POSIX path of «class cfol» “test 2” of «class cfol» “Desktop” of «class cfol» “Kemal” of «class cfol» “Users” of «class sdsk» of application “Finder” into type Unicode text. (-1700)
the good old posix path
will try to find that one out.
also the -f statement finds only files.
your earlier manpagez.com tip tells me i have to use -d for files and folders. true ?
kemalski
If theOriginal is a Finder file specifier, use
quoted form of POSIX path of (theOriginal as alias)
if every item should be found, omit the -type switch
PS: to delete files andfolders with rm, add the recursive switch
. -exec rm -r {}
The story continues…
For security reasons, you may not tell another application to do shell script with administrator privileges. Put the command outside of any tell block, or put it inside a tell me block.
Will try that
++edit:
tell me to do shell script “find " & quoted form of POSIX path of (theOriginal as alias) & " -type f ! -name ‘.*’ ! -mtime -” & cleanup_ratio_days & " -exec rm -r {} \;" with administrator privileges
YES!
And if i use the -exec rm -r {} switch it doesn’t delete the folders. It changes the modification date to todat so no folders are deleted. Will play with that also
Kemal