Jump to content




[Solved] - listing all files on the computer ( fs.list() )


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

#1 Goof

  • Members
  • 751 posts

Posted 21 May 2014 - 02:57 PM

Hello

EDIT: Added current code

I've got into a bit of a weird problem, how to get all files + paths of every single file ( not readonly ) on the computer...
But i've forgot how to do it...

My only and current code i've been able set up is:
Spoiler

Anybody able to help?


Thanks in advance

Edited by Mikk809h, 19 October 2014 - 02:57 PM.


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 21 May 2014 - 02:58 PM

The easiest way would be to simply build a table with a recursive function that uses fs.list.

#3 Goof

  • Members
  • 751 posts

Posted 21 May 2014 - 03:01 PM

Edited OP with current code.

Hmm... Yeah. thats what im trying... but i dont know exactly how to get it work properly..
(it just goes into one of the folders and keep displaying the same files in that folder)

Edited by Mikk809h, 21 May 2014 - 04:03 PM.


#4 Agoldfish

  • Members
  • 451 posts
  • LocationSome Fish Bowl in Ohio.

Posted 21 May 2014 - 03:37 PM

I think good ol'
fs.list("/")
Should work...

#5 Goof

  • Members
  • 751 posts

Posted 21 May 2014 - 04:02 PM

View PostAgoldfish, on 21 May 2014 - 03:37 PM, said:

I think good ol'
fs.list("/")
Should work...

Did you read my code?
..

The problem im having is:

View PostMikk809h, on 21 May 2014 - 03:01 PM, said:

(it just goes into one of the folders and keep displaying the same files in that folder and being stuck in that loop)


#6 apemanzilla

  • Members
  • 1,421 posts

Posted 21 May 2014 - 04:07 PM

function listAll(path)
  local path = path or ""
  local tbl = fs.list(path)
  for k,v in pairs(path) do
    if fs.isDir(v) then
      local result = listAll(fs.combine(path,v))
      for k,v in pairs(result) do
        table.insert(tbl,fs.combine(path,v))
      end
    end  
  end
  return tbl  
end
Run that code with no arguments to get a list of all files on the computer.

Edited by apemanzilla, 21 May 2014 - 04:08 PM.


#7 Goof

  • Members
  • 751 posts

Posted 21 May 2014 - 04:15 PM

hmm

That doesnt return the full path of the file...
And sholdnt the loop go through "tbl" instead of "path" ?

#8 Agoldfish

  • Members
  • 451 posts
  • LocationSome Fish Bowl in Ohio.

Posted 21 May 2014 - 05:08 PM

If I understand correctly, you want to list every editable file? If so:
for _, f in pairs(fs.list("/")) do
    if not fs.isReadOnly(f) then
	  --#blah
    end
  end
Should work.

#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 21 May 2014 - 05:09 PM

His code is a little wonky, try this instead. Again, call it with no arguments.

local function listAll(_path, _files)
  local path = _path or ""
  local files = _files or {}
  if #path > 1 then table.insert(files, path) end
  for _, file in ipairs(fs.list(path)) do
    local path = fs.combine(path, file)
    if fs.isDir(path) then
      listAll(path, files)
    else
      table.insert(files, path)
    end
  end
  return files
end

You can also throw a readOnly check in there if you like.

#10 CometWolf

  • Members
  • 1,283 posts

Posted 21 May 2014 - 05:18 PM

View PostAgoldfish, on 21 May 2014 - 05:08 PM, said:

If I understand correctly, you want to list every editable file? If so:
for _, f in pairs(fs.list("/")) do
	if not fs.isReadOnly(f) then
	  --#blah
	end
  end
Should work.
As others have said, this simply won't be enough. You need the recursive function, otherwise you'll only get the contents of the root("/") folder.

Edited by CometWolf, 21 May 2014 - 05:18 PM.


#11 apemanzilla

  • Members
  • 1,421 posts

Posted 21 May 2014 - 06:07 PM

View PostLyqyd, on 21 May 2014 - 05:09 PM, said:

His code is a little wonky, try this instead. Again, call it with no arguments.

local function listAll(_path, _files)
  local path = _path or ""
  local files = _files or {}
  if #path > 1 then table.insert(files, path) end
  for _, file in ipairs(fs.list(path)) do
    local path = fs.combine(path, file)
    if fs.isDir(path) then
      listAll(path, files)
    else
      table.insert(files, path)
    end
  end
  return files
end

You can also throw a readOnly check in there if you like.
I guess this is what I get for trying to whip up a function on my iPad three minutes before lunch :P

#12 Goof

  • Members
  • 751 posts

Posted 21 May 2014 - 08:24 PM

Hmm... Yeah.. Lyqyd's example is the most effiecent and compact way i can see it...

Thank you for helping me out, everyone.

:D

