I'm trying to use ImageMagick from the command line in Windows 7 to convert many images at once. I would like to use the -set option interface, but I'm getting unexpected results. A short example of some of the weirdness I'm seeing is this:
C:\>convert example.jpg -verbose -set filename:foo hello -set filename:bar world "%[filename:foo]_%[filename:bar].png"
example.jpg=>helloworld.png JPEG 352x264 352x264+0+0 8-bit DirectClass 131KB 0.125u 0:00.135
Notice that the underscore character is mysteriously deleted from the output filename. I can use a caret (^) to escape the second percent sign in order to retain the underscore:
C:\>convert example.jpg -verbose -set filename:foo hello -set filename:bar world "%[filename:foo]_^%[filename:bar].png"
example.jpg=>hello_world.png JPEG 352x264 352x264+0+0 8-bit DirectClass 131KB 0.125u 0:00.126
What's going on here? Why does inserting the caret cause the underscore to be retained? Is the caret (^) even the correct escaper to be trying? Is this behavior due to command shell environment variable expansion or does the weirdness result from the way ImageMagick handles the percent escapes? Either way, are there general principles regarding use of percent symbols in command line args that can be followed to cause predictable behavior?
What I actually want to do is a version of the above command with many more properties set, that can handle very filenames with special characters like spaces and percent symbols, but it seems foolish to try that if I can't understand this much simpler example. Ultimately, I'll need to execute the command from a .NET program.
I've looked at http://www.robvanderwoude.com/escapechars.php and Escaping %’s in file-/folder-names at the command-line but could not figure out how to apply the advice to my problem.
Update
Adding some more examples to answer questions in comments from @dbenham and @Synetech.
Moving the underscore from before the second percent symbol, as in the latter example above, to before the underscore, produces a file named hello^world.png
:
C:\>convert example.jpg -verbose -set filename:foo hello -set filename:bar world "%[filename:foo]^_%[filename:bar].png"
example.jpg=>hello^world.png JPEG 352x264 352x264+0+0 8-bit DirectClass 131KB 0.141u 0:00.132
Caret-escaping the surrounding quotation marks always always results in helloworld.png
, regardless of where carets are placed within:
C:\>convert example.jpg -verbose -set filename:foo hello -set filename:bar world ^"^%[filename:foo]_^%[filename:bar].png^"
example.jpg=>helloworld.png JPEG 352x264 352x264+0+0 8-bit DirectClass 131KB 0.109u 0:00.115
C:\>convert example.jpg -verbose -set filename:foo hello -set filename:bar world ^"%[filename:foo]_^%[filename:bar].png^"
example.jpg=>helloworld.png JPEG 352x264 352x264+0+0 8-bit DirectClass 131KB 0.141u 0:00.143
C:\>convert example.jpg -verbose -set filename:foo hello -set filename:bar world ^"^%[filename:foo]_%[filename:bar].png^"
example.jpg=>helloworld.png JPEG 352x264 352x264+0+0 8-bit DirectClass 131KB 0.125u 0:00.126
C:\>convert example.jpg -verbose -set filename:foo hello -set filename:bar world ^"%[filename:foo]_%[filename:bar].png^"
example.jpg=>helloworld.png JPEG 352x264 352x264+0+0 8-bit DirectClass 131KB 0.125u 0:00.131
C:\>convert example.jpg -verbose -set filename:foo hello -set filename:bar world ^"%[filename:foo]^_%[filename:bar].png^"
example.jpg=>helloworld.png JPEG 352x264 352x264+0+0 8-bit DirectClass 131KB 0.125u 0:00.111
^"^%[filename:foo]_^%[filename:bar].png^"