title: How to safely append a file name to a Windows folder path argument?
body: <p>Suppose I have a batch script that expects a folder path in argument <code>%1</code>. I want to append a file name to the path and use that in a command. Is there a simple way to do this in a way that is reliable in all situations? </p>
  
  <p>I do not want to <code>PUSHD %1</code> and simply ignore the path afterward. Assume my logic requires the current directory to remain unchanged.</p>
  
  <p>The issue is the <code>%1</code> argument may or may not have a terminating backslash - i.e. <code>c:\path</code> or <code>c:\path\</code>.</p>
  
  <p>Typically I can simply use <code>"%~1\file.ext"</code> because Windows treats <code>"c:\path\file.ext"</code> and <code>"c:\path\\file.ext"</code> identically.</p>
  
  <p>But there is one nasty case - <code>c:\</code></p>
  
  <p>The following commands produce this error: <code>The filename, directory name, or volume label syntax is incorrect.</code></p>
  
  <pre><code>dir "c:\\file.ext"
  del "c:\\file.ext"
  </code></pre>
  
  <p>The odd thing is, these commands work just fine:</p>
  
  <pre><code>if exist "c:\\file.ext" echo OK
  echo text&gt;"c:\\file.ext"
  copy "c:\\file.ext" "somePath"
  xcopy "c:\\file.ext" "somePath"
  </code></pre>
  
  <p>There is another problematic case - <code>c:</code>, meaning the current directory on drive C:, will give the wrong result if I append a backslash. <code>"c:file.ext"</code> is not the same as <code>"c:\file.ext"</code>.</p>
  
  <p><strong>Is there a simple way to safely append a file name to any given folder path argument?</strong></p>
  
  <p>I am not concerned with UNC paths, as I do not use them very often, and I believe there are some commands that do not play nice with UNC paths.</p>
  
  <p><em>Note: This is a case where I am posting a paired question and answer. This issue has been a thorn in my side for a number of years, and I just recently discovered a simple solution that I thought others may be interested in.</em></p>