title: Getting Short-Filename of Command-Line Argument from Batch-File
body: <p>The following command in a batch file does not work as expected/hoped:</p>
  
  <p>    <strong><code>echo %~nxs1</code></strong></p>
  
  <p><br />
  Here is a sample display of what I’m trying to get:</p>
  
  <pre><code>C:\&gt;type test.bat
  @dir /b %1
  @echo %~nxs1
  
  C:\&gt;test "C:\Documents and Settings\All Users\ntuser.dat"
  ntuser.dat
  NTUSER.DAT
  
  C:\&gt;test "C:\Documents and Settings\All Users\ntuser.data"
  ntuser.data
  NTUSER~1.DA~
  
  C:\&gt;test "C:\Documents and Settings\All Users\ntuser.dat.baz"
  ntuser.dat.baz
  NTUSER~1.BAZ
  
  C:\&gt;test "C:\Documents and Settings\All Users\foo.bar.baz"
  File Not Found
  foo.bar.baz (or FOO~1.BAZ or whatever, doesn’t really matter since
               file does not exist, though latter would be nice)
  </code></pre>
  
  <p>Instead what I get is something like the following (depending on assigned short-filenames):</p>
  
  <pre><code>C:\&gt;type test.bat
  @dir /b %1
  @echo %~nxs1
  
  C:\&gt;test "C:\Documents and Settings\All Users\ntuser.dat"
  ntuser.dat
  s\ntuser.dat
  
  C:\&gt;test "C:\Documents and Settings\All Users\ntuser.data"
  ntuser.data
  s\ntuser.data
  
  C:\&gt;test "C:\Documents and Settings\All Users\ntuser.dat.baz"
  ntuser.dat.baz
  z
  
  C:\&gt;test "C:\Documents and Settings\All Users\foo.bar.baz"
  File Not Found
  s\foo.bar.baz
  </code></pre>
  
  <p><br />
  <br />
  Basically, I need to pass a filename to a <strong>BAT</strong> file and have the script get (eg display) it as a short-filename, but only the filename and extension, no drive or path.</p>
  
  <p>The help info for <em>FOR</em> gives <strong>%~fsI</strong> as an example, but that has the whole path as a short-filename, not just the file. Does anyone know how to combine the S parameter from <strong>%~</strong> without getting the whole path?</p>
  
  <p><br /></p>
  
  <p>Thanks a lot.</p>
  
  <p><br />
  <br /></p>
  
  <p><strong><em>Updates</em></strong></p>
  
  <ol>
  <li><p>I am not looking for a solution in a different language, I need the BAT command to work.</p></li>
  <li><p>It seems that it works for others, so I am checking to see if it is some kind of alternate configuration issue. I’m currently testing to see if Command Processor Extensions could be the cause.</p></li>
  <li><p>It won’t work at all if Extensions are disabled (obviously), so I’m going on the hypothesis that it is a bug that was fixed in a subsequent service pack (the system I tested on is XP SP1). I’m testing SP2 and SP3 today…</p></li>
  </ol>