Quoting Issue

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Squashman
Expert
Posts: 4134
Joined: 23 Dec 2011 13:59

Quoting Issue

#1 Post by Squashman » 31 Jan 2020 23:10

I guess I have spent so much wasted time working in RedPoint that I completely forgot how to write a batch file.

This does not work.

Code: Select all

W:\>FOR /F "TOKENS=2" %G IN ('"C:\Program Files\WinZIP\wzunzip.exe" -vt ABC1NFD_20200127.zip ^|findstr /E /C:"_P.txt"') DO @echo %G
'C:\Program' is not recognized as an internal or external command,operable program or batch file.
If I take away the quotes from the search string then it works fine.

Code: Select all

W:\>FOR /F "TOKENS=2" %G IN ('"C:\Program Files\WinZIP\wzunzip.exe" -vt ABC1NFD_20200127.zip ^|findstr /E /C:_P.txt') DO @echo %G

ABC1NFD_SPS_A_20200127_P.txt
Even when I try to use the USEBACKQ option I still get the same results.

Code: Select all

W:\>FOR /F "USEBACKQ TOKENS=2" %G IN (`"C:\Program Files\WinZIP\wzunzip.exe" -vt ABC1NFD_20200127.zip ^|findstr /E /C:"_P.txt"`) DO @echo %G
'C:\Program' is not recognized as an internal or external command,operable program or batch file.

W:\>FOR /F "USEBACKQ TOKENS=2" %G IN (`"C:\Program Files\WinZIP\wzunzip.exe" -vt ABC1NFD_20200127.zip ^|findstr /E /C:_P.txt`) DO @echo %G

ABC1NFD_SPS_A_20200127_P.txt
Can someone refresh my memory.

jfl
Posts: 121
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: Quoting Issue

#2 Post by jfl » 01 Feb 2020 04:26

This is due to the way the child cmd.exe instance processes double quotes on its command line.
(And it does not matter if that child instance is invoked using single quotes ' or back ticks `.)
Extract from the cmd /? help screen :

Code: Select all

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          two quote characters
        - the string between the two quote characters is the name
          of an executable file.

    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.
So the solution for you is to add an extra pair of quotes around your first command:

Code: Select all

FOR /F "TOKENS=2" %G IN ('""C:\Program Files\WinZIP\wzunzip.exe" -vt ABC1NFD_20200127.zip ^|findstr /E /C:"_P.txt""') DO @echo %G
These extra quotes should be ^ escaped, to preserve the string/non string areas, in case your file name or search string contains tricky characters:

Code: Select all

FOR /F "TOKENS=2" %G IN ('^""C:\Program Files\WinZIP\wzunzip.exe" -vt ABC1NFD_20200127.zip ^|findstr /E /C:"<>&|.txt"^"') DO @echo %G

pieh-ejdsch
Posts: 217
Joined: 04 Mar 2014 11:14
Location: germany

Re: Quoting Issue

#3 Post by pieh-ejdsch » 01 Feb 2020 15:08


Post Reply