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

moving a file with a input into a directory

Started by AnDwHaT5, 18 March 2013 - 10:22 AM
AnDwHaT5 #1
Posted 18 March 2013 - 11:22 AM
Its in the name i need to move a file with a input in one program to a directory look at near bottom comment.
SuicidalSTDz #2
Posted 18 March 2013 - 11:26 AM

local handle = io.open(directory/file,"r") --Opens the file in the directory
local data = handle:read() --Reads all the data on the first line of the file
handle:close() --Closes the handle
input = read() --Get user password/username
if input ~= data then --Compare the input with the contents of the file
--Wrong Password
else
--Right Password
end

You can open files in directories by defining the directory first then seperate the file with a "/" like so:

users/AnDwHaT5

Or if you have directories in directories:

data/users/AnDwHaT5/username/password
AnDwHaT5 #3
Posted 18 March 2013 - 11:28 AM
yes but what if the file is a input
SuicidalSTDz #4
Posted 18 March 2013 - 11:32 AM
yes but what if the file is a input
If you get the input from that file? Which file are you getting the input from

Oh, I see:


local tab = fs.list("users")
local correct = false
repeat
input = read()
for i = 1,#tab do
  if input ~= tab[i] then
   correct = false
  else
   correct = true
  end
end
until correct
--If the user makes it past this point, the username is correct
Edited on 18 March 2013 - 10:47 AM
AnDwHaT5 #5
Posted 18 March 2013 - 11:44 AM
ok we have three things we are working with users AnDwHaT5 and lets say hello. So in hello we want to get into users which is a directory and get into AnDwHaT5. So lets say in AnDwHaT5 it says hi. The input for the password in hello is no. I want it to look in users/AnDwHaT5 and see if it is right or not. Remember the imput can be anything dont take so literally when i say no is the input its ..input.
SuicidalSTDz #6
Posted 18 March 2013 - 11:48 AM
Look at my last edit? Im not sure if that's what you are looking for..

EDIT: Is this what you mean?

File Writing Data to Hello in users/AnDwHaT5/hello
input = read()
local handle = io.open("users/AnDwHaT5/hello","w")
handle:write(input)
handle:close()

File reading the data and comparing it to users/AnDwHaT5
local handle = io.open("users/AnDwHaT5,"r")
local data = handle:read()
handle:close()
local handle = io.open("users/AnDwHaT5,"r")
local readData = handle:read()
handle:close()
if data ~= realData then
--Incorrect
else
--Correct
end
AnDwHaT5 #7
Posted 18 March 2013 - 11:57 AM
Ok i got that i need help with this code. i need it to read the pass in the file.

repeat
term.clear()
term.setCursorPos(1,1)
write("User: ")
user = read()
write("Pass: ")
pass = read()
local tab = fs.list("users")
local correct = false
for i = 1,#tab do
  if user ~= tab[i] then
   correct = false
  else
   correct = true
  end
end
until correct
local content = {}
local line = f.readLine()
while line do
  table.insert(content, line)
  line = f.readLine()
end
if content[1] == pass then
print("hi")
else
print("nope")
end
It dosnt want to work.
SuicidalSTDz #8
Posted 18 March 2013 - 12:03 PM
What file are you trying to open with f.readLine()?
AnDwHaT5 #9
Posted 18 March 2013 - 12:07 PM
Honestly that was part of a old code here is the better one

repeat
write("User: ")
user = read()
write("Pass: ")
pass = read()
local tab = fs.list("users")
local correct = false
for i = 1,#tab do
  if user ~= tab[i] then
   correct = false
  else
   correct = true
  end
end
until correct
local content = {}
end
All i need is to read the pass from the File in the directory
SuicidalSTDz #10
Posted 18 March 2013 - 12:13 PM
How many lines are you reading from? One or more?
AnDwHaT5 #11
Posted 18 March 2013 - 12:15 PM
1

wasnt that the longest post in the forums ^

the one that is it wont stop fusing the edits
SuicidalSTDz #12
Posted 18 March 2013 - 12:17 PM
Well gladly I think it is over ^_^/>


repeat
 write("User: ")
 user = read()
 write("Pass: ")
 pass = read()
 local tab = fs.list("users")
 local correct = false
 for i = 1,#tab do
  if user ~= tab[i] then
   correct = false
  else
   correct = true
  end
end
until correct
local fileLocation = ("users/"..user)
local handle = io.open(fileLocation,"r")
local data = handle:read()
handle:close()
if pass ~= data then
 print("WRONG!")
else
 print("CORRECT!")
end
AnDwHaT5 #13
Posted 18 March 2013 - 12:26 PM
Thank you so much!
SuicidalSTDz #14
Posted 18 March 2013 - 12:29 PM
Thank you so much!
It is my pleasure! ^_^/>
AnDwHaT5 #15
Posted 18 March 2013 - 12:32 PM
BTW one last thing so i dont have to post again uhh how do i move a file using a input to a directory i mean i know how without a input. I would think something like fs.move(user, "/users") but it dosnt work
SuicidalSTDz #16
Posted 18 March 2013 - 12:37 PM
BTW one last thing so i dont have to post again uhh how do i move a file using a input to a directory i mean i know how without a input. I would think something like fs.move(user, "/users") but it dosnt work
I'm not sure if I understand but:

fs.move(user,"users/")
A backslash in the front is simply a coder's choice/preference. I for one, like using it.

a/b/c is the same as /a/b/c

A backslash at the end, however, indicates a directory
AnDwHaT5 #17
Posted 18 March 2013 - 12:40 PM
file exists it says and no it dosnt in the directory
SuicidalSTDz #18
Posted 18 March 2013 - 12:44 PM
Whoops my bad:

local location = ("users/"..user)
fs.move(user,location)
AnDwHaT5 #19
Posted 18 March 2013 - 01:10 PM
Thats what worked! Thanks i can now finish my user and pass system!
SuicidalSTDz #20
Posted 18 March 2013 - 01:16 PM
Thats what worked! Thanks i can now finish my user and pass system!
Woot! ^_^/>
remiX #21
Posted 18 March 2013 - 05:46 PM
 for i = 1,#tab do
  if user ~= tab[i] then
   correct = false
  else
   correct = true
  end
end
??
if fs.exists(user) then
  -- file exists :??
end