XP Bug with short names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

XP Bug with short names

#1 Post by jeb » 02 Dec 2011 14:32

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: XP Bug with short names

#2 Post by Ed Dyreen » 02 Dec 2011 16:31

: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 :?

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: XP Bug with short names

#3 Post by orange_batch » 02 Dec 2011 17:21

Solution: Don't use short names! :P

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: XP Bug with short names

#4 Post by foxidrive » 11 Apr 2012 14:58

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

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: XP Bug with short names

#5 Post by Liviu » 11 Apr 2012 16:36

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

Post Reply