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

Getting Every File for "OS"

Started by PixelFox, 13 April 2015 - 07:33 PM
PixelFox #1
Posted 13 April 2015 - 09:33 PM
My "Operating System" (It can't really be called that…It's more that the file explorer in windows or the finder in mac)
I want to find every single file BUT the system files, .menu and .gui, and save them in a variable or something that I can manipulate, so I can show them in the GUI or Graphical User Interface. I really can't find out how, is there some kind of simple something to do that with, and also, will it show APIs also?
I want them to be able to see the APIs. But I'll make them a different Icon.
Creator #2
Posted 13 April 2015 - 09:39 PM
Are you trying to build a desktop? I don't quite understand your question, could you please clarify?
PixelFox #3
Posted 13 April 2015 - 09:43 PM
Are you trying to build a desktop? I don't quite understand your question, could you please clarify?
Yes, I am. I am wanted every file name except for the startup file, the .gui file and the .menu file. I want the computer to find every file but them, and then put them in variables or something.
Edited on 13 April 2015 - 07:44 PM
valithor #4
Posted 13 April 2015 - 09:47 PM
Your best bet is to use a table.

If i remember correctly fs.list returns a table of all the files in a given dirrectory. So using this you could use a loop that will loop through all of the files check if they are a dirrectory, and if they are then loop through all the files in that dirrectory and so on.

This will not however get the apis. The easiest way I can think to get the apis would be to do something similar to do this

for k,v in pairs(_G) do 
  if type(v) == "table" then
    -- its most likely a api so do something with it
  end
end
PixelFox #5
Posted 13 April 2015 - 09:48 PM
Your best bet is to use a table.

If i remember correctly fs.list returns a table of all the files in a given dirrectory. So using this you could use a loop that will loop through all of the files check if they are a dirrectory, and if they are then loop through all the files in that dirrectory and so on.

This will not however get the apis. The easiest way I can think to get the apis would be to do something similar to do this

for k,v in pairs(_G) do
  if type(v) == "table" then
	-- its most likely a api so do something with it
  end
end
Ah, Thanks, but uh…I don't know how I'd do that…I'm kinda a noob.
I know some fs commands…I'll look up the the API later, and thanks with the APIs too.
Oh also, I'm trying to overright a file, and fs.copy returns "testfile:4: File Exists" is there a way to stop that?
Edited on 13 April 2015 - 07:53 PM
valithor #6
Posted 13 April 2015 - 09:54 PM
-snip

You can check if the file exists before trying to copy to the name.


if not fs.exists("testfile") then
  fs.copy("hi","testfile")
end
Edited on 13 April 2015 - 07:54 PM
PixelFox #7
Posted 13 April 2015 - 10:00 PM
-snip

You can check if the file exists before trying to copy to the name.


if not fs.exists("testfile") then
  fs.copy("hi","testfile")
end
Thanks, but I'm trying to overwrite them.
Edited on 13 April 2015 - 08:01 PM
valithor #8
Posted 13 April 2015 - 10:14 PM
-snip

You can check if the file exists before trying to copy to the name.


if not fs.exists("testfile") then
  fs.copy("hi","testfile")
end
Thanks, but I'm trying to overwrite them.

Then you will need to delete the file first.
PixelFox #9
Posted 13 April 2015 - 10:17 PM
-snip

You can check if the file exists before trying to copy to the name.


if not fs.exists("testfile") then
  fs.copy("hi","testfile")
end
Thanks, but I'm trying to overwrite them.

Then you will need to delete the file first.
Ok, thanks!
TheOddByte #10
Posted 15 April 2015 - 12:53 AM
If you want to find every single file on the computer you'd need to use a recursive loop
Example

local function list( path, blacklist )
    path = path or "/"
    local files = {}
    for _, name in ipairs( fs.list( path ) ) do
        if not fs.isDir( fs.combine( path, name ) ) then
            files[fs.combine( path, name )] = name
        else
            local t = list( fs.combine( path, name ) )
            for k, v in pairs( t ) do
                files[k] = v
            end
        end
    end
    return files
end
Note, this will only list files! And I'm not 100% this code works, as I haven't tested it, but it should work :P/>
And if you don't want to it list files in your system directories you could simply modify the code to ignore those directories.