Windows 短文件名中 ~ 之后的数字如何分配?

FGr*_*reg 2 windows command-line

Windows 仍然~用于缩短具有长名称的文件和目录。您可以通过dir /X在命令提示符中键入来查看“短”名称。

我的问题是如何分配给短文件名的数字?它似乎不是按字母顺序排列的,也不是按文件创建时间工作的。

例如:

我创建了一个新的文本文件: C:\reallylongfilenameB.txt

然后我运行dir /X并看到fileB的短名称是REALLY~1.TXT

我创建了第二个文本文件: C:\reallylongfilenameA.txt

然后我运行dir /X并看到 fileA 的短名称是REALLY~2.TXT

然后我将时钟调回一天,删除C:\reallylongfilenameA.txt然后重新创建它,基本上将创建日期设为C:\reallylongfilenameA.txt1 天之前C:\reallylongfilenameB.txt

我跑了dir /XC:\reallylongfilenameA.txt简称仍然是REALLY~2.TXT.

鉴于这个例子,有没有办法让短名称C:\reallylongfilenameA.txtREALLY~1.TXT

dbe*_*ham 7

正如杰克在他的回答中指出的那样,短名称是在创建(或重命名或移动)文件时分配的。该值取决于长文件名以及已经存在的其他短名称。

确切的短名称分配算法没有记录,并且不能保证该算法在 Windows 版本之间保持不变,甚至在不同的存储系统格式之间。您不应编写依赖于任何特定短名称分配算法的代码。

短文件名存在设计限制和错误。设计限制的一个例子是del *.txt可以删除一个名为的文件,something.txt2因为短名称的扩展名可能是.txt,并且源掩码查看长名称和短名称。请参见标题为“BUG -短文件名8.3能够打破/ d和/ S选项”,在什么是Windows FINDSTR命令的无证功能和限制?例如由短名称引起的错误。

NTFS 卷可以禁用短文件名(在禁用它们之前存在的短名称将保留)。许多人(组织)在他们的系统上禁用短名称,因为它们可能导致问题。

更新

这是一个明确的论点,说明为什么尝试建立生成短文件名的算法是浪费时间 - FSUTIL 允许您为文件指定所需的任何短名称,只要它不存在于该文件中文件夹。

这是相关的 FSUTIL 帮助文档

C:\test>fsutil file
---- FILE Commands Supported ----

findbysid               Find a file by security identifier
queryallocranges        Query the allocated ranges for a file
setshortname            Set the short name for a file
setvaliddata            Set the valid data length for a file
setzerodata             Set the zero data for a file
createnew               Creates a new file of a specified size
queryfileid             Queries the file ID of the specified file
queryfilenamebyid       Displays a random link name for the file ID

C:\test>fsutil file setshortname /?
Usage : fsutil file setshortname <filename> <shortname>
   Eg : fsutil file setshortname C:\testfile.txt testfile
Run Code Online (Sandbox Code Playgroud)