#13 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 22 May 2014 - 01:02 AM

one problem you can have with this however is with large filesystems it is possible to have ComputerCraft terminate your script due to failure to yield, as such you can make a solution that uses coroutines so this doesn't happen. Now one thing with this solution is it doesn't make a table of values automatically for you, instead you have to use it in a for loop

Code

Commented Code

Usage Examples

Alternatively you can also adapt either Lyqyd's or my functions to be able to ignore certain paths too. Here's an example with mine
Code

Usage Example

Edited by theoriginalbit, 22 May 2014 - 01:20 AM.


#14 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 22 May 2014 - 06:04 AM

Wait, have you actually had a large enough quantity of files on a single ComputerCraft computer that this would take longer than ten seconds to complete? That's impressive, albeit in a kind of horrifying way.

#15 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 22 May 2014 - 06:38 AM

View PostLyqyd, on 22 May 2014 - 06:04 AM, said:

Wait, have you actually had a large enough quantity of files on a single ComputerCraft computer that this would take longer than ten seconds to complete? That's impressive, albeit in a kind of horrifying way.
haha yeah I have actually. I had a few different big 'OSes' on a computer and yeah CC killed the program 'cause the list was taking too long. haha. Also the 10 seconds kill time is a bit, well, a bit variable, on a server once I actually had a script killed after ~5 seconds.

#16 Goof

  • Members
  • 751 posts

Posted 22 May 2014 - 02:08 PM

Well... This doesnt actually matter of the time...
As you said, i could call the yields...
But the only thing im going to use this for, is to return a table with full paths, then upload everything to a website xD...

(its a "quick-function" to upload my whole computer to a certain web... )

I think im going to look into TOBIT's examples, and modify it a bit.

Thank you guys!


EDIT:



Btw. When we talk about website, i actually have an "semi" weird question about PHP
( Yes, i think this topic can go into General now, when we're talking bout' other programming languages. )

I've made a website, with an upload file ( upload.php )

This upload.php page accepts an Auth key ( to ensure its me using the web )
Then it uses a Method ( "startSession", "inSession", "endSession" )
then it uses fileData, and fileName...

But my problem is to create a new folder corresponding to latest folder ID, where the session is going to be in.

( For example: )
 uploads/1/testFile.lua
If that "1" folder already exists when a new session is started, its going to create the folder with the name of
"2"
Like this:
 uploads/2/EMPTYFOLDER


Then when posting "inSession" its going to create single files and folders with the supplied fileData inside.
For example, lets say im uploading a file in a System/test.lua folder, to the web

Then the following is going to happen in the uploads/2 folder
       Folders are created here to ensure the best compatibility of getting everything again.
uploads/2/System/test.lua



Then whenever i end the session, its going to close all connections and stop writing files to that folder.


Btw... You can ask for more info, but since i dont know that much php, im a bit noobish


Thanks in Advance

Edited by Mikk809h, 22 May 2014 - 02:16 PM.


#17 Gumball

  • Members
  • 254 posts
  • LocationFairbanks, Alaska

Posted 17 September 2014 - 11:46 PM

If you just want to list it, do this:

files = fs.list()
print(files)
repeat
until not line
print("Finished printing lines")

#18 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 17 September 2014 - 11:55 PM

So, your code will create a table of the file names in the root (ignoring all the files in sub directories), fail to print that list to screen since it's a table (and probably error out). If it doesn't error out, then it'll print "Finished printing lines" because the unnecessary repeat loop will immediately terminate since 'line' is never declared or assigned a value.

Sorry to sound so harsh, but not only does your code not work, it doesn't even address the OP's request.

For reference, your code (to list the files in the root) should be...
local files = fs.list()
for i = 1, #files do
  print(files[i])
end
print("Finished printing lines")

Edited by Dog, 18 September 2014 - 12:22 AM.


#19 Gumball

  • Members
  • 254 posts
  • LocationFairbanks, Alaska

Posted 19 October 2014 - 12:29 AM

Wasn't that good at coding then, heres my code that even colors the files and directories, ROM is green:

local tArgs = {...}
if(#tArgs ~=1) then
  files = fs.list("/")
elseif(#tArgs == 1) then
  for file=1,#tArgs do
    files = fs.list(files[file])
    if(fs.isDir(files[file]) then
	  if(files[file] == "rom") then
	    term.setTextColor(colors.green)
	    print(files[file])
	  else
	 
	  term.setTextColor(colors.yellow)
	  print(files[file])
    end
   
    else
    term.setTextColor(colors.blue)
    print(files[file])
end
  
 

 

Thats the simplest I could think of. xD

#20 Goof

  • Members
  • 751 posts

Posted 19 October 2014 - 02:55 PM

Hmm.. Didnt i tell you this was solved?

I must've forgotten that.

Well.. thanks for the answers, this is now solved :P





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users