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

Using fs.isDir() to Recognize Multiple Directory Levels.

Started by Edgelessjet, 13 August 2015 - 12:29 PM
Edgelessjet #1
Posted 13 August 2015 - 02:29 PM
I`ve made a program that can be used instead of going to the lua prompt and typing in the code there. I`m having a problem with the list function recognizing the difference between directories and files, which is done by changing the text color to green for directories. It will work for the root directory, but not for any level past that, "rom/". The code I`m showing is only the section of code for that list function, the only part of code that relates to it is a y = read() at the beginning. Thanks in advance for the help.

elseif y == "l" then --from here, I know that this elseif, it fits with my program.
  term.setCursorPos(1,1)
  term.clear()
  print("Name directory to list from.")
  local z = read()
  if fs.exists(z) == true then
	term.setCursorPos(1,1)
	term.clear()
	for i,v in ipairs(fs.list(z)) do
	  if fs.isDir(v) == true then
		term.setTextColor(colors.green)
		print(i.. ", " ..v)
		term.setTextColor(colors.white)
	  elseif fs.isDir(v) == false then
		print(i.. ", " ..v)
	  else
	end
	end
  elseif fs.exists(z) == false then
	term.setCursorPos(1,1)
	term.clear()
	print("Directory not found.")
	os.sleep(1)
	term.setCursorPos(1,1)
	term.clear()
	return
  end --To here.
Exerro #2
Posted 13 August 2015 - 09:54 PM
Fs.list returns a list of all the files/directories inside a given directory. If you list the items in /rom, you'll get something like this:


apis
autorun
help
programs
startup

Now, the folder 'apis' may not exist, but 'rom/apis' does.

On lines 9 and 10 you have this:


for i,v in ipairs(fs.list(z)) do
    if fs.isDir(v) == true then

You're checking to see if 'apis' exists (for example), not 'rom/apis'. To fix that, use


for i,v in ipairs(fs.list(z)) do
    if fs.isDir(z .. "/" .. v) == true then -- added: z .. "/" ..

Offtopic, but it bugs me when people use 'if a == true then', the '== true' is simply not necessary. Also…


if a == true then
...
elseif a == false then
...
end

can be shortened to this if 'a' is a boolean:


if a then
...
else
...
end
Lyqyd #3
Posted 13 August 2015 - 11:25 PM
It would be best to use fs.combine(z, v), as it will always handle path concatenation correctly.
Exerro #4
Posted 13 August 2015 - 11:34 PM
It would be best to use fs.combine(z, v), as it will always handle path concatenation correctly.

Yes, but I've never noticed paths such as 'abc///def' acting any differently from 'abc/def', although this may just be from using various emulators.
Edgelessjet #5
Posted 14 August 2015 - 03:56 AM
I revised my code using the fs.combine(z,v) and I also took out some of the unneeded logic. Thanks for the quick help, it woks just how I wanted it to now.

elseif y == "l" then --from here.
  term.setCursorPos(1,1)
  term.clear()
  print("Name directory to list from.")
  local z = read()
  if fs.exists(z) then
    term.setCursorPos(1,1)
    term.clear()
    for i,v in ipairs(fs.list(z)) do
    local d = fs.combine(z,v)
	  if fs.isDir(d) then
	    term.setTextColor(colors.green)
	    print(i.. ", " ..v)
	    term.setTextColor(colors.white)
	  else
	    print(i.. ", " ..v)
    end
    end
  elseif fs.exists(z) == false then
    term.setCursorPos(1,1)
    term.clear()
    print("Directory not found.")
    os.sleep(1)
    term.setCursorPos(1,1)
    term.clear()
    return
  end --To here.