diff --git a/README.md b/README.md index e816e6b..0b61615 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,10 @@ Key Bind|Effect `alt + DEL`|show the list of files marked for deletion `ctrl + shift + DEL`|clear the list of marked files +The script can be configured to move files instead of deleting them. The default folder for the moved files is `C:\Users\\delete_file`. To change the defaults create a `delete_file.conf` inside of the `script-opts` folder with contents: +``` +MoveToFolder=yes +``` ## copy-paste-URL Like its name suggests - copy and paste links into mpv with `ctrl + v` to start playback of a video. This needs an open mpv window like `mpv --idle --force-window` or a window already playing a video. Also the script utilizes powershell, so that needs to be installed as well. diff --git a/delete_file.lua b/delete_file.lua index 635b1dc..dec5274 100644 --- a/delete_file.lua +++ b/delete_file.lua @@ -1,7 +1,31 @@ local utils = require "mp.utils" +require 'mp.options' + +options = {} +options.MoveToFolder = false + +if package.config:sub(1,1) == "/" then + options.DeletedFilesPath = utils.join_path(os.getenv("HOME"), "delete_file") + ops = "unix" +else + options.DeletedFilesPath = utils.join_path(os.getenv("USERPROFILE"), "delete_file") + ops = "win" +end + +read_options(options) + + del_list = {} +function createDirectory() + if not utils.file_info(options.DeletedFilesPath) then + if not os.execute(string.format('mkdir "%s"', options.DeletedFilesPath)) then + print("failed to create folder") + end + end +end + function contains_item(l, i) for k, v in pairs(l) do if v == i then @@ -30,9 +54,37 @@ function mark_delete() end function delete() + if options.MoveToFolder then + --create folder if not exists + createDirectory() + end + for i, v in pairs(del_list) do - print("deleting: "..v) - os.remove(v) + if options.MoveToFolder then + print("moving: "..v) + local _, file_name = utils.split_path(v) + --this loop will add a number to the file name if it already exists in the directory + --But limit the number of iterations + for i = 1,100 do + if i > 1 then + if file_name:find("[.].+$") then + file_name = file_name:gsub("([.].+)$", string.format("_%d%%1", i)) + else + file_name = string.format("%s_%d", file_name, i) + end + end + + local movedPath = utils.join_path(options.DeletedFilesPath, file_name) + local fileInfo = utils.file_info(movedPath) + if not fileInfo then + os.rename(v, movedPath) + break + end + end + else + print("deleting: "..v) + os.remove(v) + end end end