title: Inconsistent "The syntax of the command is incorrect" error
body: <p>I guess I'm mad enough to use a batch file, in the first place, but this problem has me tearing my hair out! The below batch script <em>usually</em> works just fine. However, on first run it will give a "The syntax of the command is incorrect" error; and it will do this until I <code>REM</code> out everything, run it through, and then un<code>REM</code> it. Then it magically starts working again!</p>
  
  <pre><code>@echo off
  
  REM Create processed data directory
  set "PROCDIR=processed"
  mkdir %PROCDIR% 2&gt;nul
  
  REM Loop through CSV files and process them
  for %%f in (*.csv) do (
    set "in=%%~nf"
    set "input=%%~nxf"
    set "out_lang=%PROCDIR%\%in%_lang.csv"
    set "out_spr=%PROCDIR%\%in%_spr.csv"
    set "out_ajt=%PROCDIR%\%in%_ajt.csv"
  
    echo Processing %input%
  
    REM Split input file appropriately
    REM FIXME Hardcoded line ranges are brittle
    sed -n "1,2p"  %input% &gt; %out_lang%
    sed -n "3,55p" %input% &gt; %out_spr%.tmp
    sed -n "56,$p" %input% &gt; %out_ajt%.tmp
  
    REM Unpack SPR data
    REM n.b., Needs gawk 4, or later
    gawk -f spr.awk %out_spr%.tmp &gt; %out_spr%
    del %out_spr%.tmp
  
    REM Substitute AJT data values
    REM n.b., -i seems to be buggy in Windows
    sed -f ajt.sed %out_ajt%.tmp &gt; %out_ajt%
    del %out_ajt%.tmp
  )
  </code></pre>
  
  <p>If I <code>REM</code> out the <code>@echo off</code>, it appears it's failing because the variable substitution sometimes -- for unknown reasons -- doesn't work. Then, when it gets to <code>sed</code>, it just gives up.</p>
  
  <p>I assume I'm missing something... Any ideas what it might be!? (Besides my marbles!)</p>