Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 07-19-2004, 02:45 PM   #1
murpheeee
Prospect
 
Join Date: Jul 2004
Posts: 5
Applescript needed - Delete files older than 3 days

For a lab OS-X machine, I need to write an Applescript to run at startup which will delete all files from a "User Data" partition (including any subdirectories) which are older than 3 days.

Anyone help me out?
murpheeee is offline   Reply With Quote
Old 07-20-2004, 01:17 AM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
This is something that is probably better done in a shell script instead of AppleScript. You would use the 'find' program in a shell script.
See for example, this thread:
http://forums.macosxhints.com/showthread.php?t=6774
hayne is offline   Reply With Quote
Old 07-20-2004, 09:43 AM   #3
chabig
Hall of Famer
 
Join Date: Jan 2002
Posts: 3,016
Do you happen to be managing this lab with OS X Server? Doesn't it allow automatically restoring hard drive images? This might be the easiest way.

Chris
chabig is offline   Reply With Quote
Old 07-20-2004, 01:01 PM   #4
murpheeee
Prospect
 
Join Date: Jul 2004
Posts: 5
Quote:
Originally Posted by chabig
Do you happen to be managing this lab with OS X Server? Doesn't it allow automatically restoring hard drive images? This might be the easiest way.

Chris


No, using Windows 2000 server at the moment
murpheeee is offline   Reply With Quote
Old 07-20-2004, 01:54 PM   #5
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
Quote:
Originally Posted by murpheeee
I need to write an Applescript to run at startup

If you must run the script at startup then you're sunk as you must be logged in before any Applescripts can be run. If it must be at startup then you will have to use a shell script.
bramley is offline   Reply With Quote
Old 07-20-2004, 02:19 PM   #6
murpheeee
Prospect
 
Join Date: Jul 2004
Posts: 5
Quote:
Originally Posted by bramley
If you must run the script at startup then you're sunk as you must be logged in before any Applescripts can be run. If it must be at startup then you will have to use a shell script.


well how about as a login event when I login as a speficic user?

the machines all auto-login to one user account and I already run a script on login to mount a network volume.....

Last edited by murpheeee; 07-20-2004 at 02:23 PM.
murpheeee is offline   Reply With Quote
Old 07-20-2004, 02:37 PM   #7
X@vier
Triple-A Player
 
Join Date: Jul 2003
Posts: 55
I need the same

Well I have the same problem. I have this script that worked under Jaguar but not under Panther maybe someone can help us or finish it for us



tell application "Finder"
set FOLDERwithITEMS to alias ((startup disk as string) & ":Users:testuserocuments")

if (count of FOLDERwithITEMS) < 1 then

empty

else

set locked of (files of entire contents of FOLDERwithITEMS) to false
update (files of entire contents of FOLDERwithITEMS)
delete entire contents of FOLDERwithITEMS
empty

end if

end tell
X@vier is offline   Reply With Quote
Old 07-21-2004, 10:51 PM   #8
joshuawait
Prospect
 
Join Date: Jul 2004
Location: San Francisco Bay Area
Posts: 3
Lab Environment Scripts

I saw a presentation on managing labs using Mac OS X. They had an excellent presentation and some useful scripts

http://www.macosxlabs.org/tools_and_scripts/index.html
joshuawait is offline   Reply With Quote
Old 07-22-2004, 01:24 PM   #9
murpheeee
Prospect
 
Join Date: Jul 2004
Posts: 5
I was told on another board to add this to the RC file:

#BEGIN
echo -n "Removing contents of UserData..."
find /Volumes/UserData ! -atime 3 -exec rm {} \;
echo "Done"
# END

Which I did....but it does not seem to do anything.....
murpheeee is offline   Reply With Quote
Old 07-22-2004, 03:50 PM   #10
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
Here's a Applescript/shell script that does what you want. You need to change the property "root_folder" to be the UNIX path of the top level folder.

Code:
property root_folder : "full UNIX path to the top level folder"

set thescripttext to "find " & root_folder & " -type d -mtime +3 -print0 | xargs -0 rm -R"
do shell script thescripttext
set thescripttext to "find " & root_folder & " -type f -mtime +3 -print0 | xargs -0 rm"
do shell script thescripttext
PS Thanks to Hayne for the link above, and to the posters therein.
bramley is offline   Reply With Quote
Old 08-15-2006, 09:44 AM   #11
Mr Dudikowski
Prospect
 
Join Date: Feb 2006
Posts: 2
I needed a solution for this type of thing too.
In my case I have a backup folder filling up with one 35 mb file per day. I want to keep the latest two files.

I found an Apple Script at another forum and added the code that checks for new items. Apply this to a folder as a folder script and the folder will keep itself clean. Increase the number 2 to to keep even older files.

Code:
on adding folder items to this_folder after receiving added_items
	try
		tell application "Finder"
			try
				delete (every item of folder "myfoldername" of folder "Desktop" of folder "myusername" of folder "Users" of startup disk whose modification date is less than ((get current date) - 2 * days))
			end try
		end tell
	end try
end adding folder items to
Mr Dudikowski is offline   Reply With Quote
Old 08-23-2006, 02:43 AM   #12
pfrown
Prospect
 
Join Date: Feb 2002
Location: Brisbane / Australia
Posts: 33
Hi there,

just browsing the forums and came acros this post. A while ago I wrote a shell script designed to be run as a cron/launchd job (in a lab environment) to clean files that have not been created/edited/viewed for more than a specified period of time. Feel welcome to use it if it suits your needs, it has been tested quite a bit but of course it's provided "as-is" and if you use it to break something (it can be VERY destructive when used without caution) don't come crying back to me.

Regards,

Trystan
Attached Files
File Type: sh clean-student-files.sh (8.2 KB, 340 views)
pfrown is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 05:58 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.