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

Directory Size

Started by Ziriee, 12 September 2013 - 10:56 AM
Ziriee #1
Posted 12 September 2013 - 12:56 PM
Guys, I need serious help with my code. It's supposed to calculate the size of all the files in a chosen directory (ex. rom/). Put this into your computer to bug test, I would REALLY appreciate help. Thanks in advance.
Pastebin: L4EVuCy0

depth = 0
dsizeout = 0
di = {}
function dsize(pth)
		depth = depth+1
		dtemp[depth] = fs.list(pth)
		for di[depth]=1, #dtemp[depth] do
				if fs.isDir(dtemp[depth][di[depth]]) then
						dsize(pth..dtmep[depth][di[depth]].."/")
				else
						dsizeout = dsizeout+fs.getSize(pth..dtemp[depth][di[depth]])
				end
		end
		depth = depth-1
end

dsize("rom/")
Goof #2
Posted 12 September 2013 - 01:50 PM


-- Your variables (could be local)
depth = 0
dsizeout = 0
di = {}
function dsize(pth)
	depth = depth+1
	di = fs.list(pth) -- insert fs.list into the di table
	t = di -- this was just a test, you can edit t in the for loop to di and delete this line
	for t = 1, #di do -- loop through all the files in the table
		if fs.isDir(di[t]) then -- checks if the file is an directionary or not
			dsize(pth..di[t].."/") -- sets the new directionary into this new directionary
		else
			dsizeout = dsizeout+fs.getSize(pth..di[t]) -- get the file size of every invidually file and add it to the global size variable (dsizeout)
		end
	end
	depth = depth-1
end

dsize("rom/") -- Call the function to get the size
print("Size: "..dsizeout.."\nCurrent Files:\n"..textutils.serialize(di)) -- You can delete this if you want...

This should work fine…

Im editing, so I can explain the code… wait a min = Done
Ziriee #3
Posted 12 September 2013 - 02:04 PM

-snip-

This should work fine…

Im editing, so I can explain the code… wait a min = Done

But… the rom directory is obviously not 2344 bytes ._.
Goof #4
Posted 12 September 2013 - 02:12 PM
… derp… of course… back to the work
Ziriee #5
Posted 12 September 2013 - 02:16 PM
Slap this on the end of your code to translate the bytes into easier-to-read stuff.


sizes = {"","k","M","G","T"}
temp = 1
while dsizeout >= 1024 do
dsizeout = math.floor(dsizeout/102,4)/10
temp = temp+1
end
print("Size: "..dsizeout..sizes[temp].."b")
MysticT #6
Posted 12 September 2013 - 02:49 PM
This should work:

local function getSize(path)
  local size = fs.getSize(path) -- get the size of the file/directory
  if fs.isDir(path) then -- if it's a directory, get the size of its contents
    local l = fs.list(path)
    for i = 1, #l do
      size = size + getSize(fs.combine(path, l[i]))
    end
  end
  return size
end

print("ROM Size: ", getSize("/rom"))
Goof #7
Posted 12 September 2013 - 02:51 PM
im just gonna shut up from now on… i feel useless
Lyqyd #8
Posted 12 September 2013 - 02:56 PM
That's an… interesting approach, MysticT.


function getSize(path)
  local size = 0
  for _, file in ipairs(fs.list(path)) do
    if fs.isDir(fs.combine(path, file)) then
      --# if it is a directory, recurse.
      size = size + getSize(fs.combine(path, file))
    else
      size = size + fs.getSize(fs.combine(path, file))
    end
  end
  return size
end
Ziriee #9
Posted 12 September 2013 - 02:58 PM
This should work:

-snip-
Brilliant! The fact that the size of ROM is 2.2Mb and rom/programs/secret/alongtimeago is 2.1Mb made me grin uncontrollably. My mouth hurts now.
MysticT #10
Posted 12 September 2013 - 03:08 PM
That's an… interesting approach, MysticT.
Well, directories take some space too (512 bytes, not much but you have to include it too), so it's easier that way.

This should work:

-snip-
Brilliant! The fact that the size of ROM is 2.2Mb and rom/programs/secret/alongtimeago is 2.1Mb made me grin uncontrollably. My mouth hurts now.
Yeah, that's one big ascii movie xD