Jump to content




Listing folders in folders


  • You cannot reply to this topic
5 replies to this topic

#1 MarioBG

  • Members
  • 43 posts
  • LocationSpain (or Hisperia)

Posted 17 January 2015 - 10:47 PM

Hello there, chaps!
Last evening, while finishing up my OS's Version 1.2 (1.1 was never released to the public), I ran into trouble when trying to make both the installer and the size-checking utility. While I can check file sizes no problem, I have troubles thinking of a function that can iterate over a filesystem and assess its size. I mean, I thought of the dirty solution of putting many loops one inside another, but that's just bad, and it limits the number of folders you can make.

As for the installer, what I can't see is the way to check the content of folders, and I think that, given a table which contained the needed data, I wouldn't know what to do with it either. This is a problem because I want to make a single-file installer, that needs no downloading multiple files from Pastebin or similar.

So, any ideas? Thanks in advance!

#2 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 17 January 2015 - 11:29 PM

you could do this simply with a recursive function:
function getRecursiveSize(path)
	local result = 0
	for k,v in pairs(fs.list(path)) do
		if(fs.isDir(path..v)) then
			result = result + getRecursiveSize(path..v.."/")
		else
			result = result + fs.getSize(path..v)
		end
	end
	return result
end
getRecursiveSize("/")
getRecursiveSize("/rom/")

the size won't include filenames and foldernames though.

it's just a function that sums all filesizes within the given folder, subfolder, subsubfolder... so I guess thats what you wanted right?

Edited by InDieTasten, 17 January 2015 - 11:31 PM.


#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 17 January 2015 - 11:47 PM

I've taken a liking to using a table to track progress, rather than recursing:

local fileList, output = fs.list(shell.resolve(".")), {}

while #fileList > 0 do
	if fs.isDir(shell.resolve(fileList[#fileList])) then
		local thisDir = table.remove(fileList,#fileList)
		local newList = fs.list(shell.resolve(thisDir))
		for i = 1, #newList do fileList[#fileList+1] = fs.combine(thisDir,newList[i]) end
	else output[#output+1] = table.remove(fileList,#fileList) end
end

"output" ends up containing the path to every file in the current folder and its subfolders.

#4 MarioBG

  • Members
  • 43 posts
  • LocationSpain (or Hisperia)

Posted 18 January 2015 - 12:02 AM

View PostInDieTasten, on 17 January 2015 - 11:29 PM, said:

you could do this simply with a recursive function:
function getRecursiveSize(path)
	local result = 0
	for k,v in pairs(fs.list(path)) do
		if(fs.isDir(path..v)) then
			result = result + getRecursiveSize(path..v.."/")
		else
			result = result + fs.getSize(path..v)
		end
	end
	return result
end
getRecursiveSize("/")
getRecursiveSize("/rom/")

the size won't include filenames and foldernames though.

it's just a function that sums all filesizes within the given folder, subfolder, subsubfolder... so I guess thats what you wanted right?
Thanks a lot! This will do very well for checking the size of the stuff.

View PostBomb Bloke, on 17 January 2015 - 11:47 PM, said:

I've taken a liking to using a table to track progress, rather than recursing:

local fileList, output = fs.list(shell.resolve(".")), {}

while #fileList > 0 do
	if fs.isDir(shell.resolve(fileList[#fileList])) then
		local thisDir = table.remove(fileList,#fileList)
		local newList = fs.list(shell.resolve(thisDir))
		for i = 1, #newList do fileList[#fileList+1] = fs.combine(thisDir,newList[i]) end
	else output[#output+1] = table.remove(fileList,#fileList) end
end

"output" ends up containing the path to every file in the current folder and its subfolders.
This, on the other hand, will do quite nicely to craft the table that powers the installer. I'd just have to include the actual content of the files in the table, and Bob's your uncle!

Thanks to both of you, and I hope to see you round checking my OS!

#5 MarioBG

  • Members
  • 43 posts
  • LocationSpain (or Hisperia)

Posted 18 January 2015 - 11:28 AM

View PostInDieTasten, on 17 January 2015 - 11:29 PM, said:

you could do this simply with a recursive function:
 function getRecursiveSize(path) local result = 0 for k,v in pairs(fs.list(path)) do if(fs.isDir(path..v)) then result = result + getRecursiveSize(path..v.."/") else result = result + fs.getSize(path..v) end end return result end getRecursiveSize("/") getRecursiveSize("/rom/") 
the size won't include filenames and foldernames though. it's just a function that sums all filesizes within the given folder, subfolder, subsubfolder... so I guess thats what you wanted right?
By the way, if any future user is to look at this topic for help, I had to modify this function a bit for it to fully work. The result is this:
function getRecursiveSize(path)
	    local result = 0
  if fs.isDir(path) then
   for k,v in pairs(fs.list(path)) do
	 if(fs.isDir(path.."/"..v) then
	   result = result + getRecursiveSize(path.."/"..v)
	 else
	   result = result + fs.getSize(path.."/"..v)
	 end
   end
  else
   result = fs.getSize(path)
  end
	    return result
end

Edited by MarioBG, 18 January 2015 - 11:29 AM.


#6 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 18 January 2015 - 10:57 PM

This question was asked a while ago, and i posted inside of it. After looking through quite a bit of my previous posts to find the correct thread. I present to you two coding options for finding a file's size:

Mine

KingofGamesYami's






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users