evaluating if a file is there before reading

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

evaluating if a file is there before reading

Post by magice » Thu Sep 09, 2010 7:30 pm

In the project i am currently working on, I initialize an array by reading from a txt file on start up. I use the following code

Code: Select all

on importText   
   global tDataLoad
   open file "downloader.ini" for read   
   read from file "downloader.ini" until eof   
   put it into tDataLoad   
   close file "downloader.ini"  
end importText
That works fine. But In the event that the "downloader.ini" file has been deleted I want to instead initialize the tDataLoad array from a default copy saved as a custom property of the stack. I have the custom property saved as cBlankINI of stack "downloader". That part works fine. Where I am having trouble, is in the evaluation of the existence of file "downloader.ini" what would be the proper way to determine if the file exists before reading from it?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: evaluating if a file is there before reading

Post by bn » Thu Sep 09, 2010 7:54 pm

Magice,
look at "there is" in the dictionary. I tells you if the file you want to look at really exists. You have to give it the correct path and filename.

regards
Bernd

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

Re: evaluating if a file is there before reading

Post by Klaus » Fri Sep 10, 2010 11:23 am

Hi magice,

yep, "if there is a file xyz" will do the job.

Hint!
To save a lot of typing get used to the URL syntax like this:

Code: Select all

on importText   
   global tDataLoad
  if there is a file "downloader.ini" then
    put url("file:" & "downloader-ini") into tDataLoad
    ## See, just ONE line :-)
   else
    ## do your else thing
  end if   
end importText
Best

Klaus

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: evaluating if a file is there before reading

Post by magice » Fri Sep 10, 2010 2:02 pm

Thank you Bernd, that was exactly what I needed to know. And thank you Klaus for the extra hint. Unfortunately I already did the extra typing.

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

Re: evaluating if a file is there before reading

Post by Klaus » Fri Sep 10, 2010 3:35 pm

Hi magice,

my hint will also work with new scripts :D

Post Reply