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

[help] my code isnt working?

Started by Exerro, 31 August 2012 - 09:58 PM
Exerro #1
Posted 31 August 2012 - 11:58 PM
im making a program that has to copy files but fs.copy just returns access denied so im trying to use this code to replace it:

k = fs.open("sketch-it/saves/"..files[openSel+topLimit-1].."/xpos","r")
z = k.readAll()
k.close()
m = fs.open("sketch-it/movies/"..fileName.."/"..files[openSel+topLimit-1].."/xpos", "w")
m.write(z) -- it sais theres an error here
m.close()
it sais attempt to index(a nil value)
Exerro #2
Posted 01 September 2012 - 12:06 AM
now i just found out that all the other functions using fs.open have broken when they were working before…anyone know whats going on?
Chain #3
Posted 01 September 2012 - 01:53 PM
the problem wich i encountert in my program too is, you are in the wrong directory. The lua script will try to open the file inside the world savegame and the corrosponding folder with a number (dont know how to explain it). Just use something like this.

Here is the code I used to get the directory of the script and strip the filename from it.

local pathFolder = string.gsub(tostring(shell.resolveProgram("buildingslave"), "buildingslave", "buildingTemplates")

"buildingslave" is my program name.
I replace it with the folder name wich I want to use for my path.

you could do this to just get the folder where your script is in.

local pathFolder = string.gsub(tostring(shell.resolveProgram("buildingslave"), "buildingslave", "")

then just add the rest of the path normally for example in your code:

k = fs.open(string.gsub(tostring(shell.resolveProgram("<YourProgramName>"), "<YourProgramName>", "").."/sketch-it/saves"..files[openSel+topLimit-1].."/xpos", "r")

then it should work.

greets Chain
Exerro #4
Posted 01 September 2012 - 02:10 PM
the problem wich i encountert in my program too is, you are in the wrong directory. The lua script will try to open the file inside the world savegame and the corrosponding folder with a number (dont know how to explain it). Just use something like this.

Here is the code I used to get the directory of the script and strip the filename from it.

local pathFolder = string.gsub(tostring(shell.resolveProgram("buildingslave"), "buildingslave", "buildingTemplates")

"buildingslave" is my program name.
I replace it with the folder name wich I want to use for my path.

you could do this to just get the folder where your script is in.

local pathFolder = string.gsub(tostring(shell.resolveProgram("buildingslave"), "buildingslave", "")

then just add the rest of the path normally for example in your code:

k = fs.open(string.gsub(tostring(shell.resolveProgram("<YourProgramName>"), "<YourProgramName>", "").."/sketch-it/saves"..files[openSel+topLimit-1].."/xpos", "r")

then it should work.

greets Chain
is <yourProgramName> supposed to be the program that the code is in…so in my case sketch-it/moviemaker
Chain #5
Posted 01 September 2012 - 02:18 PM
the problem wich i encountert in my program too is, you are in the wrong directory. The lua script will try to open the file inside the world savegame and the corrosponding folder with a number (dont know how to explain it). Just use something like this.

Here is the code I used to get the directory of the script and strip the filename from it.

local pathFolder = string.gsub(tostring(shell.resolveProgram("buildingslave"), "buildingslave", "buildingTemplates")

"buildingslave" is my program name.
I replace it with the folder name wich I want to use for my path.

you could do this to just get the folder where your script is in.

local pathFolder = string.gsub(tostring(shell.resolveProgram("buildingslave"), "buildingslave", "")

then just add the rest of the path normally for example in your code:

k = fs.open(string.gsub(tostring(shell.resolveProgram("<YourProgramName>"), "<YourProgramName>", "").."/sketch-it/saves"..files[openSel+topLimit-1].."/xpos", "r")

then it should work.

greets Chain
is <yourProgramName> supposed to be the program that the code is in…so in my case sketch-it/moviemaker

when your lua file has the name sketch-it/moviemaker then it goes there without < > .