FileInfo function (was: Getting the type code of a file under OSX)

FlexibleLearning at aol.com FlexibleLearning at aol.com
Sun Jul 31 08:33:22 EDT 2005


Another one for the Scripter's Scrapbook! On the shoulders of Ken's fine  
stsFileInfo that retrieves file and folder attributes in an array format, I have  
below refined it to include any required attribute/s all in one command, in 
any  combination and in any any order. The returned values are presented in the 
 same order as requested.
 
Example:
 
put  flcFileInfo(myFilePath,"CreatorType","size","modified","permissions")
 
It also handles the change in the detailedFiles function, where  an 
unsupported item is now 0 instead of empty (for example the 'BackUp Date'  under 
Windows). It is, however, backwards compatibile with the earlier  detailedFiles 
version.

Watch out for line-wrapping!
 
Optimising welcome.
 
/H
The Scripter's Scrapbook
www.FlexibleLearning/ssbk.htm
 
 

--| FUNCTION: flcFileInfo
--|
--| Author: Hugh Senior (based on  stsFileInfo by Ken Ray)
--| Version: 1.0
--| Created: 30-JUL-05
--|  Last Mod: --
--| Requires: Self-contained function handler for Revolution and  MetaCard
--| Summary: Retrieves the requested file/folder information, one  per line 
in the same order as requested
--| Format: flcFileInfo(param1  [,param2,param3...])
--| Parameters:
--|    param 1:  file/folder path
--|    param 2 to  n:
--|        EITHER: "full" or omitted  for a full, tab-delimted list
--|         OR: Any combination of these 12 specific attributes in any  
order...
--|                FilePath, FileName, DataSize, ResourceSize, Size, Created, 
Modified, Accessed,  BackUp, OwnerID, GroupID, Permissions, CreatorType
--|
--|  Examples:
--|    get flcFileInfo(myFilePath) - Returns an  itemised list
--|    get flcFileInfo(myFilePath,"full") -  Returns an itemised list
--|    get  flcFileInfo(myFilePath,"FilePath","created","modified","accessed")
--|     get  flcFileInfo(myFilePath,"DataSize","ResourceSize","size")
--|     get  
flcFileInfo(myFilePath,"CreatorType","size","modified","permissions")
--|
--|  Error handling:
--|    File/folder not found - The returned  value is empty
--!    Attribute not supported - The returned  attribute value is "not 
supported"
--|    Attribute not  recognized - The returned attribute value is "not  
recognized"

----------------------------------------------------------------------

function  flcFileInfo
put param(1) into pFilePath
put  
"FilePath,FileName,DataSize,ResourceSize,Size,Created,Modified,Accessed,BackUp,OwnerID,GroupID,Permissions,CreatorType"  into tAttributeList
put there is a file pFilePath into Fflag
if (NOT Fflag AND there is NOT a folder pFilePath) then return ""
set  the itemDel to "/"
put urlEncode(last item of pFilePath) into  tItem
delete last item of pFilePath
put the directory into  tOldDir
set the directory to pFilePath  
if Fflag then  put the detailed files into tDetailedList
else put the detailed  folders into tDetailedList  
set the directory to  tOldDir
set the itemDel to ","
put  lineOffset(cr&tItem&",",cr&tDetailedList) into tLine
put  line tLine of tDetailedList into tDetailedInfo
replace "," with tab in  tDetailedInfo
set the itemDel to tab
put tDetailedInfo into  tFileA["full"]
put urlDecode(item 1 of tDetailedInfo) into  tFileA["fileName"]
put pFilePath&"/"&tFileA["fileName"] into  tFileA["filePath"]
put (item 2 of tDetailedInfo) into  tFileA["DataSize"]
if the platform is "MacOS" then put (item 3 of  tDetailedInfo) into 
tFileA["ResourceSize"]
else put "" into  tFileA["ResourceSize"]
put (item 2 of tDetailedInfo) + (item 3 of  tDetailedInfo) into tFileA["size"]
put  "Created,Modified,Accessed,BackUp" into tDates
replace "," with tab in  tDates
put the twelvehourtime into tTHT
set the  twelvehourtime to FALSE
repeat with x = 4 to 7
put item x of tDetailedInfo into tSecs
if tSecs=0 then put  "not supported" into tSecs
if tSecs is a number then  convert tSecs to long system date and long time
put tSecs  into tFileA[(item x-3 of tDates)]
end repeat
set the  twelvehourtime to tTHT
if (item 8 of tDetailedInfo) <>0 then put  (item 8 of tDetailedInfo) into 
tFileA["OwnerID"]
else put "" into  tFileA["OwnerID"]
if (item 9 of tDetailedInfo) <>0 then put  (item 9 of tDetailedInfo) into 
tFileA["GroupID"]
else put "" into  tFileA["GroupID"]
put (item 10 of tDetailedInfo) && "octal"  &","&&baseConvert(item 10 of 
tDetailedInfo,8,10)&&"decimal"  into tFileA["permissions"] # octal && decimal
put (item 11 of  tDetailedInfo) into tFileA["creatorType"]
set the itemDel to  COMMA
if (the paramCount=1) OR (param(2)="full") OR (param(2)="")  then
repeat with n=1 to num of items of  tAttributeList
put item n of tAttributeList  into L
if tFileA[L]="" then put "not  supported" into tFileA[L]
put L &TAB&  tFileA[L] into line (num of lines of tStdOut)+1 of tStdOut
end repeat
else
repeat with n=2 to the  paramCount
get  tFileA[param(n)]
if param(n) is not in  tAttributeList then put "not recognized" into  it
else if it="" then put "not supported" into  it
put it into line (num of lines of  tStdOut)+1 of tStdOut
end repeat
end  if
if tStdOut="" then return "not recognized"
else return  tStdOut
end flcFileInfo


SOURCE: Hugh Senior, FLCo
 



More information about the use-livecode mailing list