organize icon indicating copy to clipboard operation
organize copied to clipboard

filecontent filter + rename action = multiple renames

Open drgrandios opened this issue 3 years ago • 4 comments
trafficstars

I have the following rule running on a current version of organize (installed via pip):

  # try to extract the first date from the file and rename it accordingly
  - name: date_rename
    locations: /scan/ocr
    filters:
      - name
      - extension: pdf
      - filecontent: '(?P<m>[0123]\d)\.(?P<d>[01]\d)\.(?P<y>[12][09]\d\d)'
    actions:
      - rename: "{filecontent.y}-{filecontent.m}-{filecontent.d}_{name}.pdf"

Unfortunately after "rename", the filter triggers on the renamed file again and starts the action over and over... it seems to run 5 times and then proceeds with the next rule:

scan/ocr
  20220401_173738.pdf
    - (rename) Rename to /scan/ocr/2022-23-03_20220401_173738.pdf
  2022-23-03_20220401_173738.pdf
    - (rename) Rename to /scan/ocr/2022-23-03_2022-23-03_20220401_173738.pdf
  2022-23-03_2022-23-03_20220401_173738.pdf
    - (rename) Rename to /scan/ocr/2022-23-03_2022-23-03_2022-23-03_20220401_173738.pdf
  2022-23-03_2022-23-03_2022-23-03_20220401_173738.pdf
    - (rename) Rename to /scan/ocr/2022-23-03_2022-23-03_2022-23-03_2022-23-03_20220401_173738.pdf
  2022-23-03_2022-23-03_2022-23-03_2022-23-03_20220401_173738.pdf
    - (rename) Rename to /scan/ocr/2022-23-03_2022-23-03_2022-23-03_2022-23-03_2022-23-03_20220401_173738.pdf

Is there a way to stop the rule from targeting the renamed file again?

drgrandios avatar Apr 01 '22 15:04 drgrandios

This is a bug and shouldn't happen.

tfeldmann avatar Aug 04 '22 14:08 tfeldmann

I'm experiencing the same issue with the following:

rules:
  - name: "Renaming DVD folders"
    locations:
      - path: "/mnt/backups/DVDs"
        max_depth: null
    targets: dirs
    subfolders: true
    filters:
      - name:
          contains: "DVD"
    actions:
      - echo: "{path}"
      - rename:
          name: "{name.replace('[DVD] ','')}-{name.replace(' ', '_')}-{name.replace(' [1080p]','')}"
          on_conflict: "overwrite"

telometto avatar Aug 12 '22 08:08 telometto

@drgrandios Your issue seems like a bug where files are handled multiple times where they shouldn't. It's on my todo.

@telometto I replicated your issue and found this is more like a documentation issue 😅 What you're trying to do is this:

    actions:
      - echo: "{path}"
      - rename:
          name: "{name | replace('[DVD] ','') | replace(' [1080p]','') | replace(' ', '_')}"
          on_conflict: "overwrite"

{name.replace(sth, other) takes the name and replaces sth with other but works from the full name every time. By using pipes as in the example above you can chain multiple renames.

EDIT: You can also chain like this:

name: "{name.replace('[DVD] ','').replace(' [1080p]','').replace(' ', '_')}"`

tfeldmann avatar Aug 12 '22 14:08 tfeldmann

Worked a charm! Appreciate it 😀

telometto avatar Aug 12 '22 19:08 telometto

@drgrandios Thanks for reporting and sorry for the late reply. I kind of forgot about the first problem in this issue! This should be fixed in organize v3 because now all generated files are excluded for the rest of the rule.

tfeldmann avatar Jan 07 '24 18:01 tfeldmann