This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
raxteeze's profile picture

How to get size?

Started by raxteeze, 09 August 2015 - 05:47 PM
raxteeze #1
Posted 09 August 2015 - 07:47 PM
How to get size of all files in computer?
SquidDev #2
Posted 09 August 2015 - 07:52 PM
If you have a look at the FS api. You'll see a couple of useful methods.

fs.getFreeSpace("/")
A computer has a maximum size of 1 Megabyte (1,000,000 bytes). So if you did 1000000 - fs.getFreeSpace("/") you could work out how much you've used.

If you need something more accurate - for instance just one directory then you could walk the directory tree (fs.list and fs.isDir) and then add together all the sizes in all directories (fs.getSize).
Creator #3
Posted 09 August 2015 - 08:19 PM
Not that it is extremely rellevant, but I do believe a megabyte is 1024^2 bytes. :P/>
SquidDev #4
Posted 09 August 2015 - 08:34 PM
Not that it is extremely rellevant, but I do believe a megabyte is 1024^2 bytes. :P/>

Nope, that is a Mebibyte. This is because under standard (SI) units "Mega" means 10^6, and so the Mega meaning 2^20 is confusing.
Edited on 09 August 2015 - 06:34 PM
TYKUHN2 #5
Posted 09 August 2015 - 09:33 PM
Not that it is extremely rellevant, but I do believe a megabyte is 1024^2 bytes. :P/>

I thought that too. The things you learn. MEBIBYTES FOR ALL.
flaghacker #6
Posted 09 August 2015 - 09:51 PM
Not that it is extremely rellevant, but I do believe a megabyte is 1024^2 bytes. :P/>/>

Nope, that is a Mebibyte. This is because under standard (SI) units "Mega" means 10^6, and so the Mega meaning 2^20 is confusing.

I thought it was a dispute between hardware manufacturers claiming a Kb = 1000 bytes to sell there HDDs with more capacity, and programmers wanting to use the full capacity of binary numbers (thus claming a Kb = 1024 bytes). I could be wrong though…
Bomb Bloke #7
Posted 10 August 2015 - 12:54 AM
More between people who want to follow the SI standard, but yeah, one could argue that drive manufacturers are among the few who have an actual stake in pushing the "mebibyte" thing. They can go jump - it's a failed standard, and the term megabyte, in common usage, refers to 2^20 bytes.

The term "mebibyte" didn't even exist when I started programming. They'll never push that baby-speak on me.
raxteeze #8
Posted 10 August 2015 - 09:59 AM
If you have a look at the FS api. You'll see a couple of useful methods.

fs.getFreeSpace("/")
A computer has a maximum size of 1 Megabyte (1,000,000 bytes). So if you did 1000000 - fs.getFreeSpace("/") you could work out how much you've used.

If you need something more accurate - for instance just one directory then you could walk the directory tree (fs.list and fs.isDir) and then add together all the sizes in all directories (fs.getSize).

I used fs.getSize('/') and .getSize('/rom/')
Edited on 02 April 2017 - 12:45 PM
SquidDev #9
Posted 10 August 2015 - 10:03 AM
If you need something more accurate - for instance just one directory then you could walk the directory tree (fs.list and fs.isDir) and then add together all the sizes in all directories (fs.getSize).

I used fs.getSize('/') and .getSize('/rom/') but it turns out:
(image of 0 bytes)

fs.getSize only works on files - directories do not have a 'size' as such. So what you'll want to do is walk the directory tree to find every file. In a directory you need to list the files: If it is a file then you get the size, if it is a directory then you repeat the process for that directory.
raxteeze #10
Posted 10 August 2015 - 10:07 AM
If you need something more accurate - for instance just one directory then you could walk the directory tree (fs.list and fs.isDir) and then add together all the sizes in all directories (fs.getSize).

I used fs.getSize('/') and .getSize('/rom/') but it turns out:
(image of 0 bytes)

fs.getSize only works on files - directories do not have a 'size' as such. So what you'll want to do is walk the directory tree to find every file. In a directory you need to list the files: If it is a file then you get the size, if it is a directory then you repeat the process for that directory.

Ok. Thank you :)/>