Win Bat Script - Filename.pdf + filename.xml into Exiftool?

Started by Stephen Marsh, October 05, 2016, 04:29:58 AM

Previous topic - Next topic

Stephen Marsh

#15
That worked great Phil. The regular expression bit is intriguing as that offers more flexibility in automatically processing an entire directory.

If for example the only constant was the leading numbers and then the underscore and other text, how would that work?

I am new to both ExifTool and Regular Expressions, however I know enough regex to hack this together:

^\d+ or ^(\d+)

or should I be adding forward slashes around that pattern?

/^\d+/

Which should select all digits up to the underscore:

1234_some_random_file-name.pdf
9876543210_another-file name.pdf

Or should I be trying to select the opposite, all characters after the digits (while retaining the file extension)? And if so, how? (?<=^\d+).+ does not work...

I have tried to use these regexes however I must be getting the perl syntax wrong as I have an error that my regex does not exist for a -tagsfromfile operation.

So rather than your example of truncating the trailing 8 characters off the PDF filename so that it matched the source XML filename, how would one just select the variable leading digits up until the underscore (which does not rename the output file, it is just a temporary rename while processing if I understand things correctly):

1234.pdf
9876543210.pdf

(which would then match the 1234.xml and 9876543210.xml files in the other specified directory)

Phil Harvey

Sorry, but the regular expressions can not be used in the argument to -tagsFromFile.  There are just a few basic exiftool-specific formatting codes that may be used here.  So I think you'll probably have to do it one file at a time in exiftool.

- 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 ($).

Stephen Marsh

#17
That is a shame Phil about no regex with -tagsFromFile. If the goal was to automate a directory, I was thinking:

1. Preserve the current filename in metadata
"-XMP-xmpMM:PreservedFileName<${filename} -r"

2. Batch rename using regex to remove everything after the digits
9876_filenametogo.pdf to 9876.pdf so that the PDF matches the XML file naming convention 9876.xml

3. inject the required metadata from the XML files into the PDF files

4. If necessary, revert the PDF files back to their original name
"-FileName<XMP-xmpMM:PreservedFileName" -r

The various .bat files could be triggered to sequentially run from a scheduled task a minute apart from each other for example (steps 1 and 4 being optional).

Step 2 is where I am a little unsure...

Hayo Baan

For step two, it's been a while since I renamed stuff on the Windows command-line, but wouldn't something as simple as ren *.* ????.* work?
Hayo Baan – Photography
Web: www.hayobaan.nl

Stephen Marsh

#19
That could be an option Hayo, the leading digits may not only be 4 digits though, which is why I was looking for an equivalent of a greedy regex for \d+

1234_some-random-file_name.pdf > 1234.pdf

9876543_anotherfilename.pdf > 9876543.pdf

The one constant is that there will be any length of digits followed by an underscore separator. All I want is the variable length digits and to strip the underscore and other characters, preserving the extension. The other constant is that the "paired" XML file only has the digits such as 1234.xml or 9876543.xml

I thought that I could just use ExifTool commands (as no -tagsFromFile is being used in that step) to either rename and/or move or copy the files from DIR-A to DIR-B with the altered regex filename, without altering any metadata at that step?

Hayo Baan

OK, I see. This can indeed be done with exiftool. For instance
exiftool -filename"<${filename;s/(\d+).*(\..*)/$1$2/;}" FILES
should do the trick.
Hayo Baan – Photography
Web: www.hayobaan.nl

Stephen Marsh

Quote from: Hayo Baan on October 10, 2016, 07:39:40 AM
OK, I see. This can indeed be done with exiftool. For instance
exiftool -filename"<${filename;s/(\d+).*(\..*)/$1$2/;}" FILES
should do the trick.


Thank you Hayo, that worked great!