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

How to add code to every program in every directory.

Started by os.reboot(), 16 August 2012 - 07:02 PM
os.reboot() #1
Posted 16 August 2012 - 09:02 PM
I have a computer on a multiplayer server with hundreds of programs on there and only just realised I want to attach two lines like this on the end of my programs:

print("Coded by so-and-so.")
print("Do not redistribute without permission from so-and-so.")
But I have SOOOO many programs and I don't want to go in to the editor and write all of that at the end of every program. Some programs are thousands of lines long.

TL;DR:
So, how do I make a program that adds code to the bottom of every program?
Cranium #2
Posted 16 August 2012 - 09:05 PM
Just do it… That's what I would do…
os.reboot() #3
Posted 16 August 2012 - 09:14 PM
Oops. How do I delete this?
Edited on 16 August 2012 - 07:16 PM
os.reboot() #4
Posted 16 August 2012 - 09:15 PM
Just do it… That's what I would do…

> dir
rom chatfiles mailfiles pongsettings
advcalculator arrowshooter bettergps
chatmail chat connect2internet clock crash
dialog ftp mail pong redalarm timer

I'll become an old man before I get that done. :/
Cranium #5
Posted 16 August 2012 - 09:25 PM
Do you have access to the world save? Or is it on a server that you don't host?
os.reboot() #6
Posted 16 August 2012 - 09:28 PM
I do not have access to the world save so I can't use an external editor.

Now, please answer my question: How do I make a program that adds code to the bottom of every program automatically.
Cranium #7
Posted 16 August 2012 - 09:33 PM
The FS api is what you're looking for: http://computercraft.info/wiki/index.php?title=Fs.open
Example:

local m = {"first line of code here", "second line of code here"}
file = fs.open("programname", "a")
file.writeLine(m[1])
file.writeLine(m[2])
file.close()
I haven't tested it, but it should work.
os.reboot() #8
Posted 16 August 2012 - 09:40 PM
Thank you. I'll combine this with the format code that goes around on this forum to make it so it finds every program and adds code to it.