Page 1 of 1

How to get a filesize

Posted: Wed May 26, 2010 8:10 pm
by alemrantareq
Hi everybody, hope you all are fine. I need a little help. I need to get a filesize before starting my app. For example, I've a document file "test.doc" of 36 kb, & I need to check whether this doc file with 36 kb exists in my app's folder or not. if it not, it'll show an error msg. I can't cook the script from rev dictionary properly. Pls help me someone providing the right script. Replies ar most appreciated...... :lol:

Re: How to get a filesize

Posted: Wed May 26, 2010 9:37 pm
by Janschenkel
Create a new stack, drop a button onto it and set its script to:

Code: Select all

on mouseUp
   answer file "Select a file to determine the size"
   if the result is "Cancel" then exit mouseUp
   answer "File:" && it & return & "Size:" && FileSize(it)
end mouseUp

function FileSize pFilePath
   -- split the file path into the folder and url-encoded file name
   local tFolder, tEncodedFile
   set the itemDelimiter to slash
   put item 1 to -2 of pFilePath into tFolder
   put urlEncode(item -1 of pFilePath) into tEncodedFile
   set the itemDelimiter to comma
   -- now change the defaultfolder and get the detailed files information
   local tOldDefaultFolder, tDetailedFiles
   put the defaultFolder into tOldDefaultFolder
   set the defaultFolder to tFolder
   put the detailed files into tDetailedFiles
   set the defaultFolder to tOldDefaultFolder
   -- filter down the list to just the file we're interested in
   filter tDetailedFiles with (tEncodedFile & ",*")
   return (item 2 of tDetailedFiles + item 3 of tDetailedFiles)
end FileSize
Click the button, select a file, and it should tell you the file size in bytes (divide by 1024 to get the kilobytes)

Relevant entries in the DIctionary: defaultFolder, files, filter, itemDelimiter and urlEncode.
In case you're wondering why I add up item 2 and 3: to make sure it works cross-platform, including on MacOSX - where a file may actually consist of 2 parts (the data fork and resource fork).

HTH,

Jan Schenkel.

Re: How to get a filesize

Posted: Wed May 26, 2010 9:41 pm
by Dixie
Hi...

This should get you going...

Code: Select all

global gdefaultFolder

on preOpenStack
   set itemDel to "/"
   put item 1 to -2 of (the effective fileName of this stack) into gdefaultFolder
   set the defaultfolder to gdefaultFolder
   
   set itemDel to comma
   put gdefaultFolder & "/whateverTheFile" into theFileToStartWith
   put the detailed files into allTheFiles
   
   repeat with count = 1 to the number of lines of allTheFiles
      if item 1 of line count of allTheFiles = "whateverTheFile" then
         if platform() = "MacOS" then put (item 2 of line count of allTheFiles + item 3 of line count of allTheFiles) into theSize
         if platform() = "Win32" then put item 2 of line count of allTheFiles  into theSize
         exit repeat
      end if
   end repeat
   
   put theSize && "bytes"
end preOpenStack
This doesn't check the exact size of the file... it just gives you the size in bytes in the message box... I'll let you fill in the blanks. Have a look in the dictionary at 'files' and 'defaultFolder'

Hope this helps

Dixie

Re: How to get a filesize

Posted: Thu May 27, 2010 4:38 am
by alemrantareq
All the scripts working excellent ! Thanks guys.........Thanks you all. :D