Page 1 of 1

get detailed file info

Posted: Sun Feb 12, 2017 12:25 pm
by shaosean
Is there a function to get the details of a single file, based on the file path, or do we need to find the file in the output from detailed files?

Re: get detailed file info

Posted: Sun Feb 12, 2017 1:16 pm
by jmburnod
Hi shaosean,
As far i know you have to do your own
I use a simple lineoffset() to get informations about one file
Best regards
Jean-Marc

Re: get detailed file info

Posted: Sun Feb 12, 2017 5:04 pm
by shaosean
ahh.. I didn't even think of the lineOffset, I've been using a loop.. Thanks for the tip :)

Re: get detailed file info

Posted: Sun Feb 12, 2017 6:30 pm
by FourthWorld
Here's the function I use, which may save you some time with some of the tedious error-checking. Kinda handy, as it lets you get either one specified file attribute or an array with all of them:

Code: Select all

function fwFileInfo pFile, pAttribute
   -- Returns file attributes for the file specified in pFile.
   -- If pAttribute is empty, returns an array of all 11 elements
   -- LiveCode's "detailed files" supports: Name,Size,ResSize,
   -- CreationDate,ModifiedDate,AccessDate,BackupDate,UserOwner,
   -- GroupOwner,Permissions,CreatorCode.
   -- If any of those key names are passed as pAttribute, only 
   -- the value corresponding to that attribute is returned.
   ---------------------------------------------------------
   -- File exists?
   if there is not a file pFile then
      return "No such file: "& \
            quote& pFile &quote for error
   end if
   --
   put pFile into tPath
   set the itemdel to "/"
   delete last item of tPath
   put the defaultfolder into tSaveDefaultFolder
   set the defaultFolder to tPath
   -- Can't set folder (permissions)?
   if the result is not empty then
      return "Couldn't access folder: "& \
            quote& tPath &quote& \
            " ("& sysError()& ")" for error
   end if
   put the detailed files into tFiles
   set the defaultfolder to tSaveDefaultFolder
   --
   put last item of pFile into tFileName
   put lineoffset(cr& urlEncode(tFileName) &comma, cr& tFiles) into tLineNum
   put line tLineNum of tFiles into tFileAttributes
   --
   put "Name,Size,ResSize,CreationDate,ModifiedDate,AccessDate,BackupDate," & \
         "UserOwner,GroupOwner,Permissions,CreatorCode" into tAttribNames
   set the itemdel to comma
   if pAttribute is "Name" then
      return urlDecode(item 1 of tFileAttributes)
   else if pAttribute is not empty then
      -- Return single attribute:
      return item (itemoffset(pAttribute, tAttribNames)) of tFileAttributes
   else
      -- Return array of attributes:
      put 0 into i
      put empty into tAttribsA
      repeat for each item tAttrib in tAttribNames
         add 1 to i
         put item i of tFileAttributes into tAttribsA[tAttrib]
      end repeat
      -- Normalize name form:
      put urlDecode( tAttribsA["Name"]) into tAttribsA["Name"] 
      --
      return tAttribsA
   end if
end fwFileInfo