title: Batch File: Parsing a Registry Key whose name contains the % sign through the FOR command
body: <p>I'm having a problem using the reg query command to get a registry key value <strong>through the for command</strong> and saving the output to a variable.</p>
  
  <p>For instance, the following works fine both outside and within the for command:</p>
  
  <pre><code>reg query "HKEY_CURRENT_USER\Console" /v "ScreenBufferSize"
  </code></pre>
  
  <p>(Which displays the key to the current console window.)</p>
  
  <pre><code>for /f "usebackq tokens=3 delims=: " %%V in (`reg query "HKEY_CURRENT_USER\Console" /v "ScreenBufferSize"`) do set MyVar=%%V
  </code></pre>
  
  <p>(Which saves the value of the key to MyVar, which is what I want.)</p>
  
  <p>But the problem is that the key whose value I want to obtain has the % sign on its name, and the <strong>for</strong> command seems to be unable to parse it, any each way I've tried. Escaping the % sign works for the first instance:</p>
  
  <pre><code>reg query "HKEY_CURRENT_USER\Console\%%SystemRoot%%_system32_cmd.exe" /v "ScreenBufferSize"
  </code></pre>
  
  <p>(Also displays the key to the current console window.)</p>
  
  <p>But escaping it the same way in the for command fails: </p>
  
  <pre><code> for /f "usebackq tokens=3 delims=: " %%V in (`reg query "HKEY_CURRENT_USER\Console\%%SystemRoot%%_system32_cmd.exe" /v "ScreenBufferSize"`) do set MyVar=%%V
  </code></pre>
  
  <p>producing the following output:</p>
  
  <pre><code>ERROR: The system was unable to find the specified registry key or value.
  </code></pre>
  
  <p>Please note the following:</p>
  
  <ul>
  <li><p>I'm using Windows7.</p></li>
  <li><p>I'm using the code in a batch file.</p></li>
  <li><p>I'm using both Delayed Expansion and Command Extensions.</p></li>
  <li><p>I don't want to create a temporary file to save the value to a variable.</p></li>
  <li><p>A key with the same name exists in both <strong>HKCU\Console</strong> and <strong>HKCU\Console\%SystemRoot%_system32_cmd.exe</strong>, but it's the latter the one I need.</p></li>
  </ul>
  
  <p>I've tried escaping the % sign in any way I could think of, and using pipe redirection to no avail. The for command is the best I could come up with, but it's not a must, all I want is that value saved on a variable!</p>
  
  <p>Any help is much appreciated. Thank you!</p>
  
  <p>V</p>