title: How can I URL-encode spaces in an NT batch file?
body: <p>I have the misfortune of working with a program which requires all filenames passed into it to be valid URLs.  (No, I don't know why.)  Rather than having to drop to the command line and hand-craft <code>file:</code> URLs each time, I'm throwing together a batch file onto which I can simply drop files dragged from the Windows GUI.</p>
  
  <p>A full, proper URL encoder is beyond my needs or interest (most of the characters the app chokes on aren't valid in Windows filenames, anyway), but the two cases I do need to solve are backslashes and spaces.  Backslashes are easily handled by variable replacement syntax (<code>SET URL=%URL:\=/%</code>), but spaces are tricky — <code>%</code> is special for both URLs and batch files.</p>
  
  <p>Neither type of batch escaping I'm familiar with (<code>^%</code>, <code>%%</code>) allows the variable replacement to behave as desired and I haven't had any success Googling a solution.  Can any batch gurus help me out?</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>@ECHO OFF
  
  SETLOCAL
  
  SET URLPATH=file:/%~dp1
  SET URLPATH=%URLPATH:\=/%
  
  REM none of these work
  REM SET URLPATH=%URLPATH: =%20%
  REM SET URLPATH=%URLPATH: =%%20%
  REM SET URLPATH=%URLPATH: =^%20%
  
  REM works; I just need to figure out how to generate it
  SET URLPATH=file:/C:/Documents%%20and%%20Settings/bblank/example.dat
  
  stupidapp.exe %URLPATH%
  
  ENDLOCAL
  </code></pre>