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

Filesystem help

Started by shiskebab, 18 June 2016 - 02:54 PM
shiskebab #1
Posted 18 June 2016 - 04:54 PM
Hi I just had a quick question regarding filesystems in computercraft.


I'm trying to create a password program that uses a keycard.

I came across this guide:

http://www.computerc...ng-from-a-file/

which uses the "assert" function to validate the directory that the computer attempts to access, and if assert fails it will instead return an error. My problem is that I can't seem to act on this information.

if i do this:


local hRead = assert(fs.open(sFile, "r"))

Then the program will exit at this line and give an error like "failed to assert!" or something, but since this is a password protected door I can't have the program exit :P/> so I tried using an if statement like this:


if hRead = assert(fs.open(sFile, "r")) then
   sPass = hRead.readLine()
   hRead.close()
else
  disk.eject(tostring(driveSide))
  sleep(2)
end

which doesn't seem to work (gives [expected ")" at line 54] error). So now I'm looking for ideas as to how I can make sure the directory is accesible without throwing an error that stops the program from running. Ideally I'd want something like my if statement so I can do something else in case of an error.

All suggestions are appreciated!
Saldor010 #2
Posted 18 June 2016 - 05:32 PM
Can't you use the fs.exists function?

http://computercraft.info/wiki/Fs.exists
Dog #3
Posted 18 June 2016 - 05:47 PM
One thing I noticed is here:

if hRead = assert(fs.open(sFIle, "r")) then

This should be:

if hRead == assert(fs.open(sFile, "r")) then

This won't fix your problem, but I thought it should be pointed out. A single equals sign sets a variable to a value, a double equals sign compares one value/variable to another.

Having said that…as Jiloacom suggested fs.exists is probably what you're after instead of assert.
shiskebab #4
Posted 18 June 2016 - 06:00 PM
Thats actually really helpfull! I didn't include the f.exists because in the guide that I included the author calls fs.exists and then the assert function, making me think the fs.exists was redundant? However, if I can just use the fs.exists to check yes or no in the directory then thats great.