Surprising result

Mark Smith mark at
Fri Jun 22 08:39:58 CDT 2007

Following a thread (some time ago) about getting the size of a file,  
I've finally got around to doing a little test.

One of the contentions was that it seemed inefficient to have to  
navigate to the folder containing the file, then get 'the detailed  
files', and parse the result just to find out how big a file is.

I've just been brushing up on shell commands (I'm on OS X), and found  
what would seem to be a viable alternative:

function getFileSize pFullPath
   get shell("ls -l" && pFullPath
   return word -5 of it
end getFileSize

This looks like it would be more efficient...

Currently, I have a library handler:

function getFileInfo pFileName
   set the itemDelimiter to "/"
   put put item 1 to -2 of pFileName into tFolderName
   put put item -1 of pFileName  into tFileName
   put the directory into tOldFolder
   set the directory to tFolderName
   put urlDecode(the detailed files) into tFileList

   get lineOffset(tFileName,tFileList)
   put line it of tFileList into tInfo

   put item 1 of tInfo into tFileInfo["fileName"]
   put item 2 of tInfo into tFileInfo["fileSize"]
   put item 3 of tInfo into tFileInfo["resourceSize"]
   put item 4 of tInfo into tFileInfo["created"]
   put item 5 of tInfo into tFileInfo["lastModified"]
   put item 6 of tInfo into tFileInfo["lastAccessed"]
   put item 7 of tInfo into tFileInfo["lastBackedUp"]
   put item 8 of tInfo into tFileInfo["Owner"]
   put item 9 of tInfo into tFileInfo["GroupOwner"]
   put item 10 of tInfo into tFileInfo["Permissions"]
   put item 11 of tInfo into tFileInfo["fileType"]

   set the directory to tOldFolder
   return tFileInfo
end getFileInfo

so to get the size of a file, I do this:

function getFileSize pFullPath
   put getFileInfo(pFullPath) into tInfo
   return tInfo["fileSize"
end getFileSize

So I set up a list of about 600 files, and used both versions of  
'getFileSize()' in separate loops.

The transcript version took just over two seconds, the shell version  
just under 40 seconds!

Of course, in many real applications, if you needed to get the sizes  
of hundreds of files, you could probably do it much more efficiently  
than this, since many, or all, might be in the same folder.

Anyway, I thought this was information worth sharing....



More information about the use-livecode mailing list