Backup Folder

Hi, I’m new, recent switcher (1year), on Os X.

I try to transpose the scripts i used in MCS system.
The problem, except that AppleScript Force me to relearn à new language, is, there is no problem.
My script Works. (Thx to Google, MacScripter, OsxHints, a co-worker, etc, etc…)
But i want to know if there some possibilities to improve the writting and maybee the fonctionnalities of my scripts.

This one is my first try.


--1- Set Var
--1.1- Choose Folder Where The Files Will Be Copied (VarName:BUFolder)  
set BURootFolder to choose folder with prompt "Backup Folder"
--1.2- Set The Source Folder (VarName:VMFolder)
set VMFolder to ((path to home folder) as text) & "Documents:Virtual Machines:"
--1.3- Get The Date in %Y-%m-%d (ie:2008-01-17) (VarName:StrDate) 
set myyear to year of (current date) as integer -- Retrieve Year
set mymonth to month of (current date) as integer -- Retrieve Month
set myday to day of (current date) as integer -- Retrieve Day
--1.3.1- Change the month if only 1 car length like February = 2 change to 02
set MonthLength to (length of (mymonth as text))
if MonthLength > 1 then
	set StrDate to "VMware-" & myyear & "-" & mymonth & "-" & myday
else
	set StrDate to "VMware-" & myyear & "-0" & mymonth & "-" & myday
end if

--2- Duplication
tell application "Finder"
	--2.1- If Exists Destination Folder : Delete it
	if exists folder ((BURootFolder & StrDate) as text) then
		delete folder ((BURootFolder & StrDate) as text)
	end if
	--2.2- Create the Destination Folder with name VMware-" & StrDate (VMware-2008-02-2)
	set BUFolder to make new folder at BURootFolder with properties {name:StrDate}
	--2.3- Duplicate with ignoring timeout	
	ignoring application responses
		set BA to duplicate every item in entire contents of (folder VMFolder) to BUFolder
	end ignoring
end tell

So please comments and tell me what i did wrong and what’s ok.

Thx,

P

Model: MacBook2,1
AppleScript: 2.0
Browser: Safari 523.10.6
Operating System: Mac OS X (10.5)

Hi,

welcome to MacScripter :slight_smile:

If I understand right, you want to backup a folder with its entire contents to another folder.
If the folder exists, delete the contents
If not, create the folder.

The shell has a perfect function called rsync, to backup entire folders,
with a lot of options like delete items in destination which don’t exist anymore in source
and copy only the changed or new files.

Also to compose the date string a shell script is more convenient

--1- Set Var
--1.1- Choose Folder Where The Files Will Be Copied (VarName:BUFolder)  
set BURootFolder to choose folder with prompt "Backup Folder"
--1.2- Set The Source Folder (VarName:VMFolder)
set VMFolder to ((path to documents folder as text) & "Virtual Machines:")
--1.3- Get The Date in %Y-%m-%d (ie:2008-01-17) (VarName:StrDate) 
set StrDate to "VMware-" & (do shell script "date +%Y-%m-%d")

--2- Duplication
-- creates folder if doesn't exist
try
	tell application "Finder" to make new folder at BURootFolder with properties {name:StrDate}
end try
--2.3- Duplicate with ignoring timeout	
do shell script "rsync -vautE --delete " & quoted form of POSIX path of VMFolder & space & quoted form of (POSIX path of BURootFolder & StrDate)

Here is a optimized version of your original script, using the Finder


--1- Set Var
--1.1- Choose Folder Where The Files Will Be Copied (VarName:BUFolder)  
set BURootFolder to choose folder with prompt "Backup Folder"
--1.2- Set The Source Folder (VarName:VMFolder)
set VMFolder to ((path to documents folder as text) & "Virtual Machines:")
--1.3- Get The Date in %Y-%m-%d (ie:2008-01-17) (VarName:StrDate) 
set StrDate to "VMware-" & (do shell script "date +%Y-%m-%d")

--2- Duplication
tell application "Finder"
	-- if the folder doesn't exist, create it
	set BUFolder to (BURootFolder as text) & StrDate
	try
		make new folder at BURootFolder with properties {name:StrDate}
	on error
		-- if the folder exist, delete every item
		delete every item of folder BUFolder
	end try
	--2.3- Duplicate with ignoring timeout	
	ignoring application responses
		duplicate every item in entire contents of folder VMFolder to folder BUFolder
	end ignoring
end tell

Hi, Portelly. Welcome to the site. :slight_smile:

There are a few things in your script that are either unnecessary or which are done more than is necessary.

For instance, the year and day of a date are already integers, so there’s no need to use ‘as integer’ when you get them.

Also, ‘current date’ is a function that gets the current date from the system, so every time ‘current date’ is used, that function’s called again. It’s much better practice to call the function just once and store the result in a variable:

--1.3- Get The Date in %Y-%m-%d (ie:2008-01-17) (VarName:StrDate)
set now to (current date)
set myyear to year of now -- Retrieve Year 
set mymonth to month of now as integer -- Retrieve Month 
set myday to day of now -- Retrieve Day

This also ensures that you’re getting the year, month, and day from the same date if the script’s run just before midnight!

In fact, there are other ways to get the short-date result you want. The easiest for you to remember would be:

set StrDate to "VMware-" & (do shell script "date +%Y-%m-%d")

I myself am psychologically incapable of using anything as slow as a shell script to get a date or time and prefer “vanilla” AppleScript methods. The following is horrible to behold but very fast:

set now to (current date)
set myyear to year of now -- Retrieve Year 
set mymonth to month of now as integer -- Retrieve Month 
set myday to day of now -- Retrieve Day
tell (myyear * 10000 + mymonth * 100 + myday) as text
	set StrDate to "VMware-" & text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
end tell

Your variable BURootFolder contains an alias value and StrDate is text, so the expression ‘((BURootFolder & StrDate) as text)’ concatenates a text to an alias ” resulting in a list ({alias value, text value}) that’s then coerced to text. It’s very slightly more efficient to coerce the alias to text first and then concatenate the other text directly to it. Also, of course, you don’t want to do it all again in the following line. So:

tell application "Finder"
	--2.1- If Exists Destination Folder : Delete it
	set BUFolderPath to (BURootFolder as text) & StrDate
	if (exists folder BUFolderPath) then
		delete folder BUFolderPath
	end if
end tell

I hope that’s the sort of thing you wanted to know. :slight_smile:

Great \o/

Thx for all Stefan & Nigel, that’s exactly what i wanted to know.