Advertisement
markman4897

copy.lua

Sep 8th, 2020 (edited)
88
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None
  1. --[[
  2. ==============
  3.  COPY (1.0.0)
  4. ==============
  5. made by markman4897
  6.  
  7.  ~ This is a simple program for copying files and folders
  8.  
  9. Arguments:
  10.  - <required argument> source path
  11.  - <required argument> destination path
  12.  
  13. How to use:
  14.  - call the file with 'copy "source path" "destination path"
  15.  
  16. Comments:
  17.  -
  18.  
  19. TODO:
  20.  - figure out when you need / and when not and then code it so you never have to but still can
  21.  
  22. --]]
  23.  
  24.  
  25. -- =============
  26. --   Variables
  27. -- =============
  28.  
  29. local tArgs = {...}
  30.  
  31. exceptions = {
  32.   "disk",
  33.   "rom"
  34. }
  35.  
  36.  
  37. -- ==================
  38. --   Main functions
  39. -- ==================
  40.  
  41. -- checks if the file/folder is in the exceptions table
  42. local function check(x)
  43.   for k,v in pairs(exceptions) do
  44.     if x == v then
  45.       --print("exception!")
  46.       return false
  47.     end
  48.   end
  49.  
  50.   return true
  51. end
  52.  
  53. -- main copy function
  54. local function copy(source, destination)
  55.   -- collects files from source folder into a table, or just inserts the source file
  56.   if fs.exists(source) then
  57.     -- if source is a directory put all files and subfolders into the table variable
  58.     if fs.isDir(source) then
  59.       table = fs.list(source)
  60.     -- if source is a file, just copy the file and we're done
  61.     else
  62.       -- disect source into a table so we get only the filename (after the last "/")
  63.       local t = {}
  64.       for i in string.gmatch(source, "[^/]+") do
  65.         table.insert(t, i)
  66.       end
  67.  
  68.       local target = destination.."/"..t[#t]
  69.       fs.copy(source, target)
  70.     end
  71.   -- if source is an invalid path return error
  72.   else
  73.     print("Invalid source file")
  74.     return false
  75.   end
  76.  
  77.   -- go through every key in the table
  78.   for num,key in ipairs(table) do
  79.     --print("Checking:"..key)
  80.     --read()
  81.  
  82.     -- checks if the file/folder isn't in the exceptions table
  83.     if check(key) then
  84.       local a = source.."/"..key
  85.       local b = destination.."/"..key -- funny thing... "/disk///test//" is the same as "disk/test"
  86.       -- if key is a folder, recursively call this program with that folder as a parameter
  87.       if fs.isDir(a) == true then
  88.         --print("into recursion we go...")
  89.         copy(a, b)
  90.       -- if key is a file, call the copy function
  91.       else
  92.         fs.copy(a, b)
  93.         print("Copied: "..a.." =")
  94.       end
  95.     end
  96.   end
  97.  
  98.   return true
  99. end
  100.  
  101.  
  102. -- ================
  103. --   Main program
  104. -- ================
  105.  
  106. -- print help if there are no arguments or if the argument is help
  107. if tArgs[1] == nil or tArgs[1] == "help" then
  108.   print("Usage: copy <source> <destination>")
  109.   print("- You can copy files or folders and it works recursively")
  110.   print("- Folders like 'rom' and 'disk' are excluded by default")
  111.  
  112. -- if argument 2 isnt nil then procede with copying
  113. elseif tArgs[2] ~= nil then
  114.   -- call the copy function
  115.   if copy(tArgs[1], tArgs[2]) then
  116.     -- if there were no errors while copy function
  117.     print("Copying complete.")
  118.   else
  119.     -- if there were errors in copy function
  120.     print("Something went wrong.")
  121.   end
  122.  
  123. -- if there is the first argument, but no second (i think thats the only way...)
  124. else
  125.   print("You forgot to write the destination.")
  126. end
Advertisement
RAW Paste Data Copied
Advertisement