Need help! Checking if folder exists before creation...

Hi,

I writing the following script but it is not working logically:

set hd_path to (path to startup disk) as string
set thePath to (hd_path & "Users:admin:Public") as alias
set thePath2 to the thePath as string
set theName to "Docsu"
repeat
	if theName is in thePath2 then
		display dialog "enter a new name"
		
	else
		beep
		beep
		exit repeat
	end if
end repeat


if the name is in or is not in the destination i am receiving the same result!!

tk.

hi tonykhater,

this line: ‘if theName is in thePath2 then’ does not work

you can use this instead:

set hd_path to (path to startup disk) as string
set thePath to (hd_path & "Users:admin:Public") as alias
set thePath2 to the thePath as string
set theName to "Docsu"

repeat while true
	try
		get (alias (thePath2 & theName))
		-- file/folder exists
		set theName to text returned of (display dialog "enter a new name" default answer "")
	on error
		-- no file/folder of this name
		beep
		beep
		exit repeat
	end try
end repeat

D.

Shouldn’t “Users:admin:Public” end with a “:” if “Public” is a directory?

That said, I’d like to add that I prefer to use the Finder when dealing with files and folders.

set hd_path to (path to startup disk) as string
tell application "Finder"
	set thePath to (hd_path & "Users:admin:Public:")
	set theName to "Docsu"
	if exists folder (thePath) then
		beep
		-- folder exists
		if exists file (thePath & theName) then
			-- File exists
			beep
			beep
			beep
		else
			-- File does not exist
		end if
	else
		-- folder does not exist
	end if
end tell

If you are working off a server (shared volume), it is best to update the folder first.

Or to eliminate some lines, just set your path to the Public folder

tell application "Finder"
	set hd_path to (path to public folder) as string
	set theName to "Docsu:"
	if folder (thePath & theName) exists then
		display dialog "Folder exists"
	else
		display dialog "Folder does not exist"
	end if
end tell

or to your “Home” folder

	set hd_path to (path to home folder) as string 

or to your “Desktop” folder

	set hd_path to (path to desktop folder) as string 

or to your “Document” folder

	set hd_path to (path to documents folder) as string 

…Just to name a few :slight_smile: