Create and Overwrite folders

Howdy all,

This is being done in Applescript Studio, but the question is more of an Applescript question.

What I’m trying to do is have the user select a folder to back up to, create a folder with the name “Backup” and then back up the contents of a disk to this folder. For the first time, this works fine, but on subsequent attempts, it fails because the folder already exists in that location. I, for whatever reason, cannot use the ‘with rewriting’ command with the ‘create folder’ command.

Here’s what I have so far…


--previous parts of the script
set backuploc to (create new folder at bploc with properties {name:"Backup"})
--what to do afterwardds

So basically, I’d like to be able to rewrite the folder if it already exists. Either that, or check and see if a folder with the name “Backup” already exists in that location, and if it does, set backuploc to that folder instead, as I can use ‘with rewriting’ with the duplicate command.

Any idea where to go with this, guys?

Thank ya :slight_smile:
-Parrot

Something like this?


if not (exists (bploc & "Backup:") then set backuploc to (make new folder at bploc with properties {name:"Backup"})
-- continue with the rest

Well one way to check to see if the folder exits would be to do

set backuploc to (bploc & "Backup") as text
tell application "Finder"
	if (exists folder backuploc) then
		-- yep its there, overwrite the files or delete the folder and recreate it
	else
		-- no its not, create it
	end if
end tell

or you can just “try” to make the folder… if it doesn’t exist it will create it

--previous parts of the script
try
	set backuploc to (make new folder at bploc with properties {name:"Backup"})
end try
--what to do afterwardds

Edit

Stop that Adam… for once I didn’t try to make something a one-liner though LOL

Nice one-liner, Adam (although those parentheses might need adjusting slightly)… :wink:

For this kind of situation (use a current item if it exists, create it if it doesn’t), a handler something like the following (which returns an alias of the existing/created folder) might be helpful:


to get_folder of folder_name at container_folder
	tell application "Finder" to tell folder container_folder
		tell folder folder_name to if exists then return it as alias
		(make new folder at it with properties {name:folder_name}) as alias
	end tell
end get_folder

set backup_folder to get_folder of "Backup" at choose folder with prompt "Choose a container folder for your backups:"
(* do something with backup_folder *)


I used something like that, but more like this…


try
					set backuploc to (make new folder at bploc with properties {name:"Backup"})
				on error
					set backuploc to ((bploc as string) & "Backup")
				end try

The catch is that it doesn’t try to make the folder first, it just goes straight to setting backuploc to (bploc & "Backup), which then returns an error that the folder cannot be found. I know it skips the create folder step due to the fact that there is no backup folder created in the folder I selected.

oh, yeah… before I forget… maybe the properties I’m using to carry the variables from one button to the next are causing my problem?


--properties
property pspsstick : {}
property bploc : {}
property backuploc : {}

I somehow get the idea that {} is not the right way to go about it, but using null seems to error…

-Kevin

edit apparently not… using this code, and creating Backup in the folder I’m going to use it in before running it, the script runs fine…

if name of theObject is "backupbut" then
				try
					set backuploc to (make new folder at bploc with properties {name:"Backup"})
				on error
					set backuploc to ((bploc as string) & "Backup")
				end try
				set bploc2 to (backuploc as string) as alias
				set psps2 to (pspsstick as string) as alias
				tell application "Finder"
					duplicate every item of psps2 to bploc2 with replacing
				end tell
			end if

The thing is by removing the “Backup” foder, it returns an error that (path I just selected):Backup cannot be found. In this case, shouldn’t it try to create the folder first? For entertainment’s sake, I also tried switching the create folder, and use existing folder lines to see if it would work. For whatever reason, it appears to be ignoring the ‘on error’ handler…

The way you define the properties shouldn’t matter because they get coerced into tor proper data type during your set statements.

Its late and my brain is fuzzy so continue at your own risk LOL, ultimately backuploc is suppossed to contain the path to the folder right? The question is are we creating it or does it exist right? So why not do something like?

set bploc to (path to desktop) as Unicode text -- since I don't recall where bploc is coming from Im using my desktop
set backuploc to bploc & "Backup"
try
	tell application "Finder"
		make new folder at bploc with properties {name:"Backup"}
	end tell
end try