News:

2023-03-15 Major improvements to the new Geolocation feature

Main Menu

Interpretation of backslash in tag content

Started by mstorhas, January 09, 2012, 11:47:22 AM

Previous topic - Next topic

mstorhas

Hi Phil,

I have a question with respect to exiftool processing of backslashes. Executing exiftool -charset Latin -out out.jpg -IPTC:SupplementalCategories="Supplemental Categories\" -IPTC:CopyrightNotice="Copyright Notice" "orig.jpg" (backslash in front of ") will yield an error, whereas exiftool -charset Latin -out out.jpg -IPTC:SupplementalCategories="Supplemental \Categories" -IPTC:CopyrightNotice="Copyright Notice" "orig.jpg" (backslash in front of C) finishes without problem. Obviously backslashes in front of special characters are interpreted as escape signs, whereas those in front of regular characters are not. Is that assumption right?

I'm using the last version of exiftool under windows xp.

Thanks!
Martin

Phil Harvey

Hi Martin,

ExifTool does not treat backslashes specially.  In many command shells the backslash is used as a special character to escape other control characters.  This is a function of the shell, not ExifTool.  I don't know the exact quoting/escaping rules for the Windows command shell, but you could probably find this with some googling.

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

pb

Because Windows uses backslash as a path separator, I don' t think it's going to function as an escape character in the standard Windows shell (i.e. "cmd.exe").   Beyond that, I have no insights to offer, since it depends on what exiftool does with the backslash in that argument (e.g., whether it for example passes it to the os in a context where a path might be possible, or whether it gives it to perl in a context where perl might interpret it as an escape char).  (Come to think of it, I'm not sure how the compiled exiftool interacts with perl.)

--peter

pb

According to this:  http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx  I infer that the only escape chars in windows cmd are ^ (caret or circumflex) and " (bidirectional double quote).

--peter

mstorhas


Phil Harvey

People must run into this problem all the time in Windows shells because, as Peter points out, the backslash is used as a directory separator.

Hey.  As a matter of fact, this solves a problem that an ExifTool user was having back in August.  I've just e-mailed him an explanation.

From this reference, here are the rules for quoting command-line arguments in a Windows cmd shell:

    1. Apply the Microsoft C/C++ parsing rules
        a. replace literal  "  with   \"
                       literal \"  with  \\\"
                       literal \\" with \\\\\"
        b. enclose the whole parameter in double quotes

    2. Apply the Command Line Parser (cmd.exe) parsing rules
        a. determine what cmd.exe will see as the quoted parts
        b. escape the special characters < > | & ^ not in a double quoted part
            - the escape character for cmd.exe is ^
            - it may also be necessary to escape ( ) @ !
            - it's OK to escape everything
        c. If your command line will be placed in a batch file, double the % character8


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

pb

Quote from: Phil Harvey on January 10, 2012, 07:37:01 AM
People must run into this problem all the time in Windows shells because, as Peter points out, the backslash is used as a directory separator.

Hey.  As a matter of fact, this solves a problem that an ExifTool user was having back in August.  I've just e-mailed him an explanation.

From this reference, here are the rules for quoting command-line arguments in a Windows cmd shell:

    1. Apply the Microsoft C/C++ parsing rules
        a. replace literal  "  with   \"
                       literal \"  with  \\\"
                       literal \\" with \\\\\"
        b. enclose the whole parameter in double quotes

    2. Apply the Command Line Parser (cmd.exe) parsing rules
        a. determine what cmd.exe will see as the quoted parts
        b. escape the special characters < > | & ^ not in a double quoted part
            - the escape character for cmd.exe is ^
            - it may also be necessary to escape ( ) @ !
            - it's OK to escape everything
        c. If your command line will be placed in a batch file, double the % character8


- Phil

I was a little confused by this explanation, so let me clarify for the benefit of others (and also me, when I look at this again in 1 year).  The Windows cmd parser does NOT use the C/C++ parsing rules (step 1).  However, if the command line you are using is going to be given to a C or C++ program (and some other languages, too), you need to add the escapes required for C/C++ strings.  Once you've added those, you better make sure that the result is also escaped as required by the Windows cmd shell (step 2).

If that interpretation is wrong, please set me straight.

--peter

Phil Harvey

Hi Peter,

I don't understand your interpretation (or the explanation in the reference) because:

1) A majority of windows programs are written in C or C++.

2) The C/C++ quoting rules are interpreted by the C/C++ compiler, which is not run when a compiled program is executed.

3) The C/C++ escape rules don't apply to C/C++ programs in command lines of other operating systems.

The only thing I do know is that it works for 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 ($).