title: CMD: Test if for loop variable contains another variable
body: <p>I have read several posts regarding my issue (e.g. <a href="https://stackoverflow.com/questions/7005951/batch-file-find-if-substring-is-in-string-not-in-a-file">this one</a>) but still cannot get the following running:</p>
  <pre><code>for /f &quot;tokens=*&quot; %%U in ('query user * /server:MYSERVER') do (
      if %%U contains %USERNAME% then do something.
  )
  </code></pre>
  <p>So the issue is that the string to be searched for is not hard coded but a variable. I have tried all variants of %% and !! but always failed. Is there the one variant I missed to try?</p>
  <p><strong>Edit:</strong></p>
  <p>This does not answer the actual question (so it remains open) but it solves the specific task in a different way. In case it´s useful for someone I post it here (rather than in a comment because there you cannot add line breaks).</p>
  <pre><code>for /F &quot;tokens=1 delims=&quot; %%U in ('query user * /server:MYSERVER') do (
      for /F &quot;tokens=1 delims= &quot; %%V in (&quot;%%U&quot;) do (
          if /I &quot;%%V&quot; == &quot;%USERNAME%&quot; (
              echo You: %%U
          ) ELSE (
              echo      %%U
          )
      )
  )
  </code></pre>
  <p>It is based on Mofi´s suggestion below. However, I don´t have any &gt; characters in my output of the query command. For that reason I have used a simple space as delimiter. I only wonder why it works with token 1 because the output seems to start with a space, thus a delimiter (if it´s not #160 in reality).</p>
  <p>The only bad thing on that is that it does not seem to be possible to add the 2&gt;NUL to the command:</p>
  <pre><code>for /F &quot;tokens=1 delims=&quot; %%U in ('query user * /server:MYSERVER 2&gt;NUL') do (
  ...
  </code></pre>
  <p>Then the batch file drops an error. That is a pity because this way you could prevent the (stderr) message if no user is logged on at all.</p>