Skip to content

Commit 885b9bd

Browse files
committed
delete-file: option to move instead of delete
1 parent d5ea82a commit 885b9bd

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ Key Bind|Effect
7070
`alt + DEL`|show the list of files marked for deletion
7171
`ctrl + shift + DEL`|clear the list of marked files
7272

73+
The script can be configured to move files instead of deleting them. The default folder for the moved files is `C:\Users\<me>\delete_file`. To change the defaults create a `delete_file.conf` inside of the `script-opts` folder with contents:
74+
```
75+
MoveToFolder=yes
76+
```
7377

7478
## copy-paste-URL
7579
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.

delete_file.lua

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
local utils = require "mp.utils"
22

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+
319
del_list = {}
420

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+
529
function contains_item(l, i)
630
for k, v in pairs(l) do
731
if v == i then
@@ -30,9 +54,37 @@ function mark_delete()
3054
end
3155

3256
function delete()
57+
if options.MoveToFolder then
58+
--create folder if not exists
59+
createDirectory()
60+
end
61+
3362
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
3688
end
3789
end
3890

0 commit comments

Comments
 (0)