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

[Fixed] [lua] fs.copy question

Started by altalus, 17 August 2012 - 12:35 PM
altalus #1
Posted 17 August 2012 - 02:35 PM
Hi !

I know the API documentation on fs.copy says that you must specify both files to copy and place to copy, but I seems to hit a knot when trying to copy whole directories…


local thePath = "/backups/"..diskName.."/"
print ("Backup will be done at: "..thePath)
local dirExist = fs.exists(thePath)
if dirExist == true then
  -- Delete the directory
  fs.delete(thePath)
end
-- Now we crate it.
fs.makeDir(thePath)
-- Copying all the files in it.
fs.copy("/disk", thePath)

Now everything works except the fs.copy line somehow. Can we not copy whole directory structure ? In the shell I can easily do "cp /disk /backups/test/" and it will copy it all.
OmegaVest #2
Posted 17 August 2012 - 03:15 PM
Uhn. Two of these in a week. I'm saying this may be a bug, since it seems to work for some people (such as myself) but not for others. Pretty sure you've got the syntax correct, though.
altalus #3
Posted 17 August 2012 - 04:03 PM
Hummm if you would not mind trying my little program, can you do this;

- Crate a disk.
- Label the disk (no spaces, use dash if needed !)
- Run the backup program (WARNING: I have the floppy hardcoded to "bottom", so check it if you have it elsewhere !)



--[[
This little program is made to backup all
programs on this disk to the PC HDD for
fail safe.
]]--
local tArgs={...};
local sizeX, sizeY = term.getSize()

term.clear()
term.setCursorPos(1,1)

print ("Now doing the backup of this disk")
print ("on the computer.")
print ("---------------------------------")
print ("")

-- Now we check if the directory on the PC HDD
-- exists.

local diskName = disk.getLabel("bottom") -- HARD-CODED FOR NOW; Will change for tArgs[1] later.
if diskName == nil then
print ("ERROR: No disk label found on the 'bottom' floppy drive !")
shell.exit()
end
local thePath = "/backups/"..diskName.."/"
print ("Backup will be done at: "..thePath)
local dirExist = fs.exists(thePath)

if dirExist == true then
  -- Delete the directory
  fs.delete(thePath)
end

-- Now we crate it.
fs.makeDir(thePath)

-- Copying all the files in it.
fs.copy("/disk", thePath)

print ("Backup is done. Please check if all")
print ("data is in"..thePath..".")
print ("")
sleep(1)
-- We are done here...
shell.exit()


And see if it works for you :(/>/>

Thanks !
Cloudy #4
Posted 17 August 2012 - 06:37 PM
Don't create the directory to copy to. fs.copy will automatically create the destination and copy recursively.
altalus #5
Posted 17 August 2012 - 07:48 PM
Well, I create the directory in /backups/ with the disk label, so /backups/My-OS/

Then I want to copy /disk in /backups/My-OS/ for the final destination of /backups/My-OS/disk/ where all the programs would be.

However the fs.copy() does not seems to like that.
Cranium #6
Posted 17 August 2012 - 07:50 PM
maybe it doesn't like being told that there are directories before disk/… Just a thought though, no real evidence…
Cloudy #7
Posted 17 August 2012 - 08:50 PM
The destination you are trying to copy to exists. Don't make the directory before copying.
altalus #8
Posted 17 August 2012 - 08:53 PM
The destination you are trying to copy to exists. Don't make the directory before copying.

Allright, I will test this tonight and give news :(/>/>
jay5476 #9
Posted 18 August 2012 - 03:45 AM
On line 11

fs.copy("/disk", thePath)
It should be

fs.copy("/disk", "..thePath)
you need to put it in a string becuase it is with a non string/variable
Luanub #10
Posted 18 August 2012 - 03:49 AM
On line 11

fs.copy("/disk", thePath)
It should be

fs.copy("/disk", "..thePath)
you need to put it in a string becuase it is with a non string/variable

Line 11 is correct and should work, if it does need to be converted to a string just do tostring(thePath) prior to the fs.copy. Your line is missing a " and will error. There no need to add quotes or ..'s prior to the var in this case.
altalus #11
Posted 18 August 2012 - 01:56 PM
The destination you are trying to copy to exists. Don't make the directory before copying.

I can confirm this is right. I modified the code to NOT create the directory beforehand and it actually works. Thank you Cloudy and all that tried to help :(/>/>