if a file exists

Hi,

I am looking to find a code that I can find out if a file exists or not. I found several solution but none had any specific location (the ones I found they always say ‘browse to the folder’ or something like that).

I do not want to use “do shell script”. I have a file in my documents called “About Stacks.pdf” I REALLY hope someone would be able to give me a solution.



tell application "Finder"
	
	if exists file "Users:administrator:Documents:About Stacks.pdf" then
		display dialog "found it"
	else
		display dialog "not there"
	end if
end tell

Thanks in advance!

Riz

Model: Mac Pro
Browser: Internet Explorer 6.0
Operating System: Mac OS X (10.6)

Hi,

what’s wrong with your code? It’s the normal way.
There is another without the Finder


set stackFile to ((path to documents folder as text) & "About Stacks.pdf")

try
	stackFile as alias
	display dialog "found it"
on error
	display dialog "not there"
end try

It's the normal way. There is another without the Finder

I didn’t know about it. Cool!

Hi StefanK,

Thanks for your reply. Sorry for the late follow up (I was on the road). I tried the script you sent me, but it is givng an error ("Expected end of line, etc. but found unknown token).

I was wondering, if we could troubleshoot with the initial script that I had posted. I am just not sure what’s wrong with my code.

I also tried the code below and it says Syntax error and that the file was not found. I know for sure that the file IS there.


tell application "Finder"
	
	set myfile to "Users:administrator:Documents:About Stacks.pdf" as alias
	if exists file myfile then
		display dialog "found it"
	else
		display dialog "not there"
	end if
end tell



Once again, I really appriciate your help!

Thanks,

Riz

I suspect that you’ve run my script in a large application tell block,
which can cause the error (terminology clash)

In your script replace


 set myfile to "Users:administrator:Documents:About Stacks.pdf" as alias

with


set myfile to ((path to documents folder as text) & "About Stacks.pdf")

path to documents folder is the alias to the documents folder of the current user

A HFS path must start with a disk name (Users:. does not work)

Thanks a LOT StefanK!

That worked just fine. ONE last question about this, how would I go about sub folders? Say I have a folder called “Test Folder” under “Documents”.

I tried few different ways, but nothing is working out for me!

Once again, I appriciate your prompt reply!

Thanks,

Riz

same way, either


set documentsFolder to path to documents folder

tell application "Finder"
	if exists folder "Test Folder" of documentsFolder then
		display dialog "found it"
	else
		display dialog "not there"
	end if
end tell

or


set documentsFolder to path to documents folder

try
	((documentsFolder as text) & "Test Folder") as alias
	display dialog "found it"
on error
	display dialog "not there"
end try

Sorry for not being clear.

So I wanted to expand your first code to find the same file in subfolder of documents.

So the file location is Documents:Test Folder:About Stacks.pdf

The code you sent me earlier worked when I look for the file under Documents.

Thanks!
Riz


set testFolder to (path to documents folder as text) & "Test Folder:"

try
	(testFolder & "About Stacks.pdf") as alias
	display dialog "found it"
on error
	display dialog "not there"
end try

For the above script, I get an error “Expected end of line, etc. but found unknown token”
Any idea why I am getting that error?

I went to just AppleScript Editor and paste your code … any reason why it might be failing?

If there’s nothing else it must work, unless you run Snow Leopard and Apple has changes the path to commands

I think you must be giving the wrong path.

Select the file/folder in Finder and run this script from your Script Editor.
It will copy the path to the clipboard so that you can paste it.

–I know the script is poorly written but i wrote it a long time ago.

set thepath to path to users folder as string

set AppleScript's text item delimiters to ":"
set thepath to first text item of thepath
tell application "Finder"
	repeat with aitem in (get selection)
		set r to POSIX path of (aitem as text)
		set AppleScript's text item delimiters to "/"
		
		set z to the text items of r
		set AppleScript's text item delimiters to ":"
		set z1 to text items of z as string
		
		if second text item of z1 is "Volumes" then
			set z2 to "\"" & text 10 thru -1 of z1 & "\""
			set the clipboard to z2
		else
			
			set the clipboard to "\"" & thepath & ":" & (text 2 thru -1 of z1) & "\""
			--if the file or folder is from users directory
		end if
	end repeat
end tell

thanks for the post Chris!

It would be nice if i could find something ‘simplier’. If not, whatever works, I will stick with that.

Thanks,

Riz

Thanks a lot guys! I really appriciate your help!

I finally found something that works out pretty well for what I needed…


set Backup to "Backup1.bbb"

tell application "Finder"
	if exists file Backup of folder "BlackBerry Backups" of folder "Documents" of home then
		display dialog "found it"
	else
		display dialog "not there"
	end if
end tell


which OS are you using?
This does not work in Leopard.

Hi Chris,

I tried the same code on my MacBook Pro which is running 10.5.7 and that worked. I also tried the code on Mac Pro running 10.6.1. They both worked for me.

Thanks,

Riz

You can use this Applescript Handler:

on checkFileExists(FileToCheckString)
	try
		set thePath to quoted form of (POSIX path of FileToCheckString)
		do shell script "test -f " & thePath
		return true
	on error
		return false
	end try
end checkFileExists

Greets from
TMA