Jump to content




Complete Directory Listing


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

#1 xcrafter_40

  • Members
  • 50 posts
  • LocationIn a room with a computer, duh.

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!

#2 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

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 by Sewbacca, 04 March 2017 - 10:56 PM.


#3 xcrafter_40

  • Members
  • 50 posts
  • LocationIn a room with a computer, duh.

Posted 18 March 2017 - 05:32 PM

Thanks :lol: This is exactly what I was looking for!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users