-p output containing question marks

Started by DavidOB, July 26, 2016, 04:40:58 AM

Previous topic - Next topic

DavidOB

Hi,
I have a problem with printing out info via exiftool (exiftool 10.23 on Windows 7 SP1 (64bit)). I played with the -p option and found something I don't understand. For testing, I made a simplified version of the command and run it on structure of four directories, containing 3 jpgs each (the same picture renamed).

This works as expected:
exiftool -r  -p "$FilePath ${FilePath;s/.*t([^g]*)/___ \1 ___/}" -ext jpg "X:\this is test"
X:/this is test/abcd/01.jpg ___ /abcd/01.jp ___g
X:/this is test/abcd/02.jpg ___ /abcd/02.jp ___g
X:/this is test/abcd/03.jpg ___ /abcd/03.jp ___g
X:/this is test/efgh/04.jpg ___ /ef ___gh/04.jpg
X:/this is test/efgh/05.jpg ___ /ef ___gh/05.jpg
X:/this is test/efgh/06.jpg ___ /ef ___gh/06.jpg
X:/this is test/ijkl/07.jpg ___ /ijkl/07.jp ___g
X:/this is test/ijkl/08.jpg ___ /ijkl/08.jp ___g
X:/this is test/ijkl/09.jpg ___ /ijkl/09.jp ___g
    4 directories scanned
    9 image files read


However, adding \" to surround the two parts of the string, I get:
exiftool -r  -p "\"$FilePath\" \"${FilePath;s/.*t([^g]*)/___ \1 ___/}\"" -ext jpg "X:\this is test"
"X:/this is test/abcd/01.jpg" "___  ___/abcd/01.jpg"
"X:/this is test/abcd/02.jpg" "___  ___/abcd/02.jpg"
"X:/this is test/abcd/03.jpg" "___  ___/abcd/03.jpg"
"X:/this is test/efgh/04.jpg" "___  ___/efgh/04.jpg"
"X:/this is test/efgh/05.jpg" "___  ___/efgh/05.jpg"
"X:/this is test/efgh/06.jpg" "___  ___/efgh/06.jpg"
"X:/this is test/ijkl/07.jpg" "___  ___/ijkl/07.jpg"
"X:/this is test/ijkl/08.jpg" "___  ___/ijkl/08.jpg"
"X:/this is test/ijkl/09.jpg" "___  ___/ijkl/09.jpg"
    4 directories scanned
    9 image files read


Why doesn't it match the 'everything but g' part of the regular expression any more?
Or - How to print quotation marks around regexp inside the -p string?

It seems to me it is somehow connected with parsing of the string as if I put even number of \" before the second part, it is OK, odd number is not OK:
exiftool -r  -p "\"$FilePath\" \"a\" \"b\" ${FilePath;s/.*t([^g]*)/___ \1 ___/}" -ext jpg "X:\this is test"
"X:/this is test/abcd/01.jpg" "a" "b" ___ /abcd/01.jp ___g
"X:/this is test/abcd/02.jpg" "a" "b" ___ /abcd/02.jp ___g
"X:/this is test/abcd/03.jpg" "a" "b" ___ /abcd/03.jp ___g
"X:/this is test/efgh/04.jpg" "a" "b" ___ /ef ___gh/04.jpg
"X:/this is test/efgh/05.jpg" "a" "b" ___ /ef ___gh/05.jpg
"X:/this is test/efgh/06.jpg" "a" "b" ___ /ef ___gh/06.jpg
"X:/this is test/ijkl/07.jpg" "a" "b" ___ /ijkl/07.jp ___g
"X:/this is test/ijkl/08.jpg" "a" "b" ___ /ijkl/08.jp ___g
"X:/this is test/ijkl/09.jpg" "a" "b" ___ /ijkl/09.jp ___g
    4 directories scanned
    9 image files read


(the reason to surround it with question marks is I want to grab the output and use it as parameters for another application and to properly push it through the system I need to cope with directories/filenames containing spaces)

Hayo Baan

With the necessary adoptions for Mac/Unix (i.e., double quotes to single quotes and no need to escape the double quotes inside), your second command just works and nicely puts the requested quotes around the files (with the substitution as requested).
exiftool -r  -p '"$FilePath" "${FilePath;s/.*t([^n]*)/___ $1 ___/}"' -ext nef .
"/Volumes/Temp/MetaDataTest/0001.nef" "___ /0001. ___nef"
"/Volumes/Temp/MetaDataTest/0002.nef" "___ /0002. ___nef"
"/Volumes/Temp/MetaDataTest/0003.nef" "___ /0003. ___nef"
"/Volumes/Temp/MetaDataTest/0004.nef" "___ /0004. ___nef"
"/Volumes/Temp/MetaDataTest/0005.nef" "___ /0005. ___nef"
"/Volumes/Temp/MetaDataTest/0006.nef" "___ /0006. ___nef"
"/Volumes/Temp/MetaDataTest/0007.nef" "___ /0007. ___nef"
"/Volumes/Temp/MetaDataTest/0008.nef" "___ /0008. ___nef"
"/Volumes/Temp/MetaDataTest/0009.nef" "___ /0009. ___nef"
"/Volumes/Temp/MetaDataTest/0010.nef" "___ /0010. ___nef"
"/Volumes/Temp/MetaDataTest/0011.nef" "___ /0011. ___nef"

(Note: I also changed \1 to $1 as that is the preferred way to add a back reference).

So the problem is not with exiftool per se, but more likely with how the windows command line parses it. This is a known issue with the windows command line but by rewriting the argument, perhaps there is a work around to include the ". E.g., like so:
exiftool -r  -p "${FilePath;s/^|$/\042/g} ${FilePath;s/.*t([^g]*)/___ $1 ___/;s/^|$/\042/g}" -ext jpg "X:\this is test"
(this additionally replaces the beginning and end of the string with \042 which is the character ", but now written as octal character code to circumvent having to include the problematic " itself)
Hayo Baan – Photography
Web: www.hayobaan.nl

StarGeek

Using the FMTFILE option of -p would probably work as well. 
* 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).

DavidOB

When surrounding something with double quotes on commandline, Windows drops the surrounding ", translates \" inside to " and then passes all that just as a single argument to the application, so I would not blame MS in this particular case; the problem must be somewhere deeper (passing to perl? windows implementation of perlre?.
However, both suggestions, injecting char as octal code via regexp as well as format file work, thank you, gentlemen!

Edit: Also, \" inserted from within the regexp works as well, no need to know octal code \042.