Last active
November 3, 2018 19:26
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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