Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overwrite_smaller move conflict option #304

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,22 @@ rules:
on_conflict: "overwrite"
```

Use a placeholder to move all .pdf files into a "PDF" folder and all .jpg files into a
"JPG" folder. Existing files will be overwritten only if they are larger than the existing file.

```yaml
rules:
- locations: ~/Desktop
filters:
- extension:
- pdf
- jpg
actions:
- move:
dest: "~/Desktop/{extension.upper()}/"
on_conflict: "overwrite_smaller"
```

Move pdfs into the folder `Invoices`. Keep the filename but do not overwrite existing files. To prevent overwriting files, an index is added to the filename, so `somefile.jpg` becomes `somefile 2.jpg`.

```yaml
Expand Down
16 changes: 16 additions & 0 deletions organize/actions/_conflict_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
CONFLICT_OPTIONS = (
"skip",
"overwrite",
"overwrite_smaller",
"trash",
"rename_new",
"rename_existing",
Expand Down Expand Up @@ -98,6 +99,21 @@ def resolve_overwrite_conflict(
dst_fs.remove(dst_path)
return dst_path

elif conflict_mode == "overwrite_smaller":
print(f"Overwrite Smaller {safe_description(dst_fs, dst_path)}.")
src_file_larger = src_fs.getsize(src_path) > dst_fs.getsize(dst_path)
if not simulate:
if dst_fs.isdir(dst_path):
dst_fs.removedir(dst_path)
elif dst_fs.isfile(dst_path):
if src_file_larger:
dst_fs.remove(dst_path)
if src_file_larger:
return dst_path
else:
print("Skipped.")
return None

elif conflict_mode == "rename_new":
stem, ext = splitext(dst_path)
name = next_free_name(
Expand Down