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!
Listing folders in folders
Started by MarioBG, Jan 17 2015 10:47 PM
5 replies to this topic
#1
Posted 17 January 2015 - 10:47 PM
#2
Posted 17 January 2015 - 11:29 PM
you could do this simply with a recursive function:
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?
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
Posted 17 January 2015 - 11:47 PM
I've taken a liking to using a table to track progress, rather than recursing:
"output" ends up containing the path to every file in the current folder and its subfolders.
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
Posted 18 January 2015 - 12:02 AM
InDieTasten, on 17 January 2015 - 11:29 PM, said:
you could do this simply with a recursive function:
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?
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?
Bomb Bloke, on 17 January 2015 - 11:47 PM, said:
I've taken a liking to using a table to track progress, rather than recursing:
"output" ends up containing the path to every file in the current folder and its subfolders.
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.
Thanks to both of you, and I hope to see you round checking my OS!
#5
Posted 18 January 2015 - 11:28 AM
InDieTasten, 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?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
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
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











