get detailed file info

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

get detailed file info

Post by shaosean » Sun Feb 12, 2017 12:25 pm

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?

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: get detailed file info

Post by jmburnod » Sun Feb 12, 2017 1:16 pm

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
https://alternatic.ch

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: get detailed file info

Post by shaosean » Sun Feb 12, 2017 5:04 pm

ahh.. I didn't even think of the lineOffset, I've been using a loop.. Thanks for the tip :)

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10045
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: get detailed file info

Post by FourthWorld » Sun Feb 12, 2017 6:30 pm

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply