|
1 | 1 | local utils = require "mp.utils" |
2 | 2 |
|
| 3 | +require 'mp.options' |
| 4 | + |
| 5 | +options = {} |
| 6 | +options.MoveToFolder = false |
| 7 | + |
| 8 | +if package.config:sub(1,1) == "/" then |
| 9 | + options.DeletedFilesPath = utils.join_path(os.getenv("HOME"), "delete_file") |
| 10 | + ops = "unix" |
| 11 | +else |
| 12 | + options.DeletedFilesPath = utils.join_path(os.getenv("USERPROFILE"), "delete_file") |
| 13 | + ops = "win" |
| 14 | +end |
| 15 | + |
| 16 | +read_options(options) |
| 17 | + |
| 18 | + |
3 | 19 | del_list = {} |
4 | 20 |
|
| 21 | +function createDirectory() |
| 22 | + if not utils.file_info(options.DeletedFilesPath) then |
| 23 | + if not os.execute(string.format('mkdir "%s"', options.DeletedFilesPath)) then |
| 24 | + print("failed to create folder") |
| 25 | + end |
| 26 | + end |
| 27 | +end |
| 28 | + |
5 | 29 | function contains_item(l, i) |
6 | 30 | for k, v in pairs(l) do |
7 | 31 | if v == i then |
@@ -30,9 +54,37 @@ function mark_delete() |
30 | 54 | end |
31 | 55 |
|
32 | 56 | function delete() |
| 57 | + if options.MoveToFolder then |
| 58 | + --create folder if not exists |
| 59 | + createDirectory() |
| 60 | + end |
| 61 | + |
33 | 62 | for i, v in pairs(del_list) do |
34 | | - print("deleting: "..v) |
35 | | - os.remove(v) |
| 63 | + if options.MoveToFolder then |
| 64 | + print("moving: "..v) |
| 65 | + local _, file_name = utils.split_path(v) |
| 66 | + --this loop will add a number to the file name if it already exists in the directory |
| 67 | + --But limit the number of iterations |
| 68 | + for i = 1,100 do |
| 69 | + if i > 1 then |
| 70 | + if file_name:find("[.].+$") then |
| 71 | + file_name = file_name:gsub("([.].+)$", string.format("_%d%%1", i)) |
| 72 | + else |
| 73 | + file_name = string.format("%s_%d", file_name, i) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + local movedPath = utils.join_path(options.DeletedFilesPath, file_name) |
| 78 | + local fileInfo = utils.file_info(movedPath) |
| 79 | + if not fileInfo then |
| 80 | + os.rename(v, movedPath) |
| 81 | + break |
| 82 | + end |
| 83 | + end |
| 84 | + else |
| 85 | + print("deleting: "..v) |
| 86 | + os.remove(v) |
| 87 | + end |
36 | 88 | end |
37 | 89 | end |
38 | 90 |
|
|
0 commit comments