immibis

Disk copy program

Jan 19th, 2012
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.42 KB | None | 0 0
  1. local function usage()
  2.  print("Usage: clone SRC DST")
  3.  print("SRC is the side you want to copy from.")
  4.  print("DST is the side you want to copy to.")
  5.  print("Sides are left, right, front, back,")
  6.  print("top, bottom.")
  7. end
  8.  
  9. local args = {...}
  10.  
  11. if #args ~= 2 then
  12.  return usage()
  13. end
  14.  
  15. local src = args[1]
  16. local dst = args[2]
  17.  
  18. if not disk.isPresent(src) then
  19.  return print("Source disk not present ("..src..")")
  20. end
  21. if not disk.isPresent(dst) then
  22.  return print("Destination disk not present ("..dst..")")
  23. end
  24. if not disk.hasData(src) then
  25.  return print("Source disk is not a data disk ("..src..")")
  26. end
  27. if not disk.hasData(dst) then
  28.  return print("Destination disk is not a data disk ("..dst..")")
  29. end
  30.  
  31. disk.setLabel(dst, disk.getLabel(src))
  32.  
  33. src = disk.getMountPath(src)
  34. dst = disk.getMountPath(dst)
  35.  
  36. local function recursiveErase(path)
  37.  for _,v in ipairs(fs.list(path)) do
  38.   local path2 = path.."/"..v
  39.   if fs.isDir(path2) then
  40.    recursiveErase(path2)
  41.   end
  42.   fs.delete(path2)
  43.  end
  44. end
  45.  
  46. local function recursiveCopy(src, dst)
  47.  for _,v in ipairs(fs.list(src)) do
  48.   local src2 = src.."/"..v
  49.   local dst2 = dst.."/"..v
  50.   if fs.isDir(src2) then
  51.    fs.makeDir(dst2)
  52.    recursiveCopy(src2, dst2)
  53.   else
  54.    local srcf = fs.open(src2, "r")
  55.    local dstf = fs.open(dst2, "w")
  56.    dstf.write(srcf.readAll())
  57.    dstf.close()
  58.    srcf.close()
  59.   end
  60.  end
  61. end
  62.  
  63. recursiveErase(dst)
  64. recursiveCopy(src, dst)
Advertisement
Add Comment
Please, Sign In to add comment