
How to get a filesize
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 203
- Joined: Wed Jul 23, 2008 8:46 am
How to get a filesize
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...... 

-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
Re: How to get a filesize
Create a new stack, drop a button onto it and set its script to:
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.
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
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.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
Re: How to get a filesize
Hi...
This should get you going...
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
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
Hope this helps
Dixie
-
- Posts: 203
- Joined: Wed Jul 23, 2008 8:46 am
Re: How to get a filesize
All the scripts working excellent ! Thanks guys.........Thanks you all. 
