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

fs.move Not working?

Started by boudragon, 03 July 2013 - 12:20 PM
boudragon #1
Posted 03 July 2013 - 02:20 PM
Ok so here is my problem… I'm making a login for my system where if you fail too many times it locks your account down. It does so by moving the users account to a different folder and renaming it to a number for inspection later by an admin. For example the user file name is Boudragon. I am generating a random number using:

rnd = tostring(math.random(101, 999))

then it prints a message with that number as a reference code… then I do this:

fs.move ("users/".. userlog, "pending/".. rnd)

It makes the new file just fine, however it does not delete the old one from the users folder. Any ideas why? The error I got is this:

login:142: Access denied

line 142 is where the fs.move is so I KNOW its a problem with that but can't see where I went wrong…
MudkipTheEpic #2
Posted 03 July 2013 - 02:22 PM
Make sure to close all lf your file handles.

When you use anyname = fs.open("filename","handle"), when you are done using anyname, do anyname.close().
boudragon #3
Posted 03 July 2013 - 02:24 PM
Ah so even for a move I have to frist open and then close the file? Got ya… let me try that then…
boudragon #4
Posted 03 July 2013 - 02:31 PM
Crap now Im lost again lol… ok so… let me try and post a little more of what I THINK it should look like… Im sure Im wrong but here it goes…

rnd = tostring(math.random(101,999))
file = fs.open("users/".. userlog, "r")
file.move ("users/".. userlog, "pending/".. rnd)
file.close()

is that close at least? :-p

EDIT: I must be close cause now I get login:143: attempt to call nil

143 is the file.move line…
MudkipTheEpic #5
Posted 03 July 2013 - 02:43 PM
If you are reading a file, close it before you move it. Its fs.move, not file.move.
boudragon #6
Posted 03 July 2013 - 03:05 PM
Still saying access denied… I tried:

file = fs.open("users/"..userlog, "r")
file.close()
fs.move ("users/".. userlog, "pending/".. rnd)

Thing is at the moment of moving it shouldnt be reading the file at all… it reads it when it checks the password but then closes… here I will give you a larger snippet…

ulog = fs.exists("users/".. userlog)
  if ulog == true then
	ulog = fs.exists("users/".. userlog)
	if (ulog and ulog:read()==userpas) then
		term.setCursorPos(16, 18)
		print(" Login Success...")
		sleep(3)
	else
		if lockdown < 2 then
		  term.setCursorPos(16, 18)
		  print(" Wrong Password ")
		  sleep(3)
			if userlog == puselog then
			  lockdown = lockdown+1
			else
			  lockdown = 0
			  puselog = userlog
			  lockdown = lockdown+1
			end
		else
		  term.setCursorPos(15, 18)
		  rnd = tostring(math.random(101,999))
		  print(" Lockdown Ref #: ".. rnd)
		  sleep(3)
		  file = fs.open("users/".. userlog)
		  file.close()
		  fs.move ("users/".. userlog, "pending/".. rnd)
		end
	end
  else
(Code goes on to a different branch of operations for handling an unknown username from here)
boudragon #7
Posted 03 July 2013 - 06:23 PM
AFter trying a few more combinations still nothing works… Just can't see the problem…
immibis #8
Posted 03 July 2013 - 08:50 PM
He didn't say to open and close the file when moving it - he said that every time you open the file you need to close it.
boudragon #9
Posted 03 July 2013 - 08:59 PM
Even so… without any of that simply moving the file doesnt work either. It makes a COPY… but refuses to delete the original. No clue why…

edit: and as far as I can tell… the file isnt opened any other time… unless doing a fs.exists opens it?
Lyqyd #10
Posted 03 July 2013 - 09:21 PM
Are you try to manipulate files in the /rom folder or its sub folders?
boudragon #11
Posted 03 July 2013 - 09:26 PM
folder structure is outside rom so:

&#46;&#46;/users/Boudragon
&#46;&#46;/pending/###

The idea is that when they fail to use the correct password 3 times it generates a random number… displays the number as a reference code and then moves the Boudragon file to the pending directory while also renaming it to the random number.

EDIT: In case you are wondering… YES I added a check to make sure the random number isnt already taken by another file. So the code above is a little out dated so-to-speak but either way… should work the very first time AT LEAST… lol
Lyqyd #12
Posted 03 July 2013 - 09:27 PM
Why the ..? Are you trying to access folders above the root of the computer's "hard drive"?
boudragon #13
Posted 03 July 2013 - 09:30 PM
No… just saying… the ".." was meant to say that the folders I am trying to access are just in the hard drive lol sorry to confuse. Folders are just /users and /pending

