Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch parameter %~s1 gives incorrect 8.3 short name

I'm trying to write a batch file in Windows XP that takes in a fully-qualified path name and outputs the 8.3 short name version...

@echo off
echo "%~s1"

I have come across one particular case where this outputs an incorrect path and file...

C:\>test.bat "C:\Documents and Settings\angus\Local Settings\Temporary Internet Files\Content.IE5\2JSTM34V\62[1].ja2"
"C:\DOCUME~1\angus\LOCALS~1\TEMPOR~1\Content.IE5\2JSTM34V\62_1_~1.JA2M34V\62[1].ja2"

Note that the above output ("C:\DOCUME~1\angus\LOCALS~1\TEMPOR~1\Content.IE5\2JSTM34V\62_1_~1.JA2M34V\62[1].ja2") does not exist. If I remove the ".JA2M34V\62[1]" section from that output, however, then the resulting string would be a valid path to the original input file.

This seems to be a problem with the use of brackets ([]) in the filename. If I create a file 62.ja2 in the same directory, the output will be correct...

C:\>test.bat "C:\Documents and Settings\angus\Local Settings\Temporary Internet Files\Content.IE5\2JSTM34V\62.ja2"
"C:\DOCUME~1\angus\LOCALS~1\TEMPOR~1\Content.IE5\2JSTM34V\62.ja2"

Is this a bug in Windows? Does anybody know if there's a workaround to allow the batch file to properly handle this filename?

like image 245
Gus Avatar asked Dec 02 '11 09:12

Gus


1 Answers

It's not a bug in your code, it's a bug of XP!
With Vista the same code works.

It looks like a sort of a buffer corruption.
It depends of the length of the last directory name, the last characters are copied to the "short name".

And it only occurs if in the filename is one or more characters out of [];,+=<space>

A short test case

@echo off
setlocal EnableDelayedExpansion
set myDir=
set myFile=a[1].bat
set map=123456789ABCDEFGHIJKLMNOPQRSTUVW

for /L %%n in (0 1 26) do (
  set "myDir=!myDir!!map:~%%n,1!"
  md !myDir!
  echo dummy > "!myDir!\!myFile!"
  echo Dir=!myDir!
  for %%X in ("!myDir!\!myFile!") do echo   %%~sX
  echo(
  del "!myDir!\!myFile!" > nul
  rd !myDir!
)

The results of the last lines

Dir=123456789A
  C:\Projekte\batch\123456~1\A_1_~1.BAT

Dir=123456789AB
  C:\Projekte\batch\123456~1\A_1_~1.BATt

Dir=123456789ABC
  C:\Projekte\batch\123456~1\A_1_~1.BATat

Dir=123456789ABCD
  C:\Projekte\batch\123456~1\A_1_~1.BATbat

Dir=123456789ABCDE
  C:\Projekte\batch\123456~1\A_1_~1.BAT.bat

Dir=123456789ABCDEF
  C:\Projekte\batch\123456~1\A_1_~1.BAT].bat

Dir=123456789ABCDEFG
  C:\Projekte\batch\123456~1\A_1_~1.BAT1].bat

Dir=123456789ABCDEFGH
  C:\Projekte\batch\123456~1\A_1_~1.BAT[1].bat

Dir=123456789ABCDEFGHI
  C:\Projekte\batch\123456~1\A_1_~1.BATa[1].bat

Dir=123456789ABCDEFGHIJ
  C:\Projekte\batch\123456~1\A_1_~1.BAT\a[1].bat

Dir=123456789ABCDEFGHIJK
  C:\Projekte\batch\123456~1\A_1_~1.BATK\a[1].bat

Dir=123456789ABCDEFGHIJKL
  C:\Projekte\batch\123456~1\A_1_~1.BATKL\a[1].bat

Dir=123456789ABCDEFGHIJKLM
  C:\Projekte\batch\123456~1\A_1_~1.BATKLM\a[1].bat

Dir=123456789ABCDEFGHIJKLMN
  C:\Projekte\batch\123456~1\A_1_~1.BATKLMN\a[1].bat

Dir=123456789ABCDEFGHIJKLMNO
  C:\Projekte\batch\123456~1\A_1_~1.BATKLMNO\a[1].bat

Dir=123456789ABCDEFGHIJKLMNOP
  C:\Projekte\batch\123456~1\A_1_~1.BATKLMNOP\a[1].bat

Dir=123456789ABCDEFGHIJKLMNOPQ
  C:\Projekte\batch\123456~1\A_1_~1.BATKLMNOPQ\a[1].bat

Dir=123456789ABCDEFGHIJKLMNOPQR
  C:\Projekte\batch\123456~1\A_1_~1.BATKLMNOPQR\a[1].bat
like image 73
jeb Avatar answered Sep 23 '22 18:09

jeb