Here's my third shot at a template for the full path of books in a calibre library, updated with some new template functions. Thank you, chaley and kovid. The full path templates using formats_paths() are easier to construct, with better performance, and (I assume) accuracy.
The descriptions for the templates using new functions are dark blue.
Example Templates, Composite Columns, Filename or Path of Books in calibre Library. I am not concerned about path length or filename length in a library because calibre automatically stays within filesystem limitations by truncating filename or other path components when necessary, and the calibre library folder and filename structure are off-limits anyway. But some users are concerned enough to want short titles, filenames, or paths.
- Generalized Path. This first example uses no template language. It shows the exact generalized full path for any book in my primary calibre library named Prime, which is in a folder Libs in a folder Books in my home folder username on my system startup internal drive, which is formatted for HFS Plus (aka Mac OS Extended) Journaled and lives in OS X 10.8.x. My real username contains 7 characters so in the examples I shortened unboggling to 7 characters. The book id number in parentheses is automatically assigned when a book is added to a calibre library (and unique within that library). Author1 is the first author in the multi-value field Author(s).
Code:
/Users/unboggl/Books/Libs/Prime/author1/title (id)/title - author1.extension
- Shortening the Path. The folder names leading to my library Prime are purposely short to maximize remaining string length within any path limitations, but the path length could be shortened more by moving the calibre library to the first level of my user account and eliminating the folders Books and Libs, or by moving the library to root level of a different drive volume with a short name, as in /Volumes/Bk/
- Differences for Other Operating Systems and File Systems. Windows uses a volume designator such as "C:\" and a backslash between path components, while on current versions of OS X, Linux, and other variants of Unix the path component separator is usually forwardslash (depending on specific file system).
- Constant, File Extension. For templates dealing with just the filename component of the path, the file extension at the end of the filename can be approximated by using a string constant, which can be customized per user. For precision it should include the preceding dot plus all the characters of the extension. But string length of potential extensions varies. Many are 4 characters such as epub, mobi, azw3, while some are 3 characters such as rtf, txt, lit. Some are 5 or more. Most of my books have only one format, EPUB, though there are a few exceptions that also have a PDF or MOBI. To cover the file extension for most of my cases, for filename templates I just use .epub as the constant, and do not worry about possible small differences.
- First Author. In the author part of the filename for a book in a calibre library, calibre uses only author1 (the first author) out of potentially many co-authors. So author1 needs to be extracted from the list of co-authors in Author(s).
Code:
{authors:list_item(0,&)}
- Filename String. This general program mode template returns the string for the filename component of the path, contatenating the string constant .epub with the title - author1 template. The extension may be wrong due to the use of .epub as a constant. That can be changed to a different extension in the template (within the single quotes).
Code:
program: strcat(template('{title} - {authors:list_item(0,&)}'), '.epub')
- Filename Length. This general program mode template counts the number of characters in the filename component of the path. The count may be inexact due to the use of .epub as a constant. That can be changed to a different extension in the template (within the single quotes).
Code:
program: strlen(strcat(template('{title} - {authors:list_item(0,&)}'), '.epub'))
- Note, Filename Template Inexactness. This applies only to the templates above that deal just with the Filename component. In addition to the inexactness pertaining to the extension constant, there may be other differences between those filename templates and the actual exact structure of filename in a calibre library. First, calibre automatically truncates a path component for a book in a library when string length exceeds a certain limit, which may be different depending on operating system or file system. Another difference mentioned in custom column: count number of characters, Post #17) is the actual path of a book in the library may include multiple sequential spaces, while the template processor reduces any multiple sequential spaces to one space. Compare the first example of a possible title in the actual filename of a book in the library with the second example showing that same title as rendered by a {title} template.
Code:
The Spirit of Spaciness
Code:
The Spirit of Spaciness
- Path String. This general program mode template uses the new function formats_paths() and accurately (AFAIK) returns the string for the entire path, including all folder names, filename, and the extension for the format chosen as the selector key, for books in current library with that format. The selector key can be changed from EPUB to whatever format is preferred (within the single quotes).
Code:
program: select(formats_paths(),'EPUB')
- Path Length. This general program mode template uses the new function formats_paths() and accurately (AFAIK) counts the number of characters in the entire path, for books in current library with that format. The selector key can be changed from EPUB to whatever format is preferred (within the single quotes).
Code:
program: strlen(select(formats_paths(),'EPUB'))
- Current Library Path. This new template function returns the path for the current library, which in my case is /Users/unboggl/Books/Libs/Prime
Code:
{:'current_library_path()'}
- Approximate Formats. This new template function returns the list of formats from the DB for each book in current library, without referring to any book files, so it is probably accurate rather than definitely accurate. A composite column based on {:'approximate_formats()'} might help calibre start up faster than a composite column based on {formats}.
Code:
{:'approximate_formats()'}