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

Complete Directory Listing

Started by xcrafter_40, 04 March 2017 - 08:23 PM
xcrafter_40 #1
Posted 04 March 2017 - 09:23 PM
Hi.

If I have a directory like:

Disk
|
+—abc
+—def
+—folder1
\\\\\\+—ghi
\\\\\\+—jkl

How would I turn it into a table like so:
x = {"/abc","/def","/folder1/ghi","/folder1/jkl"}

I tried something like:
>>> x = find("*")
>>> x

{"abc","def","ghi","jkl"}

But as you can see, it doesn't return the directory.

How would I go about doing this?
Any help would be appreciated!

PS: Sorry about the \\\ for the directory listing! I couldn't make it display repeated spaces!
Sewbacca #2
Posted 04 March 2017 - 10:51 PM
You have to index your folder.
Try this:

function indexFolder (sPath, tRes)
local tRes = tRes or {}
local tFolder = fs.list(sPath) -- List files in this folder
for i = 1, #tFolder do -- Check out each subfolder
  local sSubfolder = fs.combine(sPath, tFolder[i])
  if fs.isDir(sSubfolder) then -- Is it a subfolder?
   indexFolder(sSubfolder, tRes) -- Add it to your index.
  end
  tRes[#tRes + 1] = sSubfolder
end
return tRes
end
print(textutils.serialize(indexFolder("disk")))

I think this would do what you wanna do.
Also try out:


function indexFolder (sPath, tRes)
local tRes = tRes or {}
local tFolder = fs.list(sPath) -- List files in this folder
for i = 1, #tFolder do -- Check out each subfolder
  local sName = tFolder[i]
  local sSubfolder = fs.combine(sPath, sName)
  if fs.isDir(sSubfolder) then
   tRes[sName] = indexFolder(sSubfolder, {sSubfolder})
  else
   tRes[sName] = sSubfolder
  end
end
return tRes
end
print(textutils.serialize(indexFolder("disk")))
Edited on 04 March 2017 - 09:56 PM
xcrafter_40 #3
Posted 18 March 2017 - 05:32 PM
Thanks :lol:/> This is exactly what I was looking for!