Jump to content




Directory Size

computer media lua

9 replies to this topic

#1 Ziriee

  • Members
  • 47 posts

Posted 12 September 2013 - 11:56 AM

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/")


#2 Goof

  • Members
  • 751 posts

Posted 12 September 2013 - 12: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

#3 Ziriee

  • Members
  • 47 posts

Posted 12 September 2013 - 01:04 PM

View PostMikk809h, on 12 September 2013 - 12:50 PM, said:

-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 ._.

#4 Goof

  • Members
  • 751 posts

Posted 12 September 2013 - 01:12 PM

... derp... of course... back to the work

#5 Ziriee

  • Members
  • 47 posts

Posted 12 September 2013 - 01: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")


#6 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 12 September 2013 - 01: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"))


#7 Goof

  • Members
  • 751 posts

Posted 12 September 2013 - 01:51 PM

im just gonna shut up from now on... i feel useless

#8 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 12 September 2013 - 01: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


#9 Ziriee

  • Members
  • 47 posts

Posted 12 September 2013 - 01:58 PM

View PostMysticT, on 12 September 2013 - 01:49 PM, said:

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.

#10 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 12 September 2013 - 02:08 PM

View PostLyqyd, on 12 September 2013 - 01:56 PM, said:

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.

View PostZiriee, on 12 September 2013 - 01:58 PM, said:

View PostMysticT, on 12 September 2013 - 01:49 PM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users