1
votes

I have about 600 books in PDF format where the filename is in the format:

AuthorForename AuthorSurname - Title (Date).pdf

For example:

Foo Z. Bar - Writing Scripts for Idiots (2017)

Bar Foo - Fun with PDFs (2016)

The metadata is unfortunately missing for pretty much all of them so when I import them into Calibre the Author field is blank.

I'm trying to write a script that will take everything that appears before the '-', removes the trailing space, and then adds it as the author in the PDF metadata using exiftool.

So far I have the following:

    for i in "*.pdf"; 
    do exiftool -author=$(echo $i | sed 's/-.*//' | sed 's/[ \t]*$//') "$i"; 
    done 

When trying to run it, however, the following is returned:

    Error: File not found - Z.
    Error: File not found - Bar
    Error: File not found - *.pdf
        0 image files updated
        3 files weren't updated due to errors

What about the -author= phrase is breaking here? Please could someone enlighten me?

1
when you seen that the shell has not expanded the * char, that indicates that you want to remove the dbl-quotes around *.pdf. Also, answer below looks very handy. An excellent first Q, please keep posting, but Please read stackoverflow.com/help/how-to-ask , stackoverflow.com/help/dont-ask , stackoverflow.com/help/mcve and take the tour before posting more Qs here. Good luck. - shellter
Thanks, I'll make sure to do so - NonSatiata

1 Answers

2
votes

You don't need to script this. In fact, doing so will be much slower than letting exiftool do it by itself as you would require exiftool to startup once for every file.

Try this
exiftool -ext pdf '-author<${filename;s/\s+-.*//}' /path/to/target/directory

Breakdown:
-ext pdf process only PDF files
-author the tag to copy to
< The copy from another tag option. In this case, the filename will be treated as a pseudo-tag
${filename;s/\s+-.*//} Copying from the filename, but first performing a regex on it. In this case, looking for 1 or more spaces, a dash, and the rest of the name and removing it.

Add -r if you want to recurse into subdirectories. Add -overwrite_original to avoid making backupfiles with _original added to the filename.

The error with your first command was that the value you wanted to assign had spaces in it and needed to be enclosed by quotes.