deleting files folder older than 30 days

Hello just asking to see if anybody can help me - as i am new to applescript i am looking at finding a script to delete files and folders within a folder. I have a script that works a treat on my local machine… What i was wanting to do if it is possible is on our server have a folder where all the teams on our network can transfer files into and then some how run a script to delete them after 30 days. i was going to place the script on my ical and get it to run on a monthly basis…

The problem is getting the path correct to mount the server and look in the named folder and delete the files - any ideas would be very appreciated or suggestions…

here is the script so far:
try
tell application “Finder”
try
delete (every item of folder “Bally” of folder “Desktop” of folder “marcfield” of folder “Users” of startup disk whose modification date is less than ((get current date) - 30 * days))
end try
end tell
end try
end

tell application “Finder”
empty the trash
beep
end tell

Model: G5
AppleScript: 1.10.7
Browser: Safari 522.12
Operating System: Mac OS X (10.4)

Moved to AppleScript | OS X

Hi,

something like this

property un : "user"
property pw : "pass"
property myServer : "myServer.local"
property myVolume : "myVolume"
property theFolder : "myVolume:Users:me:documents:myFolder:"

try
	if myVolume is not in (do shell script "ls /Volumes") then
		mount volume "afp://" & un & ":" & pw & "@" & myServer & "/" & myVolume
	end if
	
	tell application "Finder"
		delete (get items of folder theFolder whose modification date < ((current date) - 30 * days))
		empty
		say "items deleted"
	end tell
end try

hello thanks for replying i am unsure how this works but will this script run from my machine over onto the server … as i am new to this i have tried different options do you have any formula for this that is idiot proof.???

I can get it work on my machine but not on the server…

i can access the folder by using /Volumes/Server/Bally which takes me to the folder on the server…

The folder is on part of a raid of the server so i need to somehow tell it to go to the above path and remove contents within this folder…

so from my machine - mount server - goto folder Bally which is loose on the Server/Raid and delete contents…

do i need to run this on the local server???

i will keep tying in the meantime

:confused:
cheers

marc

here is the code so far - which elements do i need to change to set it up for the correct path as shown above??
property un : “xxxx”
property pw : “xxxx”
property myServer : “Macintosh HD.local” - ???
property myVolume : “Macintosh HD” - ???
property theFolder : “Macintosh HD:Users:marcfield:documents:Bally:” - ???

try
if myVolume is not in (do shell script “ls /Volumes”) then
mount volume “afp://” & un & “:” & pw & “@” & myServer & “/” & myVolume
end if

tell application "Finder"
	delete (get items of folder theFolder whose modification date < ((current date) - 30 * days))
	empty
	say "items deleted"
end tell

end try

Hi

What Stefan has give you here is a script to run on your machine or your colleagues.
which will mount the server then proceed to check for 30 day old folders etc…

if you want to run it on the actually server then something like this run periodically from ical may do the trick!

tell application "Finder"
	set theFolder to alias "Macintosh HD:Users:marcfield:documents:Bally:"
	delete (get items of folder theFolder whose modification date < ((current date) - 30 * days))
	empty -- this line will empty your trash! 
end tell

heres a guide to getting an alias path to a folder. open up script editor and type

choose folder

and then compile and run.
you will be prompted to find a folder!
When you have the folder your after then click okay and in the result window of script editor will be the alias file path to that particular folder copy and paste it onto your script.

I found this script very useful, but am experiencing a problem with it. In my use below, if I include the “try” part, then the server gets connected but the file(s) never get moved. When I remove the “try” section and have the server attached, the files get moved over fine. I also tried putting the finder command inside the try section, but still the server gets connected to and the files don’t get moved. Where am I going wrong? Here is the script:


property un : "user"
property pw : "pass"
property myServer : "192.168.5.1"
property myVolume : "exports"
property myFolder : "Folder1:mystuff"


on adding folder items to this_folder after receiving these_items
	
	try
		if myVolume is not in (do shell script "ls /Volumes") then
			mount volume "smb://" & un & ":" & pw & "@" & myServer & "/" & myVolume
		end if
		
	end try
	
	tell application "Finder"
		move these_items to myVolume & ":" & myFolder
	end tell
	
	
end adding folder items to

you cannot move files to a string

try

move these_items to folder (myVolume & ":" & myFolder)

Do I feel stupid! I was thrown off by the fact that it was working without the try section. I still don’t understand how that’s possible now that I see this.

Thanks for the heads-up.

I just figured out, it’s still possible to specify the folder in a Finder tell block just by a string (ending with a colon) but it’s not a good style.

Debugging folder actions by running them it’s hopeless because you get no error messages, the handler just doesn’t work.
I usually debug folder actions like this (assuming this_folder is not needed)

instead of

on adding folder items to this_folder after receiving these_items
	-- your code
end adding folder items to

I use

-- on adding folder items to this_folder after receiving these_items
set these_items to choose file with multiple selections allowed
-- your code
-- end adding folder items to

then you get normal error messages if something’s going wrong

Big Big Thanks to stefan & pidge1 - this script is working now - thankyou very much - if you ever passing Elland in Yorkshire i will buy you a beer - cheers. This is going to save me some time…

here is the script that runs correctly with my ical on a monthly basis- deleting files/folder older than 30 days in a specified folder…

tell application “Finder”
set theFolder to alias “Server:Transfer:”
delete (get items of folder theFolder whose modification date < ((current date) - 30 * days))
empty – this line will empty your trash!
end tell

it looks so simple - but the best things in life are simple…

cheers once again

marc.:smiley:

Hi Marc,

a better syntax is either

tell application "Finder"
    set theFolderPath to "Server:Transfer:"
    delete (get items of folder theFolderPath whose modification date < ((current date) - 30 * days))
    empty -- this line will empty your trash!
end tell

or

tell application "Finder"
    set theFolderAlias to alias "Server:Transfer:"
    delete (get items of theFolderAlias whose modification date < ((current date) - 30 * days))
    empty -- this line will empty your trash!
end tell

do you see the difference? :wink:

Yes Stefan this is good (spotted the Alias) - am i write in saying both ways are safe as i don’t want it to start deleting folders which are loose on the server - i take it the script will only delete the named folder items,… i checked it out with ical and it works a treat - hooraay!!

Here is another script i need to resolve - i have set up a distiller server and created in & out folders for the people to use… what i need to do now if it is possible is the files that drop in to the out folder get a script that will move the files to another folder on our server - but the problem is that we have a job no for each job - the pdf’s in the out folder have a job number at the start…i.e “5000”-joebloggs.pdf (5001) and so on.

job folder - 5000 and so on
then inside that folder is a ArtworkPDF folder… -
the folder structure stays the same once inside the job number.

so the pdf in the out folder needs to some how tell the file to move to a folder based on the first few digits and place the file inside the ArtworkPDF folder - i hope this makes some sense??? - i am not sure if applescript is powerful enough for this… - i will probably have to tweak it to make sure it works in every instance…

A guy is coming in today who normally deals with our automated/filemaker side of things - but i am trying to set some of these up myself - and also with the help of you guys…

any ideas or just to tell me that this is impossible before i try and start it…

cheers

marc

  • 1 step closer to polishing off applescript - not!!

Of course AppleScript can do this,
but more detailled information about the folder structure is needed