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

Scanning Infinite folders

Started by RoD, 11 June 2014 - 03:35 PM
RoD #1
Posted 11 June 2014 - 05:35 PM
If i need, for example, to scan all the files in one computer, can i make a code to enter each folder as it finds it?
So like, if i have this folders:


    rom
   /	 \
apis   programs

is it possible to automatically scan the rom, the apis and the program folder?
I can make this easy script to scan one directory:

function get( dir, isLookingFiles )
tFolders = {}
tFiles = {}
for k, v in pairs(fs.list(dir)) do
  if fs.isDir(v) then
   tFolders[k] = v
  else
   tFiles[k] = v
  end
end
if isLookingFiles then return tFiles else return tFolders end
end
Edited on 11 June 2014 - 03:38 PM
KingofGamesYami #2
Posted 11 June 2014 - 06:12 PM
Call the function from within the function. the function scans for files, and if it finds a directory it calls itself to scan that directory.


function scan( dir )
 local files = {}
 for k, v in pairs ( fs.list( dir ) ) do
  if fs.isDir( v ) then
   scan( v )
  else
   files[ k ] = v
  end
 end
end
Cranium #3
Posted 11 June 2014 - 06:26 PM
I think I threw together something that might work…I haven't tested it yet.

local function fileTree(dir)
    local tree = {}
    if not dir then dir = "/"
    for k,v in pairs(fs.list(dir)) do
	    if fs.isDir(v) then
		    tree[v] = fileTree(v)
	    else
		    table.insert(tree, v)
	    end
    end
    return tree
end
Edited on 11 June 2014 - 04:26 PM
RoD #4
Posted 11 June 2014 - 06:37 PM
Call the function from within the function. the function scans for files, and if it finds a directory it calls itself to scan that directory.


function scan( dir )
local files = {}
for k, v in pairs ( fs.list( dir ) ) do
  if fs.isDir( v ) then
   scan( v )
  else
   files[ k ] = v
  end
end
end
Hum i see… Like a loop… Thanks!
I think I threw together something that might work…I haven't tested it yet.

local function fileTree(dir)
	local tree = {}
	if not dir then dir = "/"
	for k,v in pairs(fs.list(dir)) do
		if fs.isDir(v) then
			tree[v] = fileTree(v)
		else
			table.insert(tree, v)
		end
	end
	return tree
end
It looks nice, i will test both codes.
Thanks both
RoD #5
Posted 11 June 2014 - 06:54 PM
Call the function from within the function. the function scans for files, and if it finds a directory it calls itself to scan that directory.


function scan( dir )
local files = {}
for k, v in pairs ( fs.list( dir ) ) do
  if fs.isDir( v ) then
   scan( v )
  else
   files[ k ] = v
  end
end
end
I'v tested and it won't return the table. Then i added the return to get the table from this function and i only get the root files:

function scan( dir )
local files = {}
for k, v in pairs ( fs.list( dir ) ) do
  if fs.isDir( v ) then
   scan( v )
  else
   files[ k ] = v
  end
end
return files -- Only the root files are returned
end

I think I threw together something that might work…I haven't tested it yet.

local function fileTree(dir)
	local tree = {}
	if not dir then dir = "/"
	for k,v in pairs(fs.list(dir)) do
		if fs.isDir(v) then
			tree[v] = fileTree(v)
		else
			table.insert(tree, v)
		end
	end
	return tree
end
This one (needed a end :P/>) gets the root files and a table in the end ( i assume its getting tables inside tables ).
Cranium #6
Posted 11 June 2014 - 07:32 PM
Yeah, it'll throw everything in a table, and each directory should be a new table within the main one.
CometWolf #7
Posted 11 June 2014 - 10:22 PM
Neither of these functions will be able to scan every folder for ever file properly, Kings's is the closest however.

local scan,tScanFiles
scan = function( dir, main )
  tScanFiles = main and {} or tScanFiles
  for k, v in pairs ( fs.list( dir ) ) do
	if fs.isDir( v ) then
	  scan( dir.."/"..v )
	else
	  tScanFiles[#tScanFiles+1] = dir.."/"..v
	end
  end
  return main and tScanFiles
end

tFiles = scan(dir,true)
It's possible to work around needing the main bool, but this is simpler :P/>
Edited on 11 June 2014 - 08:43 PM
RoD #8
Posted 12 June 2014 - 02:28 PM
Neither of these functions will be able to scan every folder for ever file properly, Kings's is the closest however.

local scan,tScanFiles
scan = function( dir, main )
  tScanFiles = main and {} or tScanFiles
  for k, v in pairs ( fs.list( dir ) ) do
	if fs.isDir( v ) then
	  scan( dir.."/"..v )
	else
	  tScanFiles[#tScanFiles+1] = dir.."/"..v
	end
  end
  return main and tScanFiles
end

tFiles = scan(dir,true)
It's possible to work around needing the main bool, but this is simpler :P/>
This code is pretty close, yet, i get the root files and the rom files and folders (it's not scanning the rest of the folders just the ones in the root)
theoriginalbit #9
Posted 12 June 2014 - 02:35 PM
The code I posted here, will do the trick, make sure to read it as well as the examples to actually understand it. :)/>
CometWolf #10
Posted 12 June 2014 - 02:36 PM
I forgot to concat dir.."/" to the fs.isDir argument. It's pretty clear you're not understanding much of these codes though…
RoD #11
Posted 12 June 2014 - 02:57 PM
I forgot to concat dir.."/" to the fs.isDir argument. It's pretty clear you're not understanding much of these codes though…
I didn't noticed it. I actually understand this codes, i just can't made them like by myself, but i am really good at understanding how codes work (if i know most of the functions).