Batch parameter %~s1 gives incorrect 8.3 short name

前端 未结 1 1800
情话喂你
情话喂你 2021-01-05 07:10

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\"
<         


        
相关标签:
1条回答
  • 2021-01-05 07:39

    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
    
    0 讨论(0)
提交回复
热议问题