title: Handling trailing backslash & directory names with spaces in batch files
body: <p>This is a variation of this question:
  <a href="https://stackoverflow.com/questions/2952401/remove-trailing-slash-from-batch-file-input">Remove Trailing Slash From Batch File Input</a></p>
  
  <p>but it's subtly different, so I don't think it's a dupe.</p>
  
  <p>I'm having trouble making this work with directories that have spaces in them (running WinXP).</p>
  
  <pre><code>:START
  @echo What folder do you want to process? (Provide a path without a closing backslash)
  set /p datapath=
  
  ::Is string empty?
  IF X%datapath% == X GOTO:START
  
  ::Does string have a trailing slash? if so remove it 
  IF %datapath:~-1%==\ SET datapath=%datapath:~0,-1%
  
  echo %datapath%
  </code></pre>
  
  <p>It handles:</p>
  
  <pre><code>c:\
  </code></pre>
  
  <p>properly (stripping it to c:)</p>
  
  <p>But if you enter:</p>
  
  <pre><code>c:\test space
  </code></pre>
  
  <p>the error is "space was unexpected at this time."</p>
  
  <p>If you try enter:</p>
  
  <pre><code>"c:\test space"
  </code></pre>
  
  <p>You get the same error.</p>
  
  <p>I thought it would involve a strategically placed " or two in this line:</p>
  
  <pre><code>IF %datapath:~-1%==\ SET datapath=%datapath:~0,-1%
  </code></pre>
  
  <p>But I haven't had any luck.</p>
  
  <p>Any ideas?</p>