Faff-free files function

by Peter Brett on July 13, 2016 No comments

The next developer preview release of LiveCode 8.1 will contain a long-requested enhancement to make writing programs that work with files much easier.

For time immemorial, the files() and folders() functions in LiveCode Script have operated only on the current folder (also known as the current working directory), which is accessed using the defaultFolder global property.

Unfortunately, setting the defaultFolder can fail for many reasons.  For example, it might fail if the requested directory doesn’t actually exist, or if you don’t have permissions to read it.  A reasonably robust implementation of a ListStackFilesInFolder() function that lists the stack files in directory might look something like this:

function ListStackFilesInFolder pFolder
   local tOldFolder, tFiles

   put the defaultFolder into tOldFolder
   set the defaultFolder to pFolder
   if the result is not empty then
      throw "Something went wrong!"
   end if

   put files() into tFiles
   if the result is not empty then
      throw "Something else went wrong!"
   end if
   filter tFiles with ".livecode"

   set the defaultFolder to tOldFolder
   if the result is not empty then
      throw "Something went very very wrong!"
   end if

   return tFiles
function

This is long-winded even with silly error handling, and it is a bit of a pain to re-implement something like this every time you want to get a list of the files in a particular directory. To make matters worse, if setting the defaultFolder back to its original value fails for some reason, there’s nothing that can be done to recover from it. Whoops.

It would be much more sensible to just tell the files() function which directory you want it to look at, and leave the defaultFolder untouched.

function ListStackFilesInFolder pFolder
   local tFiles

   put files(pFolder) into tFiles
   if the result is not empty then
      throw "Something else went wrong!"
   end if
   filter tFiles with ".livecode"

   return tFiles
function

Much more concise! And don’t worry; if you don’t pass any argument to the files() function, it still looks at the defaultFolder as before.

In the next developer preview release of LiveCode 8.1, you will be able to do just that, and the folders() function has been upgraded to match.  The core LiveCode dev team are looking forward to being able to remove lots of now-unnecessary defaultFolder-setting from all our LiveCode programs and the IDE.

Quick quiz

  1. The list returned by the folders() function includes the filename “..”.  What is this, and why should you almost always filter it out before using the list?
  2. Find an example of a series of actions that would make switching back to the original defaultFolder fail.
  3. It’s common to process files in a folder by looping over the lines of files(). Under what circumstances can this go wrong, and what can you do about it?

 

Peter BrettFaff-free files function

Related Posts

Take a look at these posts

Join the conversation

*