Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use the double quotation mark as a delimiter with syntax like:</p> <pre><code>FOR /F delims^=^"^ tokens^=2 %G IN ('echo I "want" a "pony"') DO @ECHO %G </code></pre> <p>When run on the command line, using <code>tokens^=2</code> should give you <code>want</code>, and 4 tokens gets you a pony.</p> <p>Applying the technique to your original question, this should work in your batch file:</p> <pre><code>FOR /F delims^=^"^ tokens^=2 %%G IN ('FINDSTR /L "ProductShortName" "data.txt"') </code></pre> <p><strong>Details</strong></p> <p>I'm no expert on <a href="http://www.daviddeley.com/autohotkey/parameters/parameters.htm#WINCMDRULES" rel="noreferrer">quirks of the command line parser</a>, but it may help to think of the usual <code>"delims=blah tokens=blah"</code> as a single, combined argument passed to FOR. The caret escaping trick in <code>delims^=blah^&nbsp;tokens^=blah</code> bypasses the need for enclosing quotes while still treating the sequence as a single argument. I've used a bit of creative analogy here, and the effect isn't universal across the shell. E.g. you can't do <code>dir C:^\Program^ Files</code> (which makes sense since <code>^</code> is a valid filename character).</p> <p><strong>Test Cases</strong></p> <p>With sufficient escaping, you can quickly check your original sample on the command line:</p> <pre><code>FOR /F delims^=^"^ tokens^=2 %G IN ('echo ^^^&lt;?define ProductShortName="Foo" ?^^^&gt;') DO @ECHO %G </code></pre> <p>Others playing with this may want to create a file <strong>testcases.txt</strong>:</p> <pre><code>blah blah "red" blah "green" blah How about a "white" "unicorn"? </code></pre> <p>and run something like:</p> <pre><code>FOR /F delims^=^"^ tokens^=2 %G IN (testcases.txt) DO @ECHO %G </code></pre> <p>to check results for a variety of inputs. In this case it should yield:</p> <pre><code>red green white </code></pre> <p>One last example:</p> <pre><code>FOR /F delims^=^"^ tokens^=2 %G IN ('FINDSTR /L "unicorn" "testcases.txt"') ^ DO @ECHO The unicorn is %G. </code></pre> <p>Finally, note my testing for this was done on Windows Server 2003.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload