Advertisement
Onionnion

Automatic File Copier

Feb 8th, 2012
1,724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.63 KB | None | 0 0
  1. ---------------------------
  2. -- Automatic File Copier --
  3. --        For CC         --
  4. ---------------------------
  5. -- Written by Onionnion  --
  6. --------------------------------------------------
  7. -- This script is released with the CC license! --
  8. ---------------- Go crazy with it! ---------------
  9. --------------------------------------------------
  10.  
  11. function resetScreen(quitQ,numberMade,label) -- function that resets the screen
  12.     term.clear()           -- clears terminal
  13.     term.setCursorPos(1,1) -- sets cursor's position
  14.     if quitQ == true then  --if quitQ is true, which starts out as false at line 10
  15.         if label == nil then
  16.             print("Current disk label: numerical")
  17.         elseif label == 0 then
  18.             print("Current disk label: None")
  19.         else
  20.             print("Current disk label: "..label)
  21.         end
  22.         print("Copies Made: "..tostring(numberMade)) -- prints total made
  23.         print("Press L to change the label.")
  24.         print("Press C to change the copying operation.")
  25.         print("Press Q to quit.")
  26.     end
  27. end
  28.  
  29. function labeler()
  30.     while true do
  31.         resetScreen()
  32.         print("How would you like label the disks?\n(Enter number for selection.)")
  33.         print("1: Number (1, 2, 3..etc.)")
  34.         print("2: Custom Label")
  35.         print("3: No label")
  36.         local whatDo = read()
  37.         if whatDo == "1" then
  38.             label = nil
  39.             break
  40.         elseif whatDo == "2" then
  41.             resetScreen()
  42.             write("Enter the label: ")
  43.             label = read()
  44.             break
  45.         elseif whatDo == "3" then
  46.             label = 0
  47.             break
  48.         else
  49.             resetScreen()
  50.             print("Not a correct input, try again")
  51.             sleep(2)
  52.         end
  53.     end
  54.     resetScreen(quitQ,numberMade,label)
  55. end
  56.  
  57. function copyChange()
  58.     while true do -- file I/O
  59.         resetScreen()
  60.         write("File to Copy: ")
  61.         fileIn = read()  -- input file
  62.         write("Location/Name to copy to: ")
  63.         fileOut = read() -- output file
  64.         if fs.exists(fileIn) == false or fs.exists(fileOut) == true then -- if input file isn't there or if output file already exists
  65.             resetScreen()
  66.             print("Invalid inputs! Try again.")
  67.             print("(Maybe file incorrect file to be copied or file to be created is already there?)")
  68.             sleep(2)
  69.         else
  70.             break
  71.         end
  72.     end
  73.     resetScreen(quitQ,numberMade,label)
  74. end
  75.  
  76. local side = ""      -- side disk drive is on
  77. local numberMade = 0 -- keeps track of how many were made
  78. local quitQ = false  -- quitQ is going to be short for quit-Question, meaning if false,
  79.                      --   do not display the option to quit yet until able to
  80.                      
  81. resetScreen(quitQ)
  82. print("Insert a disk into the drive to be used.")
  83. while true do
  84.     local event, param = os.pullEvent()
  85.     if event == "disk" then
  86.         side = param
  87.         break
  88.     end
  89. end
  90.  
  91. labeler()
  92. copyChange()
  93. quitQ = true -- sets quitQ to true
  94.                     -- it's always a good idea to keep comments orderly like you see here and above
  95.  
  96. while true do
  97.     resetScreen(quitQ,numberMade,label)
  98.     event, param = os.pullEvent()
  99.     if event == "disk" then
  100.         if fs.exists(fileOut) == true then -- if the file is already there (can't overwrite with cp!)
  101.             print("File already there! Please insert another disk.")
  102.             print("Press any button to eject and continue.")
  103.             while true do
  104.                 local event = os.pullEvent()
  105.                 if event == "char" or event == "key" then
  106.                     disk.eject(side)
  107.                     resetScreen(quitQ,numberMade)
  108.                     break
  109.                 end
  110.             end
  111.         else
  112.             fs.copy(fileIn, fileOut) -- copies with variables
  113.             if label ~= nil and label ~= 0 then
  114.                 disk.setLabel(side, label)
  115.             elseif label == 0 then
  116.                 ---
  117.             else
  118.                 disk.setLabel(side, tostring(numberMade + 1))
  119.             end
  120.             numberMade = numberMade + 1 -- add one to the count
  121.             disk.eject(side)
  122.             resetScreen(quitQ,numberMade,label)
  123.         end
  124.     end
  125.     if event == "char" and param == "l" then
  126.         labeler()
  127.     elseif event == "char" and param == "c" then
  128.         copyChange()
  129.     elseif event == "char" and param == "q" then -- test if quit button was pressed
  130.         break -- break exits the loop; jumps right down to the 'print'
  131.     end
  132. end
  133.  
  134. quitQ = false
  135. resetScreen(quitQ)
  136. print("Bye!")
  137. sleep(1)
  138. resetScreen(quitQ)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement