ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: mrdpenguin on January 08, 2017, 06:11:13 PM

Title: Pad beginning of returned tag value
Post by: mrdpenguin on January 08, 2017, 06:11:13 PM
I have a complex file renaming methodology that I use where I have a bunch of fields that I require to all be fixed width. One of those fields is the FileIndex. This is for CR2 file renaming. When I use the -filename option to rename the file and use the FileIndex as part of the new file name, is there a way to force it to be fixed length and pad the beginning with a defined number of zeros?
Title: Re: Pad beginning of returned tag value
Post by: StarGeek on January 08, 2017, 07:02:42 PM
There's a few ways to do it.  For example, to pad to 5 digits
"-Filename<${FileIndex;$_=sprintf('%05d',$_)}"

"-Filename<${FileIndex;$_=substr('00000'.$_,-5)}"

"-Filename<${FileIndex;$_='0' x (5-length).$_}"

Necro Edit: The first method drops trailing digits if too long.  For example, if FileIndex was 123456, then the result would be 12345.  The other two drop leading digits, e.g. 123456 would become 23456.