PDA

View Full Version : Check for file size in LUA



Sicter
May 17th, 2006, 02:25 PM
Can anybody help me write a script in LUA to check size of a file. Actually, what I want to do is write a script that compares 2 files and if the size differs, trigger an event. Any friendly souls out there that wanna help me achieve this?

Thanks in advance

birty
May 17th, 2006, 02:53 PM
open the file and then seek to the end, this will give you the size

VaioUserChris
May 17th, 2006, 03:03 PM
Look at post number 10 on this thread:
http://www.promixis.com/forums/showthread.php?t=9136&highlight=detect_file_size

Dan Ward has a function to read file size for you. In your example you'll want to change it a little -- maybe something like this:


LoadLuaCom ()

--Function to Detect the file Size
function detect_file_size (which_file)
fso = luacom.CreateObject ("Scripting.FileSystemObject")
file =fso:GetFile(which_file)
file_size = (strsub(file.size,1,3))
fso = nil
collectgarbage ()
return file_size
end

File1 = "C:\\MyFile1.txt"
File2 = "C:\\MyFile2.txt"

FileSize1 = detect_file_size (File1)
FileSize2 = detect_file_size (File2)

if FileSize1 ~= FileSize2 then
TriggerEvent(Myevent,18,"payload1")
end


Chris

birty
May 17th, 2006, 03:24 PM
much easier with lua's io functions:


file=openfile(filename,"r")
filesize=seek(file,"end")
closefile(file)

Sicter
May 17th, 2006, 03:39 PM
VaioUserChris' script works like a charm, thank you my friend.

I'll even try birty's suggestion.

Thanks guys, much appreciated.