Detailed File Info

Bob Sneidar bobsneidar at iotecdigital.com
Fri Jan 24 17:26:14 EST 2025


Hi all. 

I’ve been wanting to write a function for getting the size and the last modified datetime of one specific file. I finally figured out how to do it. Here’s the function. Enjoy. 

function getDetailedFileInfo pFilePath
   -- new method use shell
   if the platform contains "win" then   
      replace slash with backslash in pFilePath
      put "dir /T:W "  & quote & pFilePath & quote into tShellCommand
      put shell(tShellCommand) into tResult
      put word 1 of line 6 of tResult into tDate
      put formatDate(tDate, "sql date") into tDate
      put word 2 of line 6 of tResult into tTime
      put formatTime(tTime, "sql time") into tTime
      put tDate && tTime into tDateTime
      put word 4 of line 6 of tResult into tFileSize
      replace comma with empty in tFileSize
      put tFileSize *1000 into tFileSize
   else
      put "getFileInfo -m" && quote & pFilePath & quote into tShellCommand
      put shell(tShellCommand) into tResult
      put formatDate(word 1 of tResult, "sql date") && \
            formatTime(word 2 of tResult, "sql time") into tDateTime
      put "echo "& quote & "$(( $(stat -f'%z' filename)))" & quote into tShellCommand
      replace fileName with "'" & pFilePath & "'" in tShellCommand
      put word 1 of shell(tShellCommand) into tFileSize
   end if
   
   put tFilePath & comma & tFileSize & comma & tDateTime & cr after tNewDetailedFiles
   return tNewDetailedFiles
end getDetailedFileInfo

FUNCTION formatDate theDate, theFormat
   /*
   Accepts any valid date for the first parameter. If not a valid date, it simply returns
   what was passed. Second parameter can be any of the following:
   sql date: date in the yyyy-mm-dd format
   short date, abbreviated date, internet date, long date: LC versions of the same
   julian date: Julian number based on (I believe) Jacques formula
   standard date: The date in the form of "mm/dd/yyyy"
   */
   
   put theDate into tSavedDate
   put the itemdelimiter into theOldDelim
   set the itemdelimiter to "-"
   
   IF the length of item 1 of theDate = 4 AND \
         the number of items of theDate = 3 AND \
         item 1 of theDate is a number AND \
         item 2 of theDate is a number AND \
         item 3 of theDate is a number THEN
      put item 2 of theDate & "/" & \
            item 3 of theDate & "/" & \
            item 1 of theDate into theDate
   END IF
   
   -- replace "." with "/" in theDate
   convert theDate to dateitems
   set the itemdelimiter to theOldDelim
   
   --   if the number of items of theDate <> 7 then
   --      answer "'" & theDate & "' is not a valid date format!"
   --      return tSavedDate
   --   end if
   
   SWITCH word 1 of theFormat
      CASE "sql"
         /*
         put item 1 of theDate & "-" & \
               format("%02d",item 2 of theDate) & "-" & \
               format("%02d",item 3 of theDate) into theDate
         */
         put format("%s-%02d-%02d", item 1 of theDate, item 2 of theDate, \
               item 3 of theDate) into theDate
         break
      CASE "short"
         convert theDate from dateitems to short date
         break
      CASE "abbreviated"
         convert theDate from dateitems to abbreviated date
         break
      CASE "abbr"
         convert theDate from dateitems to abbreviated date
         break
      CASE "internet"
         convert theDate from dateitems to internet date
         break
      CASE "long"
         convert theDate from dateitems to long date
         break
      CASE "julian"
         put the date into theDate
         convert theDate to dateItems
         IF  ((item 2 of theDate = 1) OR (item 2 of theDate = 2)) THEN
            put 1 into theDay
         ELSE
            put 0 into theDay
         END IF
         put item 1 of theDate + 4800 - theDay into theYear
         put item 2 of theDate + (12 * theDay) - 3 into theMonth
         put item 3 of theDate + \
               ((153 * theMonth + 2) div 5) + \
               (365 * theYear) + \
               (theYear div 4) - \
               (theYear div 100) + \
               (theYear div 400) - \
               32045 into theDate
         break
      case "standard"
         put format("%02d/%02d/%04d", item 2 of theDate, item 3 of theDate, \
               item 1 of theDate) into theDate
         break
      default 
         put tSavedDate into theDate
   END SWITCH
   
   return theDate
END formatDate

FUNCTION formatTime theTime, tOutFormat, tInFormat
   /*
   formatTime accepts any valid time and returns the form of the time specified in the second parameter.
   The valid formats are:
   sql time: hh:mm:ss (Note: combining sql date from the formatDate() function with the
   sql time will produce a valid SQL date time type).
   short time: LC short time format
   abbreviated time: LC abbr time format (same as short time)
   long time: LC long time format
   seconds: the number of seconds since the prior midnight
   military: the military time 00:00 - 23:59
   */
   
   IF theTime is empty THEN return empty
   if tOutFormat is empty then return theTime
   if tInFormat is empty then put "short time" into tInFormat
   
   if tOutFormat is among the items of "short time,long time,abbreviated time,seconds" then
      put "convert theTime from " & tInFormat & " to " & tOutFormat into tCommand
      do tCommand
      return theTime
   end if
   
   -- strip leading and trailing spaces
   put word 1 to -1 of theTime into theTime
   
   -- replace spaces with colons
   replace space with colon in theTime
   replace ":AM" with " AM" in theTime
   replace ":PM" with " PM" in theTime
   
   -- convert integer to time
   if theTime is an integer then \
         put theTime & ":" & "00" into theTime
   
   -- determine if seconds are in theTime
   set the itemDelimiter to ":"
   put the number of items of theTime >2 into tUseSeconds
   set the itemDelimiter to ","
   
   set the numberformat to "00"
   SWITCH word 1 of tOutFormat
      CASE "sql"
         convert theTime to dateitems
         put (item 4 of theTime +0) & ":" & \
               (item 5 of theTime +0) & ":" & \
               (item 6 of theTime +0) into theTime
         break
      CASE "short"
         convert theTime to short time
         break
      CASE "abbreviated"
         convert theTime to abbreviated time
         break
      CASE "long"
         convert theTime to long time
         break
      CASE "seconds"
         convert theTime to seconds
         break
      CASE "military"
         put theTime into tMilitaryTime
         convert tMilitaryTime to dateitems
         put 0 + item 4 of tMilitaryTime & ":" & 0 + item 5 of tMilitaryTime into theTime
         if tUseSeconds is true then \
               put ":" & 0 + item 6 of tMilitaryTime after theTime
         break
   END SWITCH
   
   return theTime
END formatTime

Bob S


More information about the use-livecode mailing list