[SOLVED] Trying to get used space on a computer

Started by xFranciB_, Dec 15, 2019, 02:50 PM

Previous topic - Next topic

xFranciB_

Hey there,
I've been trying to get the used space on my computer but it's not working.
I've used the following commands:

local freeSpace1 = fs.getFreeSpace("/")
local freeSpace2 = (freeSpace1/10000)
local usedSpace = (200-freeSpace2)

print(usedSpace)

What it should do is:
Get the free space (which is 2,000,000 (2MB of storage in bytes) minus all of the programs weight);
Divide it by 10,000 (2,000,000 / 10,000 = 200)
Subtract the result from 200 (200 - 200 = 0)

If the computer was empty, we would have 0% used space and, if it had, for example, 100 bytes of used space, it would do this:

2,000,000 - 100 = 1,999,900
1,999,900 / 10,000 = 199,99
200 - 199,99 = 0.01% of used space

In theory, this should work, but it's not doing that.
I tried the first time, and it gave me around 98 (I had 20KB of my own files), and by trying again after adding around 300KB to the computer I got 103.

The usage went up, but it's not proportionate (300KB is about 15% of 2000). If I try this on a new computer I get 100.05 (If the memory was 100% I would get 200), which doesn't make any sense, because the computer has 0KB of used space and the ROM doesn't occupy this much. Also, the computer with 20KB has less used space than this, which makes it even weirder.

fs.getFreeSpace doesn't even seem to work sometimes. If I add 100KB of files it doesn't update at times.

What am I doing wrong? I've been trying for more than 1 hour now and I'm starting to lose my mind.

Thanks in advance,

~xFranciB_

SquidDev

Quote from: xFranciB_ on Dec 15, 2019, 02:50 PMfs.getFreeSpace doesn't even seem to work sometimes. If I add 100KB of files it doesn't update at times.
Free space is only recomputed when the files are changed by ComputerCraft itself, or when the computer is first started. If you add files using an external program, you'll need to reload the world.

It's also worth noting that files (and folders) have a minimum size of 500 bytes, just to compensate for file system overhead. So if you have 10 empty files, they'll count as taking up 5kB. That said, I don't think that'll be impacting you here - I honestly can't see anything immediately wrong with the code.
GitHub | CC:Tweaked: A ComputerCraft fork | Plethora: A peripheral mod

xFranciB_

Nevermind, I find a solution myself.
To anyone wondering, this is the code I used:

local freeSpace = fs.getFreeSpace("/")

local usedSpace = (100-(freeSpace/10000))
usedSpacer = math.floor(usedSpace + 0.5)

print(usedSpacer.."%")

This is what it does if it has, for example 500,000 bytes of free storage (out of 1,000,000):

100-(500,000/10,000) = 100-50 = 50

Print result: 50%

The second line of code is used to round up or down the number if needed.
I realized that my config file was set to 1,000,000 bytes, and not 2,000,000 as i thought.
I also did some changes making the code smaller and smarter.