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

Creating backups

Started by mrpoopy345, 01 December 2013 - 06:52 AM
mrpoopy345 #1
Posted 01 December 2013 - 07:52 AM
Hello! I was trying to make a program that backed up all your files into a folder named "backup" (Yes, I know what rom is, it was just for the challenge aspect.) Now, on my current code, it works, but it still gives me an error when first trying to make a backup even though the program itself worked. The error it gives me is:

backup:11: No such file.

Current code:

print("Backcup creator.")
if fs.isDir("backup") then
sleep(1)
print("Backup already made.")
else
sleep(1)
print("Creating backup...")
fs.makeDir("backup")
s = shell.programs()
for _, v in ipairs(s) do
fs.copy(v, "backup/"..v)
sleep(1)
print("Backup created.")
end
end
Any help?

Edit: New problem! It works fine, but I want it to copy all the files, even the user created ones. Any help on that front?
Edited on 01 December 2013 - 07:37 AM
theoriginalbit #2
Posted 01 December 2013 - 08:30 AM
I could be wrong, but I believe that what is returned from shell.programs also includes aliases, which aren't files, but are references to files. As such you should use fs.list instead of shell.programs


local s = fs.list("/rom/programs/")
LBPHacker #3
Posted 01 December 2013 - 08:31 AM
EDIT: And I was right! I knew I was going to be ninja'd!

Thing is, the point of a backup program is backing up all files (not just the programs) on the computer (well, except for the existing backups). List all files on the computer and copy them into a new directory (eg. string.format("~backup-%i-%i", os.day(), os.time())). fs.list is your friend.
Edited on 01 December 2013 - 07:31 AM
mrpoopy345 #4
Posted 01 December 2013 - 08:31 AM
Dank you!
mrpoopy345 #5
Posted 01 December 2013 - 08:36 AM
Edit: New problem! It works fine, but I want it to copy all the files, even the user created ones. Any help on that front?
theoriginalbit #6
Posted 01 December 2013 - 09:01 AM
don't get a list of rom, get a list of the root directory and ignore the rom and the backups directory.
Engineer #7
Posted 01 December 2013 - 09:09 AM
don't get a list of rom, get a list of the root directory and ignore the rom and the backups directory.
To extend on this, this is some actual code:

for index, filename in pairs(fs.list("/")) do
  if not fs.isReadOnly(filename) then
	  print( string.format("The file \"%s\" is created by an user!", filename))
  end
end
Edited on 01 December 2013 - 08:09 AM
mrpoopy345 #8
Posted 01 December 2013 - 01:59 PM
Dank you very much!