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

Files and directories- Help still needed

Started by RoD, 30 March 2014 - 04:49 PM
RoD #1
Posted 30 March 2014 - 06:49 PM
I am currently devolping an OS and i would like to know a few things:
  • Can i print only the files in a directory?
  • Can i print only the folders in a directory?
  • Can i have an desktop that creates clickable icons that are files in a specific folder automaticaly?
Those are my main questions right now and i dont like to start many topics but i am really confused and need some help.

I can also say why do i want to know this:

As i said i am making an OS and i want to have a account management that reads the accounts (that are files in a directory) wich contain the password inside the file. I want to be able to change the name and the password of various accounts (these accounts can be created and removed, thats why i want to be capable of changing the username and password).

I need to print folders only for a file explorer.

And finnaly the desktop icons in order to take the files that are in a directory like "os/user1/desktop" and display whatever it is inside that folder in the desktop.

Ideas are also wellcome.
Edited on 03 April 2014 - 04:24 PM
CometWolf #2
Posted 30 March 2014 - 07:52 PM
Yes, yes and yes!
Using fs.list you can get a table with all the files/folders in a directory. You can then use fs.isDir to determine wether it's a file or a directory.
The last bit is a bit more complicated, you'll have to create the contents of your dekstop based on the aformentioned file list, adding in the information regarding where it's drawn as it goes. This information can then be later used with mouse_click events to open the file or folder that is clicked on.
RoD #3
Posted 30 March 2014 - 08:16 PM
Nice, i already tried to use fs.isDir and fs.list but i cant find a way of combine the two to output just a file.
And about the desktop, i pretty much want like a general file browser, wich there is some in the forums but just a desktop with the files displaced in alfabetic order or any order actually.
Thanks for the reply anyways.
CometWolf #4
Posted 30 March 2014 - 08:20 PM
It's a fairly simple thing to do.

local tFiles = {}
local tFolders = {}
for _k,file in pairs(fs.list"/") do
  if fs.isDir(file) then
    tFolders[#tFolders+1] = file
  else
    tFiles[#tFiles+1] = file
  end
end
RoD #5
Posted 30 March 2014 - 08:37 PM
It's a fairly simple thing to do.

local tFiles = {}
local tFolders = {}
for _k,file in pairs(fs.list"/") do
  if fs.isDir(file) then
	tFolders[#tFolders+1] = file
  else
	tFiles[#tFiles+1] = file
  end
end
You sir are just Awesome :D/> :D/>
Just a little ps:
I didnt got how to print out from that function so i changed to:

local i = 0
for _k,file in pairs(fs.list"/") do
  if fs.isDir(file) then
  else
  i = i + 1
print(file)
  end
end
print(i)
I added the "i" variable to know how many files are in there in order to work on the desktop, yet if anyone have some tips, advices and help to offer about the desktop or the OS it would be apreciated. :D/>

EDIT: (for anyone who is looking for the same answers as me) The changes that i made to CometWold's code are in order to count and print the files only. If you wish to count and print the folders:

local i = 0
for _k,file in pairs(fs.list"/") do
  if fs.isDir(file) then
	i = i + 1
print(file)
  else
  end
end
print(i)
Edited on 30 March 2014 - 07:10 PM
CometWolf #6
Posted 30 March 2014 - 09:01 PM
if you want the count of folders you could just print the size of the table.

print(#tFolders)
:P/>
RoD #7
Posted 30 March 2014 - 09:11 PM
if you want the count of folders you could just print the size of the table.

print(#tFolders)
:P/>
Yup but because i changed the code and removed the tables it wouldn't work if i had that in my code.
RoD #8
Posted 31 March 2014 - 11:21 AM
if you want the count of folders you could just print the size of the table.

print(#tFolders)
:P/>
I have another question, how can i see the files in a certain directory?
I already tried:

dispfiles("folder/test")
function dispfiles(dir)
	   local i = 0
	   for _k,file in pairs(fs.list(dir)) do
			  if fs.isDir(file) then
			  else
					 i = i + 1
					 print(file)
			  end
	   end
end
But it prints out the folders as well, and i dont want folders to be displayed for now :P/>
Edited on 31 March 2014 - 09:37 AM
Katorone #9
Posted 31 March 2014 - 11:37 AM
Something like this should work (untested):

dispfiles("folder/test")
function dispfiles(dir)
  local files = {}
  local dirs = {}
  for _,v in ipairs(fs.list(dir)) do
  if fs.isDir(file) then
    dirs[#dirs+1] = v
  else
    files[#files+1] = v
  end
  print("There were "..#dirs.." directories found:")
  for _,v in ipairs(dirs) do
    print("> "..v)
  end
  print("There were "..#files.." files found:")
  for _,v in ipairs(files) do
    print("> "..v)
  end
end
CometWolf #10
Posted 31 March 2014 - 01:25 PM
You have to provide the full path to isDir aswell, so change it to fs.isDir(dir.."/"..file)
RoD #11
Posted 31 March 2014 - 06:53 PM
You have to provide the full path to isDir aswell, so change it to fs.isDir(dir.."/"..file)
That's why it didn't work, thanks :D/>
And thanks katorone aswell, you may want to check your code though (i got some errors) ;)/>