Unexpected extra spaces in filenames when using custom filename format in Zotero 7


Description:
I'm using Zotero 7 on Linux and have set up a custom filename format for renaming PDF files. The format is designed to follow these rules:

1. Include the family name and initialized given name of the first creator, followed by a dot.
2. Include the year, followed by a dot.
3. Include the item type (for books) or journal abbreviation (for other types), followed by a dot.
4. Replace Windows/Linux forbidden characters in the filename:
- Replace <>:/\| with "-"
- Remove "?*
- Replace all spaces with "_"

Current "Customize Filename Format":
```
{{ creators name="family-given" initialize="given" max="1" name-part-separator="" }}
{{ year suffix="." }}
{{ if {{ itemType == "book" }} }}
{{ itemType suffix="." }}
{{ else }}
{{ journalAbbreviation suffix="." }}
{{ endif }}
{{ if {{ title match="[<>:/\|]" }} }}
{{ title replaceFrom="(<|>|:|/|\\|\|)" replaceTo="-" regexOpts="g" truncate="255" }}
{{ endif }}
{{ if {{title match="["?*]"}} }}
{{ title replaceFrom="("|?|*)" replaceTo="" regexOpts="g" truncate="255" }}
{{ endif }}
{{ if {{title match=" "}} }}
{{ title replaceFrom="(\s+)" replaceTo="_" regexOpts="g" truncate="255" }}
{{ endif }}
```

Issue:
The resulting filenames contain unexpected extra spaces. For example:
```
AuthorB.2024. book. The_Book_Title.pdf
```
There are extra spaces before "book" and "The" in the filename.

Questions:
1. How can I modify the format expression to eliminate these extra spaces?
2. Are there any errors in the regex expressions that need to be corrected?

Thank you.
  • Replace Windows/Linux forbidden characters in the filename
    Just on this, note that this shouldn't be necessary. Zotero already makes sure that filenames are valid across platforms.
  • Thanks. When I was using Zotero 6, it occasionally failed to remove characters like ':', leading to sync issues between Windows and Linux. So I used the ZotFile plugin to handle these special characters during file renaming. It's great to hear that Zotero 7 now manages this automatically.

    BTW, I noticed that the 'Customize Filename Format' shared earlier has some issues. It reproduces multiple titles if more than two conditions are met. I also found the cause of the extra spaces—they’re due to leading spaces at the beginning of each line. Since Zotero 7 can now handle special characters in filenames, the format can be rewritten as follows:

    Current "Customize Filename Format":
    ```
    {{ creators name="family-given" initialize="given" max="1" name-part-separator="" }}
    {{ year suffix="." }}
    {{ if {{ itemType == "book" }} }}
    {{ itemType suffix="." }}
    {{ else }}
    {{ journalAbbreviation suffix="." }}
    {{ endif }}
    {{ title replaceFrom="(\s+)" replaceTo="_" regexOpts="g" truncate="255" }}
    ```
  • edited August 16, 2024
    It's great to hear that Zotero 7 now manages this automatically.
    This isn't new in Zotero 7. Zotero has always removed colons and everything else listed as reserved on Wikipedia. (Here's a line from 2008 doing so.) If you were using ZotFile for renaming, it might not have enforced that in the same way.
Sign In or Register to comment.