Main Menu

Padding numbers

Started by Juanca1944, May 21, 2024, 05:42:22 PM

Previous topic - Next topic

Juanca1944



Hello everyone,

Thanks to finding this wonderful tool, I started reorganizing my music collection. Now I have the possibility to have the correct title and also add the track number, but there are several formats: 3, 03, 3/18, and I want to normalize them to the 03 format, meaning with a leading 0 if necessary.
 
This works in Perl: perl -e 'printf "%02d\n", 3', but when I try to use it with exiftool, I get an error
exiftool -r -ext mp3 '-testName< ${track;sprintf('%02d')} - ${title;}.%e' file
"Warning: Numeric variables with more than one digit may not start with '0' for 'track'",
and I haven't been able to find a solution.

I appreciate anyone who can help me.

Best regards.

StarGeek

You have to do the same thing with the quotes as you did in the Perl command, single quotes outside, double quotes inside. That is assuming you're using Mac/Linux.

exiftool -r -ext mp3 '-testName< ${track;sprintf("%02d")} - ${title;}.%e' file

Or escape the interior quotes... at least I think that would work
exiftool -r -ext mp3 '-testName< ${track;sprintf(\'%02d\')} - ${title;}.%e' file

Another option using the Perl Repetition Operator x, no other quotes needed
exiftool -r -ext mp3 '-testName< ${track;$_=0 x(2-length).$_} - ${title;}.%e' file
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Phil Harvey

I don't see how your track sprintf expression will work.

Try this:  ${track;$_=sprintf("%02d",$_)}

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

StarGeek

Ooops, I didn't look closely enough at the rest of it.  I just saw the quotes.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Juanca1944

Hello everyone,

First of all, thank you all for your time and for sending your posts.

I did the test, and the result is as follows:

StarGeek's third option works perfectly.

Phil Harvey's option obviously works.

I was also doing my own research, and Regex did the magic
exiftool -r -ext mp3 '-testName< ${track;s/\b(?=\d\b)/0/g;} - ${title;}.%e' FILE

Thanks again and best regards

8->jcf