Jump to content




Scanning Infinite folders

help lua

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

#1 RoD

  • Members
  • 313 posts

Posted 11 June 2014 - 04: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 by RoD, 11 June 2014 - 04:38 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 11 June 2014 - 05: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


#3 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 11 June 2014 - 05: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 by Cranium, 11 June 2014 - 05:26 PM.


#4 RoD

  • Members
  • 313 posts

Posted 11 June 2014 - 05:37 PM

View PostKingofGamesYami, on 11 June 2014 - 05:12 PM, said:

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!

View PostCranium, on 11 June 2014 - 05:26 PM, said:

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

#5 RoD

  • Members
  • 313 posts

Posted 11 June 2014 - 05:54 PM

View PostKingofGamesYami, on 11 June 2014 - 05:12 PM, said:

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

View PostCranium, on 11 June 2014 - 05:26 PM, said:

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 ).

#6 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 11 June 2014 - 06:32 PM

Yeah, it'll throw everything in a table, and each directory should be a new table within the main one.

#7 CometWolf

  • Members
  • 1,283 posts

Posted 11 June 2014 - 09: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 by CometWolf, 11 June 2014 - 09:43 PM.


#8 RoD

  • Members
  • 313 posts

Posted 12 June 2014 - 01:28 PM

View PostCometWolf, on 11 June 2014 - 09:22 PM, said:

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)

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 June 2014 - 01: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. :)

#10 CometWolf

  • Members
  • 1,283 posts

Posted 12 June 2014 - 01: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...

#11 RoD

  • Members
  • 313 posts

Posted 12 June 2014 - 01:57 PM

View PostCometWolf, on 12 June 2014 - 01:36 PM, said:

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).





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users