Page 1 of 1

How do I check wheter a file exists or not?

Posted: Wed Nov 04, 2015 9:40 am
by AgentItay
I tried to use if exists(special folder path blabla and filename) but it doesn't work.

Re: How do I check wheter a file exists or not?

Posted: Wed Nov 04, 2015 9:52 am
by dave.kilroy
Hi, one of the things is to make sure LiveCode is looking in the correct folder - also - use the "is there a file" format...

Code: Select all

   put  "testfile.txt" into tFile
   set itemdel to slash
   put item 1 to -2 of the effective filename of the current stack & slash & tFile into tPath
   if there is a file tPath then
      answer "aye"
   else
      answer "nay"
   end if
EDIT: the exists() function is only for controls, not files or folders...

Re: How do I check wheter a file exists or not?

Posted: Wed Nov 04, 2015 10:06 am
by AgentItay
Thanks Dave :D
It's working perfectly, but I don't really understand what this means,

Code: Select all

put item 1 to -2 of the effective filename of the current stack & slash & tFile into tPath
can you explain what it means please?

Re: How do I check wheter a file exists or not?

Posted: Wed Nov 04, 2015 10:18 am
by dave.kilroy
Hi - that line was just to set things up for the sample code...

Getting the effective filename of the current stack will produce the name and location of the stack you are working on - for example it might be something like "/Users/dave/Dropbox/LC/some-project/testStack.livecode"

Setting the itemdel (short for item delimiter) to slash allows us to split up the effective filename by slash

Getting item 1 to -2 (getting all the items up to the second-last one) gives us the location of the folder holding the stack we are working on

Then after that all we do is add a slash to the end of the resulting string and after that add the file name we are looking for

Please note that the sample code I sent you was for checking files in the same folder as the current stack - you would have to set up things to look in a different folder if you wanted to check files elsewhere...

Re: How do I check wheter a file exists or not?

Posted: Wed Nov 04, 2015 10:26 am
by AgentItay
I think I get it now.
And what if I want to look for the file in the desktop, or my documents, how do I do that?
EDIT: And also, how do I create a file in the same location as the exported .exe file?

Re: How do I check wheter a file exists or not?

Posted: Wed Nov 04, 2015 2:24 pm
by Klaus
AgentItay wrote:And what if I want to look for the file in the desktop, or my documents, how do I do that?
Look up "specialfolderpath" in the dictionary, that is exactly what you are looking for!
AgentItay wrote:EDIT: And also, how do I create a file in the same location as the exported .exe file?
Dave already answered (and explained the script) in his last posting(s)... 8)

Re: How do I check wheter a file exists or not?

Posted: Thu Nov 05, 2015 8:28 am
by AgentItay
Right, managed to do it :D
Thank you very much.