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

Can you make a program copy itself

Started by hypercharge2, 20 July 2016 - 10:34 PM
hypercharge2 #1
Posted 21 July 2016 - 12:34 AM
I would like to know if you are able to make a program copy itself from a floppy disc to a computer and vise versa.
Emma #2
Posted 21 July 2016 - 02:00 AM
Mhm, just use fs.copy(), for example:

fs.copy("/absolute/path/to/your/program", "/absolute/path/to/new/location")
--ex
fs.copy("/disk1/mycoolprogram", "/mycoolprogram")
Of course, you probably don't want to hardcode the path to your program, what you can do is call shell.getRunningProgram() like so:

fs.copy(shell.getRunningProgram(), "/mycoolprogram")
Or if you want to preserve the name already given to it you can use fs.getName() which returns the final component of a path (the file's name)

fs.copy(shell.getRunningProgram(), "/" .. fs.getName(shell.getRunningProgram())) --A forward slash is prepended to make sure it gets placed in root, you can change this if you would like
hypercharge2 #3
Posted 21 July 2016 - 02:33 AM
Thank you so much! :D/>