Skip to content

Instantly share code, notes, and snippets.

@Sascha-T
Last active November 3, 2018 19:26
Show Gist options
  • Save Sascha-T/31c0cd29c429856ee9a35919a98729e8 to your computer and use it in GitHub Desktop.
Save Sascha-T/31c0cd29c429856ee9a35919a98729e8 to your computer and use it in GitHub Desktop.
This is a lua script for OpenComputers to get the size of a file (returns 0 on directories)
local args = {...}
local shell = require("shell")
if not args[1] then
print("Please specify a path")
print("stat (PATH)")
require("os").exit()
end
local path = shell.resolve(args[1])
local fs = require("component").filesystem
if not fs.exists(path) then
print("File does not exist")
require("os").exit()
end
local size = math.ceil(fs.size(path))
-- Code from http://lua-users.org/wiki/SimpleRound
function round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
-- End
function sizeFormat(size)
if size < 1024 then
return(size .. " B")
end
if size > 1024 and size < 1048576 then
return(round(size / 1024, 2) .. " KiB")
end
if size > 1048576 and size < 1073741824 then
return(round(size / 1048576, 2) .. " MiB")
end
end
local bigFBios = false
if size > 4096 then
bigFBios = true
end
print(sizeFormat(size))
if bigFBios then
print("WARN!: Too big for eeprom")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment