批处理参数%〜S1提供了不正确8.3短名 [英] Batch parameter %~s1 gives incorrect 8.3 short name

查看:162
本文介绍了批处理参数%〜S1提供了不正确8.3短名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写在Windows XP中的批处理文件,它接受一个完全合格的路径名和输出8.3短名称版本...

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"

请注意,上面的输出(C:\\ DOCUME〜1 \\安格斯\\ LOCALS〜1 \\ TEMPOR〜1 \\ Content.IE5 \\ 2JSTM34V \\ 62_1_〜1.JA2M34V \\ 62 [1] .ja2)不存在。如果我从输出删除.JA2M34V \\ 62 [1]部分,然而,然后将得到的字符串将是原始输入文件的有效路径。

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.

这似乎是在文件名中使用括号([])中的问题。如果我创建在同一个目录中的文件62.ja2,输出将是正确的...

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"

这是Windows中的一个错误吗?有谁知道,如果有一个变通方法,让批处理文件来妥善处理这个文件名?

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

推荐答案

这是不是在你的code的一个错误,这是XP的一个bug!结果
与Vista相同的code工作。

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>

一个短的测试案例

@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

这篇关于批处理参数%〜S1提供了不正确8.3短名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