Detailed File Information for One File

Richard Gaskin ambassador at fourthworld.com
Fri Apr 27 16:39:17 EDT 2018


Bob Sneidar wrote:

> Not to beat a severely wounded horse, but one reason for implementing
> a detailed file function (and probably why it hasn't been tackled) is
> that getting the detailed file information from a command shell is
> decidedly  NOT straightforward.

Shell is great for things we can't do in LiveCode Script yet may not 
warrant writing special C-based API interfaces for in LC Builder.

But in addition to the complexity of attempting to rely on such 
interprocess communication across platforms, it also carries a certain 
impact on memory and performance.

"Why run one process when you can run two at twice the price!" :)

In the enhancement request I cited earlier:
https://quality.livecode.com/show_bug.cgi?id=20384

...Trevor raised a good point about instances where apparently macOS 
sometimes disallows access to a folder even while allowing access to 
specific files within it.  It's hard for me to see how that would not be 
a configuration error on any other system, but I trust Apple has a good 
reason for that.  And in such cases, we're hosed with any current LC 
Script solution that needs access to the whole folder's content.

But for the other 99.9% of cases, using a scripted handler reduces the 
task to a one-liner, here made even more convenient by allowing either 
the detailed info returned in its LC-native comma-delimited format or as 
an array if preferred:


on mouseUp
    put specialFolderPath("desktop")&"/test/Thisisafilename1000" into tPath
    put fwFileInfo( tPath, "Array") into tA
    if the result is not empty then
       answer the result
    else
       put the keys of tA
    end if
end mouseUp


function fwFileInfo pFile, pArrayFlag
    -- Returns info for the file specified in pFile.
    -- By default, returns the data in comma-delimited
    -- form in the order specified by "detailed files".
    -- With any value in the optional pArrayFlag arg
    -- it will return an array with descriptive keys.
    --
    if there is not a file pFile then
       return "No such file: "& pFile for error
    end if
    set the itemdel to "/"
    put urlEncode(last item of pFile) into tFileName
    put item 1 to -2 of pFile into tPath
    put files(tPath, "detailed") into tFiles
    if the result is not empty then
       return "Error getting file info: "& the result & \
             " ("&sysError()&")" for error
    end if
    put lineoffset(cr& tFileName&",", cr&tFiles) into tLineNum
    put line tLineNum of tFiles into tInfo
    if pArrayFlag is empty then
       return tInfo for value
    else
       set the itemdel to ","
       put 0 into i
       put 
"name,size,resourceSize,dateCreated,dataModified,dateAccessed,"& \
             "dateBackedUp,owner,group,permissions,MacType" into tFields
       repeat for each item tItem in tInfo
          add 1 to i
          if i = 1 then put urlDecode(tItem) into tItem
          put tItem into tA[item i of tFields]
       end repeat
       return tA for value
    end if
end fwFileInfo


Performance isn't bad (times shown in ms on Ubuntu 14.04 with Ext4 file 
system) - each test was done with the slower form of the function call 
that uses the array option:

Folder with 20 files:     0.000072
Folder with 5,000 files:  0.024766
Folder with 10,000 files: 0.042299

I'll leave it as an exercise for the reader to compare with benchmarks 
for obtaining the same info via shell.  But at a fraction of a 
millisecond with LC Script even in an insanely large directory, I'll 
wager the time to write the test would exceed any time saved over one's 
lifetime. :)

-- 
  Richard Gaskin
  Fourth World Systems
  Software Design and Development for the Desktop, Mobile, and the Web
  ____________________________________________________________________
  Ambassador at FourthWorld.com                http://www.FourthWorld.com




More information about the Use-livecode mailing list