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

Attempt to index....?

Started by svdragster, 03 February 2013 - 10:38 AM
svdragster #1
Posted 03 February 2013 - 11:38 AM

function check(datei)
   term.setTextColour(colors.white)
   write(datei.." ")
   if fs.isDir(datei) then
      term.setTextColour(colors.green)
      print("\(Verzeichnis\)")
   else
      term.setTextColour(colors.orange)
      print("Inhalt:")
      f = io.open(datei, "r")
      data = f:read("*a") 
      f:close()
      print(data)
      term.setTextColour(colors.white)
       if data:find ("shell.run") then
         term.setTextColour(colors.red)
         print("> Found a line with shell.run in "..datei.."!")
         term.setTextColour(colors.white)
      else
         term.setTextColour(colors.lightgreen)
         print("No shell.run in "..data)
         term.setTextColour(colors.white)
      end
   end
   print("")
end

function ReadDir(dir)
 local  dateien = fs.list(dir)
    for i=1,#dateien do
      if fs.isDir(dir) then
         ReadDir(dateien[i])
      end
      check(dateien[i])
  end
end

ReadDir("/")

scan:11: attempt to index? (a nil value)
wat is wrong
Kingdaro #2
Posted 03 February 2013 - 11:45 AM
data = f:read("*a")

Should be:

data = f.readAll()

EDIT: No, the error here is that f doesn't exist. Should probably check if f isn't nil first before reading it.
remiX #3
Posted 03 February 2013 - 11:46 AM
Works fine for me, but I do see a problem arising from your ReadDir function because you're calling ReadDir within itself if dataein is a folder. Remove that just to check if it works.

edit:

data = f:read("*a")

Should be:

data = f.readAll()

EDIT: No, the error here is that f doesn't exist. Should probably check if f isn't nil first before reading it.

No f:read("*a") is the way for io. readAll() is for fs
svdragster #4
Posted 03 February 2013 - 11:58 AM
When removing that just nothing happens ~_~ It prints nothing
remiX #5
Posted 03 February 2013 - 12:01 PM
I removed only that line and it works fine for me… Check the attachment
svdragster #6
Posted 03 February 2013 - 12:05 PM
Woah, thanks.

Edit: Well I wanted it to check all the directories, but without that I don't know a different way to check all dirs for program. Anyone knows how?
remiX #7
Posted 03 February 2013 - 12:10 PM
Is yours working yet?
svdragster #8
Posted 03 February 2013 - 12:15 PM
Is yours working yet?
when removing the line?
MysticT #9
Posted 03 February 2013 - 12:42 PM
I think the problem is that fs.list returns a list of the file names inside a directory, so when using io.open you pass the name of the file, instead of the full path.
this should work:

function ReadDir(dir)
 local dateien = fs.list(dir)
 for i = 1, #dateien do
  local path = fs.combine(dir, dateien[i])
  if fs.isDir(path) then
    ReadDir(path)
  end
  check(path)
 end
end
svdragster #10
Posted 04 February 2013 - 03:39 AM
Now it stops at rom/apis/colors, with the error message: "scan:22: Expected number"
MysticT #11
Posted 04 February 2013 - 04:11 AM
There's no lightgreen color:

term.setTextColour(colors.lightgreen)
It should be:

term.setTextColour(colors.lime)
svdragster #12
Posted 04 February 2013 - 04:27 AM
There's no lightgreen color:

term.setTextColour(colors.lightgreen)
It should be:

term.setTextColour(colors.lime)

oopsydaisy