title: Sending sqlcmd output file to directory that is a variable
body: <p>Currently, the following code in a batch file works: it runs all SQL scripts in <code>%SCRIPTFOLDER%</code> and sends each script's output to a CSV within the same folder as the scripts.</p>
  
  <pre><code>FOR /F "tokens=*" %%S IN (
      'DIR /B "%SCRIPTFOLDER%\*.sql" '
  ) DO (
      sqlcmd -b -S %INSTANCE% -d %DATABASE% -i "%%~fS" -s "|" -o "%%~dpnS.csv" -W
      IF ERRORLEVEL 1 GOTO errorhandling
      ECHO %%~nS.csv successfully created
  )
  </code></pre>
  
  <p>What I'd like to do is allow the user to specify where the generated CSVs get sent to using a variable %OUTPUTFOLDER%.</p>
  
  <p>I tried placing <code>%OUTPUTFOLDER%</code>, which is a full path, drive, and folders (e.g. D:\some folder\output) in various positions within <code>%%~dpnS.csv</code>. Specifically,</p>
  
  <pre><code>%%~dp%OUTPUTFOLDER%nS.csv
  </code></pre>
  
  <p>and</p>
  
  <pre><code>%%~dpn%OUTPUTFOLDER%S.csv
  </code></pre>
  
  <p>but they didn't work and I'm (probably obviously to you) woefully inept at batch file syntax!</p>
  
  <p>I understand that <code>dp</code> is the drive and path and that <code>S</code> is the file name, but I'm not sure how to integrate that with a the variable that is the path.</p>