Also I KNOW my directories work just fine… I am able to create accounts, login successfully… everything else works BUT THIS… the lockout feature…
immibis #14
Posted 03 July 2013 - 10:38 PM
Or you could use fs.copy and fs.delete instead if fs.move won't work.
boudragon #15
Posted 03 July 2013 - 10:42 PM
Tried it :(/> Was the first thing I tried after none of my other attempts at moving worked… seems to have a problem deleting which I understnad is usually due to opening the file… and I thought I figured it out cause it does look at the file for the password, however, even adding a close after the password check it STILL wont move or delete.
immibis #16
Posted 04 July 2013 - 02:27 AM
Pastebin your password check, including the place where the file is closed.
albrat #17
Posted 04 July 2013 - 05:36 AM
ulog:read()

You call a function here, could you paste a snippet with this function please. I am assuming that you open a file and read from it here… (this could be why you can not delete the file hence the Access Denied)

Also try adding "./users/" ..

and "./pending/" ..


Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh… I see the error now while typing this.

/" gives you a string with a " in it instead of a /

try
fs.move ("users//".. userlog, "pending//".. rnd)

could be something silly like that. the interpreter could be taking "users/".. userlog, " as a single string instead of taking users/ then appending userlog
boudragon #18
Posted 04 July 2013 - 09:50 AM
immibis I pasted a snippet of the password check above… it's all in there :-p as for albrat I thought for sure your solution would work… but nope :(/> especially when I use the same kind of operations when creating a user account and it works just fine. The only difference really is with creating an account it just makes the file rather than renaming and moving one.
MudkipTheEpic #19
Posted 04 July 2013 - 10:56 AM

local pass
ulog = fs.exists("users/".. userlog,"r")
  if ulog == true then
	ulog = io.open("users/".. userlog)
        pass=ulog and ulog:read()
        ulog:close()
	if (pass and pass==userpass) then
		term.setCursorPos(16, 18)
		print(" Login Success...")
		sleep(3)
	else
		if lockdown < 2 then
		  term.setCursorPos(16, 18)
		  print(" Wrong Password ")
		  sleep(3)
			if userlog == puselog then
			  lockdown = lockdown+1
			else
			  lockdown = 0
			  puselog = userlog
			  lockdown = lockdown+1
			end
		else
		  term.setCursorPos(15, 18)
		  rnd = tostring(math.random(101,999))
		  print(" Lockdown Ref #: ".. rnd)
		  sleep(3)
                  --# I never said you had to open it to move it, I said every time you are done with a file handle, close it!
		  fs.move ("users/".. userlog, "pending/".. rnd)
		end
	end
  else
I fixed the code you gave me, if it still doesnt work its because some other part is broken.

Edit:
ulog:read()

You call a function here, could you paste a snippet with this function please. I am assuming that you open a file and read from it here… (this could be why you can not delete the file hence the Access Denied)

Also try adding "./users/" ..

and "./pending/" ..


Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh… I see the error now while typing this.

/" gives you a string with a " in it instead of a /

try
fs.move ("users//".. userlog, "pending//".. rnd)

could be something silly like that. the interpreter could be taking "users/".. userlog, " as a single string instead of taking users/ then appending userlog

You are mixing forward slashes, /, with backward slashes, \. Backward slashes are the escape characters.
boudragon #20
Posted 04 July 2013 - 11:17 AM
Ok tried… still Access Denied… :(/> I know what you meant about closing after opening… but the ONLY time the file is opened is when checking the password during that sequence… I've looked before it and there is nothing… and after it nothing exists either…

EDIT: At this point I'm willing to face scrutiny for my messy programming and pastebin the unfinished code just to get the darn thing working LMAO! Would that help? :-p
MudkipTheEpic #21
Posted 04 July 2013 - 11:33 AM
Ok tried… still Access Denied… :(/>/> I know what you meant about closing after opening… but the ONLY time the file is opened is when checking the password during that sequence… I've looked before it and there is nothing… and after it nothing exists either…

EDIT: At this point I'm willing to face scrutiny for my messy programming and pastebin the unfinished code just to get the darn thing working LMAO! Would that help? :-p

Yes, yes it would.
boudragon #22
Posted 04 July 2013 - 11:40 AM
Ok… here is the pastebin ID… I know it's not "pretty" but for the most part it works… and being a beginner I think I'm doing ratehr well LOL!

p9TEjd7x
boudragon #23
Posted 06 July 2013 - 07:55 PM
??? Did I lose my tech support? LOL
boudragon #24
Posted 07 July 2013 - 03:43 PM
Ok after MANY hours of manipulating my code and google searches I managed to solve the problem. Turns out with all the if/then statements and such I had some of my equations either in or out of loops where the didn't belong and such… long story but all that matters now is it works LOL! Thanx for the help!