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

[Lua] Turtle copying on placement

Started by W00dyR, 27 December 2012 - 04:24 AM
W00dyR #1
Posted 27 December 2012 - 05:24 AM
Hey, I've been trying something that allows me to write scripts on a main computer which has a disk drive below it, then when you place a turtle on the left of the disk drive, it copies all the programs from the disk into the turtle. Now this works fine, as long as the program doesn't exist yet. I am looking for a way to copy the programs, even if they already exist in the turtle.

If the program exists, it needs to get the version from the disk (since that will be the most recent version). I figured it has to do with an if/else and fs.delete, but I am not sure how to use it properly.

Here is my disk/startup file:


print("Copying files...")

fs.copy("disk/file1", "file1")
fs.copy("disk/file2", "file2")
fs.copy("disk/file3", "file3")
fs.copy("disk/file4", "file4")

print("Copied programs.")

rs.setOutput("top", true") -- This is because I have a lamp above 

Thanks ahead.
remiX #2
Posted 27 December 2012 - 05:47 AM
Use an if statement to check if they exist:


if fs.exists("disk/file1") then
    fs.delete("disk/file1")
end
OmegaVest #3
Posted 27 December 2012 - 05:54 AM
Well, for finding if it exists already, you can use:


if fs.exists("fileX") then
   fs.delete("fileX")
end

But, for figuring out the version numbers, that depends on what your first line is. Personally I would just make the first line of any program the version number, with a comment tag.

Sample:

-- 3.14


So, figuring that out would be something akin to this:
Spoiler

if fs.exists("fileX") then
   -- Following Section gathers the Version lines, supposing you mak it the first line of the program. -- 
   fOld = fs.open("fileX", "r")  --  Read HD File.
   fNew = fs.open("disk/fileX", "r"  --  Read DISK File
   sOldVer = fOld.readLine()  --  Get first line of HD file
   sNewVer = fNew.readLine()  --  Get first line of DISK file
   fOld.close()  --  Saves any file changes to the HD (since we use "r" instead of "w", no changes, so it just releases the file)
   fNew.close()  --  Saves any file changes to the DISK (since we use "r" instead of "w", no changes, so it just releases the file)

   -- Following Section will find the numbers, and compare them
   sOldVer = tonumber(string.sub(sOldVer, 4, #sOldVer))  --  Using the comment line I used as an example, this would grab the number 3.14, since 3 is the third char on the line, and the string is only 8 chars long.
   sNewVer = tonumber(string.sub(sNewVer, 4, #sNewVer))  -- See last line

   if sNewVer > sOldVer then  --  Comparison and replacement.
	  fs.delete("fileX")
	  fs.copy("disk/fileX", "fileX")
   end
end

But this is rough, and probably won't work properly. But even if it doesn't, you should be able to figure out how to do it from this.
W00dyR #4
Posted 27 December 2012 - 05:57 AM
Use an if statement to check if they exist:


if fs.exists("disk/file1") then
	fs.delete("disk/file1")
end


Wont this check if the file is on the disk, then delete it of the disk? The startup file is in the computer btw, not in the turtle, all that happens is that the computer copies the things from the disk onto the turtle.
W00dyR #5
Posted 27 December 2012 - 05:58 AM
Well, for finding if it exists already, you can use:


if fs.exists("fileX") then
   fs.delete("fileX")
end

But, for figuring out the version numbers, that depends on what your first line is. Personally I would just make the first line of any program the version number, with a comment tag.

Sample:

-- 3.14


So, figuring that out would be something akin to this:
Spoiler

if fs.exists("fileX") then
   fOld = fs.open("fileX", "r")
   fNew = fs.open("disk/fileX", "r"
   sOldVer = fOld.readLine()
   sNewVer = fNew.readLine()
   fOld.close()
   fNew.close()

   sOldVer = tonumber(string.sub(sOldVer, 4, #sOldVer))
   sNewVer = tonumber(string.sub(sNewVer, 4, #sNewVer))
   if sNewVer > sOldVer then
	  fs.delete("fileX")
	  fs.copy("disk/fileX", "fileX")
   end
end

But this is rough, and probably won't work properly. But even if it doesn't, you should be able to figure out how to do it from this.

Ive only recently started with ComputerCraft lua coding, and therefore I do not quite understand all the terms, but I get the general idea. I am quite sure that there is an easier way to do this though. I don't want to just copy paste the code you wrote there, since then I still wouldn't learn anything of it :P/>
OmegaVest #6
Posted 27 December 2012 - 06:00 AM
Query: When you say "On the computer", do you mean you are trying to copy from the disk to the turtle, using the computer as a controller? Because, that won't work. If, however, you intend to put the startup on the disk, then that will work.
W00dyR #7
Posted 27 December 2012 - 06:07 AM
Query: When you say "On the computer", do you mean you are trying to copy from the disk to the turtle, using the computer as a controller? Because, that won't work. If, however, you intend to put the startup on the disk, then that will work.

The program that is used to copy, is "disk/startup", so it is on the disk. I only use the computer to edit it. It sais that as well in the first post ;)/>
W00dyR #8
Posted 27 December 2012 - 06:14 AM
-
W00dyR #9
Posted 27 December 2012 - 07:16 AM
Bump, anybody else having ideas?
Lyqyd #10
Posted 27 December 2012 - 07:28 AM
I have an idea: be patient. This is a forum, not an instant messaging service. Triple-posting just to bump your own topic is highly frowned upon.

OmegaVest gave you the answer. You can't copy over existing files, so check if the file exists in the destination, and delete it first if it does. What's wrong with that answer?
W00dyR #11
Posted 27 December 2012 - 07:43 AM
I have an idea: be patient. This is a forum, not an instant messaging service. Triple-posting just to bump your own topic is highly frowned upon.

OmegaVest gave you the answer. You can't copy over existing files, so check if the file exists in the destination, and delete it first if it does. What's wrong with that answer?

Its a triple post because: First message was a reply, then 5 minutes after I said I was gone for a bit so I couldnt reply quickly, then when I got back (after an hour & the post was spammed away), I did bump it.

And yes he did, and if you would read the reply I would say that I don't quite understand what is going on in the script and that as long as I don't understand it I can't quite put it to my use now can I?
Lyqyd #12
Posted 27 December 2012 - 08:44 AM
Again, a forum isn't an instant messaging service. You don't need to tell us when you step away from the computer. An hour is not a long time. A week wouldn't even be too long. Your topic was not "spammed". You don't need to triple post.

Okay, please point out which part confuses you in the following code and I'll do my best to explain:


if fs.exists("file1") then
  fs.delete("file1")
end
fs.copy("disk/file1", "file1")

Most if the rest of the code he posted goes above and beyond what you asked for, the above is all that's necessary to complete the task.
OmegaVest #13
Posted 27 December 2012 - 08:52 AM
If the program exists, it needs to get the version from the disk (since that will be the most recent version).


My code was about this line. And I'm gonna add in some comments when I get a moment.
Lyqyd #14
Posted 27 December 2012 - 09:02 AM
If the program exists, it needs to get the version from the disk (since that will be the most recent version).


My code was about this line. And I'm gonna add in some comments when I get a moment.

I read that as, "even if there already is a copy on the turtle, it needs to be overwritten with the version on the disk."
ChunLing #15
Posted 27 December 2012 - 04:00 PM
If the newest version will always be on the disk, then you just need to do what remiX said, but delete the files on the turtle rather than on the disk.