How to know the file size?

Hello everyone,

I want to know if there is any way to know the exact size of individual files.

function M.CheckFileFun(TheOrder)
	local ReData = "NotExist"
	local path = system.pathForFile(TheOrder, system.DocumentsDirectory )
	local fhd = io.open( path, "r" )
	if fhd then
	   fhd:close()
	   ReData = "Exist"
	end
	return ReData
end

I now use this method to find out if the file exists, so that players don’t download it repeatedly.

But now I have an update data area for emergency use, so that I can put the update file after the bug appears after selling.

I am using Aws S3, I can know the size of the data being downloaded.

I want to use the size of the new data to compare the size of the old data to prevent players from downloading it repeatedly.

Although the data files for emergency updates are small, if players are repeatedly downloaded, over time, it will be a huge cost. :sob:

Hi.

You can read the contents of the file into a string with local contents = fhd:read("*a") (the a meaning “all”) and then get its length, via #contents or string.len.

Thank you for the method.

But this seems different.

My file byte number is completely different from the number displayed by this method.

Not sure if it would work but docs(https://docs.coronalabs.com/api/library/lfs/index.html) link to this LFS reference and I can see attributes returning size. Is it what you are searching for?

2 Likes

@bgmadclown

Thank you, this method is the same as the size of the file downloaded by my S3.

This is the first time I have seen the webpage you provided. :smile:

Thanks again for the assistance of StarCrunch and bgmadclown.

1 Like

If this is on Windows, you might need to call open with “rb” instead of “r”. Otherwise, not sure what’s up. Anyhow, sounds like you’ve got something that works.

@StarCrunch

Thanks for your reminder, I will focus on Android and Ios at present, but players may also have Windows requirements in the future, I will change “r” to “rb” in the future.

function M.CheckFileSizeFun(TheOrder,TheSize)
	local ReData
	local path = system.pathForFile(TheOrder, system.DocumentsDirectory )
	
	local TheData = lfs.attributes( path )
	if TheData ~= nil then	
		ReData = TheData.size
	end
	
	if TheSize == ReData then
		ReData = "AlreadyUpdated"
	else
		ReData = "NotUpdated"
	end
	
	return ReData
end

Now it works.