Page 1 of 1

XP Bug with short names

Posted: 02 Dec 2011 14:32
by jeb
Hi,

this question Batch parameter %~s1 gives incorrect 8.3 short name shows another XP-Bug.

As a result: XP copies too much characters while building the short name,
so you get for a path like
C:\temp\123456789ABCDEFGHIJKLMNOPQR\A[1].BAT

The short name
C:\temp\123456~1\A_1_~1.BATKLMNOPQR\a[1].bat

Instead of
C:\temp\123456~1\A_1_~1.BAT

And it seems to be only occurs, if the filename contains []+;,=<space> :!:

It can be tested with this

Code: Select all

@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!
)


jeb

Re: XP Bug with short names

Posted: 02 Dec 2011 16:31
by Ed Dyreen
:shock:
Serious and dangerous bug, thanks jeb. Always nice to see others scooping Stackoverflow,
to me Stackoverflow is one big mess to find anything in.

I can imagine this problem to occur in realtime, I have many of those weird named mp3's in my database.
Wonder if there's a secure fix :?

Re: XP Bug with short names

Posted: 02 Dec 2011 17:21
by orange_batch
Solution: Don't use short names! :P

Re: XP Bug with short names

Posted: 11 Apr 2012 14:58
by foxidrive
Ed Dyreen wrote:Wonder if there's a secure fix :?


Maybe some code like this would make it more robust:

convert using var=%%~sA
:loop
if not exist "%var%\" remove last character and goto :loop
hopefully %var% here will be the short filename

Re: XP Bug with short names

Posted: 11 Apr 2012 16:36
by Liviu
jeb wrote:XP copies too much characters while building the short name,
[...]
And it seems to be only occurs, if the filename contains []+;,=<space> :!:

Guess this bug is twins with the one in 'for' loop variables http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/afb023e1792881b5?hl=en#. In that case, it's not limited to certain characters, the test case in the previous link was using just plain alphanumerics.

Also, as noted later in that thread, the original test case works as expected in Win7, but a different test case failed there, too (that one, however, involved unicode filenames).

Liviu