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

Getting file size of current program?

Started by RadiationAlert, 07 June 2015 - 03:28 PM
RadiationAlert #1
Posted 07 June 2015 - 05:28 PM
I'm running a program that calculates the amount of disk space used (not free). I get an error whenever the loop iterates over the file actually calculating:

test:9: No such file


local Disk = shell.dir ();
local DiskContents = fs.list (Disk);

local DiskSize = 0

for index, file in pairs (DiskContents) do
print ("Previous Space Calculated: "..DiskSize);
print ("Current File: "..file);
***** DiskSize = DiskSize + fs.getSize (file); ***** Line 9
end

print ("Total Disk Space Used: "..DiskSize.." of "..fs.getFreeSpace (Disk));
KingofGamesYami #2
Posted 07 June 2015 - 05:39 PM
That's because you aren't using the full path.

Your code lists things in the directory "Disk", eg.

Disk/startup
Disk/program
Disk/help

But fs.list returns a table of names looking like this:

startup
program
help

fs.getSize expects to get the full path (Disk/startup), but is handed the "local" path (startup).

fs.combine( "Disk", "file" ) will return "Disk/file", or the absoulte path. (Note: you should still pass it the variable file, not "file". The string was an example)
RadiationAlert #3
Posted 07 June 2015 - 05:44 PM
By converting to the full path, it seems to work correctly now. Thanks.
davidgumazon #4
Posted 20 July 2015 - 11:33 AM
local Disk = shell.dir ();
local DiskContents = fs.list (Disk);

local DiskSize = 0

for index, file in pairs (DiskContents) do
print ("Previous Space Calculated: "..DiskSize);
print ("Current File: "..file);
***** DiskSize = DiskSize + fs.getSize (file); ***** Line 9
end

wow! i learn new code in LUA i finally get it how for index, var in pairs(val) do end it works :D/> thank you for the Code T_T

-- i test this code and it works :D/>
test={'xxd','xdx','xdd'}
for var1, var2 in pairs(test) do
if var2=='xdd' then
  print(var1) print(var2)
end
end
+1 Like

This Code for my OS ;D