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

Copying programs via program.

Started by zedekblue, 27 May 2013 - 11:25 PM
zedekblue #1
Posted 28 May 2013 - 01:25 AM
The goal:
I have a master disk which holds all of my finalized programs. I want to be able to place a turtle down next to an adjacent computer (or turtle) and be able to copy over the programs I want without having to tediously type in "delete turtleStartup', 'copy disk/turtleStartup turtleStartup' over and over for all my turtles.
I don't know how to force CC code into another machine, so I really need help in getting this done.

Also it would be cool if somebody could figure out how I could list my programs as such:
[1] turtleStartup
[2] doorStartup
[3] barrierTurtle

etc.
in order for me to input: '1' and have it copy turtleStartup, or '2' and have it copy doorStartup.
Thanks a bunch!
Apfeldstrudel #2
Posted 28 May 2013 - 01:42 AM
You might wanna check out the fs api and the char event for input
Spongy141 #3
Posted 28 May 2013 - 10:14 AM
Run this on the turtle, make sure it is wireless

rednet.open('right') --If I remember correctly its modem is on the right side, correct me if I'm wrong
CheckID = '2' --Replace '2' with the computers ID, do [ print(os.getComputerID()) ] to print the ID of the computer
id, fileName = rednet.receive()
if id == CheckID then
  id, fileData = rednet.receive()
  file = fs.open(fileName,'w')
  file.write(fileData)
  file.close()
end
Run this on the computer

fileN = {...}
rednet.open('top') --Change top to your modem side
TurtleID = '1' --Change '1' to the turtles ID
if fileN[1] == '1' then
  file = fs.open('turtleStartup','r')
  fileData = file.readAll()
  rednet.send(TurtleID,'turtleStartup')
  sleep(0.2)
  rednet.send(TurtleID,fileData)
elseif fileN[1] == '2' then
  file = fs.open('doorStartup','r')
  fileData = file.readAll()
  rednet.send(TurtleID,'doorStartup')
  sleep(0.2)
  rednet.send(TurtleID,fileData)
elseif fileN[1] == '3' then
  file = fs.open('barrierStartup','r')
  fileData = file.readAll()
  rednet.send(TurtleID,'barrierStartup]')
  sleep(0.2)
  rednet.send(TurtleID,fileData)
end
To send the file do
 <what you called this program> <file you want to send> 
This will work for any file, but you probably will only use it for the turtle thingy.
Cranium #4
Posted 28 May 2013 - 10:57 AM
Another thing you can do is create a startup file on the disk. It can simply copy any programs from the disk to the turtle on turtle boot. Lemme make a quick and dirty example:

local sourceList = fs.list("disk")
local destList = fs.list("/")
local updates = {}
for source = 1, #sourceList do
    if not fs.isDir(sourceList[source]) then
        local pcFile = fs.open(sourceList[source], "r")
	    local sourceContents = pcFile.readAll()
	    for dest = 1, #destList do
		    if not fs.isDir(dest) then
    		    local destFile = fs.open(dest, "r")
			    local destContent = destFile.readAll()
			    if destContent ~= sourceContent then
				    table.insert(updates, {dest, source})
			    end
		    end
	    end
    end
end

if #updates > 0 then
    print("There are updates available. Would you like to update?")
    write("Y/N : ")
    local response = tolower(read())
    if response == "y" then
	    for i = 1, #updates do
		    local sourceFile = fs.open("disk/"..updates[i][2], "r")
		    local sourceContent = sourceFile.readAll() 
		    sourceFile.close()
		    local destFile = fs.open(updates[i][1], "w")
		    destFile.write(sourceContent)
		    destFile.close()
	    end
    else
	    print("No files updated")
    end
end
I haven't even tested it, but it should work.