title: Spurious spaces in output of for loop
body: <p>I'm trying to use a batch file to convert a file containing sql code into a single environment variable for use with the MSSQL utility bcp.
  For example, if InFile.sql contains</p>
  
  <pre><code>-- This is a simple statement
  SELECT *
      FROM table
  </code></pre>
  
  <p>The output of <code>ECHO %query%</code> should be
  <code>SELECT * FROM people</code></p>
  
  <p>The code below works for me most of the time</p>
  
  <pre><code>SETLOCAL=ENABLEDELAYEDEXPANSION 
  :: Replace VarOld with VarNew
  FOR /f "delims=" %%a IN ('TYPE InFile.sql') DO ( SET line=%%a &amp; ECHO !line:table=people! &gt;&gt; TmpFile1 )
  
  :: Remove comment lines starting with  '-'  and remove newline characters
  (FOR /f "eol=- delims=" %%a in (TmpFile1) DO SET/p=%%a ) &lt;nul &gt;TmpFile2
  
  :: Create variable 'Query'
  FOR /f "delims=" %%a IN ('TYPE TmpFile2') DO SET query=%%a
  </code></pre>
  
  <p>however, the first FOR loop adds 3 space characters at the end of each line and the second FOR loop adds another space character so the result is
  <code>SELECT *     FROM people</code></p>
  
  <p>I could cope with the additional spaces (although the purist in me wasn't happy!) until I had to use it with a long SQL query and multiple replacement steps - every line in the file was having 12 space characters added.  The additional spaces are enough to make the resulting query around 8300 characters long - too much for Windows' 8196 character limit for a batch file line.</p>
  
  <p>Can anybody see how I can remove these spurious spaces?</p>