Backing up and deleting contents of folder

Hello,

I am a complete newbie when it comes to the world of Applescript. I’ve bought some books to try and learn how to code in Applescript, and I’ve made some simple scripts, however I have a pressing need to create an Applescript that will automate a very critical backup process.

I was wondering if somebody might be able to help me develop a script that, while running, will take all of the contents of a particular folder, move the contents to a folder named that particular days date, and then delete the contents of the folder it copied them from. This script would run once a day at 11:59 PM.

Any help you guys could provide would be greatly appreciated.

I also have a script running that converts .tif’s into .pdf’s, using Adobe Acrobat Professional 7. Most of the time the script runs great, however occasionally a .tif will open and then Adobe will be unable to save the file for some reason. After this, all files sent to that folder are not converted until the original file that caused the script to stop gets deleted.

Here is the script in question;

on adding folder items to this_folder after receiving added_items
set this_folder to this_folder as string
repeat with this_item in added_items
tell application “Finder” to set filename to the name of this_item
set shortname to characters 1 thru -5 of filename as string
set file_info to name extension of (info for this_item)
if file_info contains “tif” then
tell application “Adobe Acrobat 7.0 Professional”
open this_item
save document 1 to file (this_folder & shortname & “.pdf”)
close document 1
end tell
delete this_item
end if
end repeat
end adding folder items to

Thanks,
JG

Hi

I’m not sure if this is exactly what your after but it should be a step in the right direction!!

set thedate to date string of (current date)
tell application "Finder"
	make new folder with properties {name:thedate}
	set d to every item of (choose folder)
	duplicate d to folder thedate
	delete d
end tell

to kick the script off at a certain time you could use “iCal” i guess that would be the easiest way!! but there is others
which could involve a bit more work like launchd.

good luck

I managed to find another script that looks as if might be more up my alley, but I’m having some trouble with it. . This script was written for another purpose, but it does closely match what I’m trying to do, so I thought I’d give it a shot. Anyways, here’s the script;

property source_folder : "~/Desktop/Device Intake" (*source folder*)
property dest_folder : "1/1/2008" (*destination folder*)
property time_diff : 11 * hours (*time period to elapse before moving files and folders in source*)

on run
	set files_to_move to {} (*empty list*)
	set folder_contents to list folder (source_folder as alias) without invisibles (*get current contents of source*)
	repeat with aItem in folder_contents (*process data*)
		set current_item to source_folder & aItem as string (*convert to full Apple path as string*)
	end repeat
	if (count of files_to_move) > 0 then (*if no files/folders meet criteria then don't do anything*)
		set copied_files to my copy_the_files(files_to_move, dest_folder) (*copy NOT MOVE files/folders to destination*)
		if ((count of copied_files) = (count of files_to_move)) then (*check that all items have been copied*)
			set response to display dialog ((count of copied_files) as string) & " were moved to " & dest_folder & " Delete orginals ?" buttons {"Yes", "Quit"} default button 1
			if button returned of response = "Yes" then (*if all files copied then check to see if user wants to delete.*)
				set deleted_files to my delete_the_files(files_to_move) (*call routine to delete files*)
			end if
		else
			display dialog "ERROR: Some files not copied." (*if some files not copied then must not delete them!*)
		end if
	else
		display dialog "No files were found that met the criteria set." (*if no files meet crteria*)
	end if
end run

on copy_the_files(filelist, destination) (*rountine to copy files expressed in filelist*)
	set return_list to {}
	repeat with aItem in filelist
		tell application "Finder"
			activate
			set return_list to return_list & ((duplicate (aItem as alias) to (destination as alias)) as string)
		end tell
	end repeat
	return return_list (*return list of copied files*)
end copy_the_files

on delete_the_files(filelist) (*rountine to delete files. Files go to Trash. Can be recovered.*)
	set return_list to {}
	repeat with aItem in filelist
		tell application "Finder"
			activate
			delete (aItem as alias)
		end tell
	end repeat
	return return_list
end delete_the_files

For some reason I cannot get it to find the folder that I want it to move files from. Right now its on my desktop, and the path is “~/Desktop/Device Intake” The script keeps saying “File ~/Desktop/Device Intake wasn’t found” Like I said before, I’m a complete newbie when it comes to manipulating files, so any help would be appreciated.

It looks like the original script was not meant to use a POSIX path for the source_folder. Try this instead:

property source_folder : (path to desktop folder as Unicode text) & "Device Intake:" (*source folder*)

This will use an HFS path (which is what the coercion to alias expects) instead of the POSIX one (i.e. the string literal you were using).
Note: Because this script stores this path in a property, it will effectively be hard coded to work only for the user by which and the machine on which it was compiled. When moving to a new user or computer it would likely have to be recompiled. To arrange for this to be more easily portable between users and machines, try something like this:

property source_folder_on_desktop : "Device Intake:" (*source folder*)
--.other parts of script.
on run
	set source_folder to (path to desktop folder as Unicode text) & source_folder_on_desktop
	-- .rest of handler.
end run
--.rest of script.

This moves the determination of the HFS path to the desktop folder (which includes the boot volume’s name and the user’s short username) to runtime instead of compile time.

I have not looked at the rest of the script, the property just caught my eye and it turned out that it was contributing to your “File . wasn’t found” error.

Hi,

the easiest way to move the files is the rsync command of the shell, it creates also the destination directory.
The date string is in the format yyyy-mm-dd, it’s not recommended to use slahes in file names


property dest_folder : "/path/to/base/folder/" (*destination folder*)

set source_folder to POSIX path of (path to desktop) & "Device Intake/"
set timestamp to do shell script "/bin/date +%Y-%m-%d"
do shell script "/usr/bin/rsync -atE " & quoted form of source_folder & space & quoted form of (dest_folder & timestamp)
do shell script "/bin/rm -r " & quoted form of source_folder & "*"