tfeldmann / organize

The file management automation tool.
http://organize.readthedocs.io
MIT License
2.33k stars 134 forks source link

Folder instructions in filename? #52

Closed F5lx6k564f3 closed 4 years ago

F5lx6k564f3 commented 4 years ago

I would like to include path/folder-instructions into the filename because I have a lot of different files (and there are always new categories added) I don't want create rules for. For example my filename is '2019_Jobs_CategoryA_TagB_A-Media_content-name_V01_draft_eng.docx' which means: Move the file to the folder '2019/Jobs/CategoryA/TagB/Media/drafts/eng' whereby 'A-' is an additional instruction and should be removed from the filename afterwards ('2019_Jobs_CategoryA_TagB_content-name_V01_draft_eng.docx').

I have a rough idea to figure it out with python but I'm new to it (see below a sketch). Is there a possibility to use such variables, conditions etc. with organizer natively? If no, is it possible to do it with Python in Organizer at all?

  1. Transform file-string into array
  2. Search for 'A-...', 'V...' and 'content-name' and get index of values
  3. remove value 'A-... and 'content-name' of array
  4. build new filename string
  5. remove value 'V...' and 'A-' of array
  6. build folder-path string (convert _ to /) etc.
tfeldmann commented 4 years ago

Yes, this can be achieved with organize. You can use a python-filter to parse the instructions and generate the new path.

For example:

rules:
- folders: "~/Documents/"
  filters:
    - extension:
      - pdf
      - docx
    - filename:
        contains: "_"
    - python: |
        import os
        parts = []
        instructions = dict()
        for part in path.stem.split("_"):
            if part.startswith("A-"):
                instructions["A"] = part[2:]
            elif part.startswith("V-"):
                instructions["V"] = part[2:]
            elif part.startswith("content-name"):
                instructions["content"] = part[12:]
            else:
                parts.append(part)
        return {
            "new_path": os.path.join(*parts),
            "instructions": instructions,
        }
  actions:
    - echo: "New path: {python.new_path}"
    - echo: "Instructions: {python.instructions}"
    - echo: "Value of A: {python.instructions.A}"
    - move: "~/Documents/{python.new_path}/{path.name}"
tfeldmann commented 4 years ago

I'm closing this issue - if you have any issues with this feel free to reopen

F5lx6k564f3 commented 4 years ago

No issues at the moment. Thanks!