{"id":19178,"date":"2016-07-13T15:12:32","date_gmt":"2016-07-13T15:12:32","guid":{"rendered":"https:\/\/legacy.livecode.com\/?p=19178"},"modified":"2016-07-13T15:12:32","modified_gmt":"2016-07-13T15:12:32","slug":"faff-free-files-function","status":"publish","type":"post","link":"https:\/\/legacy.livecode.com\/faff-free-files-function\/","title":{"rendered":"Faff-free files function"},"content":{"rendered":"<p>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.<\/p>\n<p>For time immemorial, the <strong>files()<\/strong> and\u00a0<strong>folders()<\/strong> functions in LiveCode Script have operated only on the current folder (also known as the current working directory), which is accessed using the\u00a0<strong>defaultFolder<\/strong> global property.<\/p>\n<p>Unfortunately, setting the\u00a0<strong>defaultFolder<\/strong> can fail for many reasons. \u00a0For example, it might fail if the requested directory\u00a0doesn&#8217;t actually exist,\u00a0or if you don&#8217;t have\u00a0permissions to read it. \u00a0A reasonably robust implementation of a\u00a0<strong>ListStackFilesInFolder()<\/strong> function that lists the stack files in directory might look something like this:<!--more--><\/p>\n<p>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.<\/p>\n<p>For time immemorial, the\u00a0<strong>files()<\/strong>\u00a0and\u00a0<strong>folders()<\/strong>\u00a0functions in LiveCode Script have operated only on the current folder (also known as the current working directory), which is accessed using the\u00a0<strong>defaultFolder<\/strong>\u00a0global property.<\/p>\n<p>Unfortunately, setting the\u00a0<strong>defaultFolder<\/strong>\u00a0can fail for many reasons. \u00a0For example, it might fail if the requested directory\u00a0doesn&#8217;t actually exist,\u00a0or if you don&#8217;t have\u00a0permissions to read it. \u00a0A reasonably robust implementation of a\u00a0<strong>ListStackFilesInFolder()<\/strong>\u00a0function that lists the stack files in directory might look something like this:<\/p>\n<pre><code class=\"livecode\">function ListStackFilesInFolder pFolder\r\n   local tOldFolder, tFiles\r\n\r\n   put the defaultFolder into tOldFolder\r\n   set the defaultFolder to pFolder\r\n   if the result is not empty then\r\n      throw \"Something went wrong!\"\r\n   end if\r\n\r\n   put files() into tFiles\r\n   if the result is not empty then\r\n      throw \"Something else went wrong!\"\r\n   end if\r\n   filter tFiles with \".livecode\"\r\n\r\n   set the defaultFolder to tOldFolder\r\n   if the result is not empty then\r\n      throw \"Something went very very wrong!\"\r\n   end if\r\n\r\n   return tFiles\r\nfunction<\/code><\/pre>\n<p>This is long-winded\u00a0even 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\u00a0<strong>defaultFolder<\/strong> back to its original value fails for some reason, there&#8217;s nothing that can be done to recover from it. Whoops.<\/p>\n<p>It would be much more sensible to just tell the\u00a0<strong>files()<\/strong> function which directory you want it to look at, and leave the\u00a0<strong>defaultFolder<\/strong> untouched.<\/p>\n<pre><code class=\"livecode\">function ListStackFilesInFolder pFolder\r\n   local tFiles\r\n\r\n   put files(pFolder) into tFiles\r\n   if the result is not empty then\r\n      throw \"Something else went wrong!\"\r\n   end if\r\n   filter tFiles with \".livecode\"\r\n\r\n   return tFiles\r\nfunction<\/code><\/pre>\n<p>Much more concise! And don&#8217;t worry; if you don&#8217;t pass any argument to the\u00a0<strong>files()<\/strong> function, it still looks at the\u00a0<strong>defaultFolder<\/strong> as before.<\/p>\n<p>In the next developer preview release of LiveCode 8.1, you will be able to do just that, and the\u00a0<strong>folders()<\/strong> function has been\u00a0upgraded to match. \u00a0The core LiveCode dev team are looking forward to being able to remove lots of now-unnecessary <strong>defaultFolder<\/strong>-setting\u00a0from all our\u00a0LiveCode programs and the IDE.<\/p>\n<h4>Quick quiz<\/h4>\n<ol>\n<li>The list returned by the\u00a0<strong>folders()<\/strong> function includes the filename &#8220;..&#8221;. \u00a0What is this, and why should you almost always\u00a0filter it out before using the list?<\/li>\n<li>Find\u00a0an example of a series of actions that would make\u00a0switching back to the original <strong>defaultFolder<\/strong>\u00a0fail.<\/li>\n<li>It&#8217;s common to process files in a folder by looping over\u00a0<strong>the lines of files()<\/strong>. Under what circumstances can this go wrong, and what can you do about it?<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u00a0folders() functions in LiveCode Script have operated only on the current folder (also known as the current working directory), which is accessed using the\u00a0defaultFolder global property. Unfortunately,<\/p>\n","protected":false},"author":27,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"om_disable_all_campaigns":false,"footnotes":""},"categories":[45],"tags":[192],"class_list":["post-19178","post","type-post","status-publish","format-standard","hentry","category-blog","tag-livecode-8"],"acf":[],"_links":{"self":[{"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/posts\/19178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/comments?post=19178"}],"version-history":[{"count":5,"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/posts\/19178\/revisions"}],"predecessor-version":[{"id":19219,"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/posts\/19178\/revisions\/19219"}],"wp:attachment":[{"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/media?parent=19178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/categories?post=19178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/tags?post=19178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}