Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- ==============
- COPY (1.0.0)
- ==============
- made by markman4897
- ~ This is a simple program for copying files and folders
- Arguments:
- - <required argument> source path
- - <required argument> destination path
- How to use:
- - call the file with 'copy "source path" "destination path"
- Comments:
- -
- TODO:
- - figure out when you need / and when not and then code it so you never have to but still can
- --]]
- -- =============
- -- Variables
- -- =============
- local tArgs = {...}
- exceptions = {
- "disk",
- "rom"
- }
- -- ==================
- -- Main functions
- -- ==================
- -- checks if the file/folder is in the exceptions table
- local function check(x)
- for k,v in pairs(exceptions) do
- if x == v then
- --print("exception!")
- return false
- end
- end
- return true
- end
- -- main copy function
- local function copy(source, destination)
- -- collects files from source folder into a table, or just inserts the source file
- if fs.exists(source) then
- -- if source is a directory put all files and subfolders into the table variable
- if fs.isDir(source) then
- table = fs.list(source)
- -- if source is a file, just copy the file and we're done
- else
- -- disect source into a table so we get only the filename (after the last "/")
- local t = {}
- for i in string.gmatch(source, "[^/]+") do
- table.insert(t, i)
- end
- local target = destination.."/"..t[#t]
- fs.copy(source, target)
- end
- -- if source is an invalid path return error
- else
- print("Invalid source file")
- return false
- end
- -- go through every key in the table
- for num,key in ipairs(table) do
- --print("Checking:"..key)
- --read()
- -- checks if the file/folder isn't in the exceptions table
- if check(key) then
- local a = source.."/"..key
- local b = destination.."/"..key -- funny thing... "/disk///test//" is the same as "disk/test"
- -- if key is a folder, recursively call this program with that folder as a parameter
- if fs.isDir(a) == true then
- --print("into recursion we go...")
- copy(a, b)
- -- if key is a file, call the copy function
- else
- fs.copy(a, b)
- print("Copied: "..a.." =")
- end
- end
- end
- return true
- end
- -- ================
- -- Main program
- -- ================
- -- print help if there are no arguments or if the argument is help
- if tArgs[1] == nil or tArgs[1] == "help" then
- print("Usage: copy <source> <destination>")
- print("- You can copy files or folders and it works recursively")
- print("- Folders like 'rom' and 'disk' are excluded by default")
- -- if argument 2 isnt nil then procede with copying
- elseif tArgs[2] ~= nil then
- -- call the copy function
- if copy(tArgs[1], tArgs[2]) then
- -- if there were no errors while copy function
- print("Copying complete.")
- else
- -- if there were errors in copy function
- print("Something went wrong.")
- end
- -- if there is the first argument, but no second (i think thats the only way...)
- else
- print("You forgot to write the destination.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement