How do I check wheter a file exists or not?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
AgentItay
Posts: 60
Joined: Mon Aug 24, 2015 8:54 pm

How do I check wheter a file exists or not?

Post by AgentItay » Wed Nov 04, 2015 9:40 am

I tried to use if exists(special folder path blabla and filename) but it doesn't work.

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

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

Post by dave.kilroy » Wed Nov 04, 2015 9:52 am

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...
"...this is not the code you are looking for..."

AgentItay
Posts: 60
Joined: Mon Aug 24, 2015 8:54 pm

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

Post by AgentItay » Wed Nov 04, 2015 10:06 am

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?

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

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

Post by dave.kilroy » Wed Nov 04, 2015 10:18 am

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...
"...this is not the code you are looking for..."

AgentItay
Posts: 60
Joined: Mon Aug 24, 2015 8:54 pm

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

Post by AgentItay » Wed Nov 04, 2015 10:26 am

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?

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

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

Post by Klaus » Wed Nov 04, 2015 2:24 pm

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)

AgentItay
Posts: 60
Joined: Mon Aug 24, 2015 8:54 pm

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

Post by AgentItay » Thu Nov 05, 2015 8:28 am

Right, managed to do it :D
Thank you very much.

Post Reply