Does anyone have a snippet of code to test if a folder exists?

Does anyone have a snippet of code to test if a folder exists? I have tried writing this, but cannot get it to work. I used “if (foldertest exists) then” and “if not(foldertest exists)”, but I cannot prove it it is true or false with these statments.

Many Thanks
Sean Balfe :?:

This might work.

tell application "Finder"
	if exists folder "path:to:folder:" then
		-- it exists, do something
	else
		--it doesn't exist, do something else
	end if
end tell

– Rob

You can also bypass the Finder by using this code (which will work for files as well as folders):

f_exists("path:to:folder:")

on f_exists(the_path)
	try
		get the_path as alias
		return true
	on error
		return false
	end try
end f_exists

Jon

Thank you both so much, last one worked.

Sean Balfe

Here’s a handler similar to Jon’s that does some additional parameter-checking. Got this from the applescript-users mailing list:


on testPathExists(inputPath)
	-- version 1.5
	-- from Richard Morton, on applescript-users@lists.apple.com
	-- public domain, of course. :-)
	-- gets somewhat slower as nested-depth level goes over 10 nested folders
	if inputPath is not equal to "" then try
		get alias (inputPath as string) -- just in case inputPath was not string
		return true
	end try
	return false
end testPathExists