Windows 如何确定/处理任何给定文件的 DOS 短名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/324041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:40:01  来源:igfitidea点击:

How does Windows determine/handle the DOS short name of any given file?

windowsfilefilesystemsshort-filenames

提问by Matt Refghi

I have a folder with these files:

我有一个包含这些文件的文件夹:

alongfilename1.txt <--- created first
alongfilename3.txt <--- created second

When I run DIR /xin command prompt, I see these short names assigned:

当我在命令提示符下运行DIR /x时,我看到分配了这些短名称:

ALONGF~1.TXT alongfilename1.txt
ALONGF~2.TXT alongfilename3.txt

Now, if I add another file:

现在,如果我添加另一个文件:

alongfilename1.txt 
alongfilename2.txt <--- created third
alongfilename3.txt

I see this:

我看到这个:

ALONGF~1.TXT alongfilename1.txt
ALONGF~3.TXT alongfilename2.txt
ALONGF~2.TXT alongfilename3.txt

Fine. It seems to be assigning the "~#" according to the date/time that I created the file. Is this correct?

美好的。它似乎是根据我创建文件的日期/时间分配“~#”。这样对吗?

Now, if I delete "alongfilename1.txt", the other two files keep their short names.

现在,如果我删除“alongfilename1.txt”,其他两个文件将保留它们的短名称

ALONGF~3.TXT alongfilename2.txt
ALONGF~2.TXT alongfilename3.txt

When will that ID (in this case, ~1) be released for use in another shortname. Will it ever?

该 ID(在本例中为 ~1)何时会被释放以用于另一个短名称。会永远吗?

Also, is it possible that a file on my machine has a short name of X, whereas the same file has a short name of Y on another machine? I'm particularly concerned for installations whose custom actions utilize DOS short names.

另外,是否有可能我机器上的文件的短名称为 X,而同一文件在另一台机器上的短名称为 Y?我特别关注自定义操作使用 DOS 短名称的安装。

Thanks, guys.

谢谢你们。

采纳答案by Piskvor left the building

The short filename is created with the file. The algorithm works like this (usually, but see moocha's reply):

短文件名是随文件一起创建的。该算法的工作原理如下(通常,但请参阅moocha 的回复):

counter = 1
stripped_filename = strip_dots(strip_non_ascii_characters(filename))
shortfn = first_6_characters(stripped_filename)
while (file_exists(shortfn + "~" + counter + "." + extension)) {
    increment counter by 1
    if more digits are added to counter, shorten shortfn by 1 
    /* e.g. if counter comes to 9 and shortf~9.txt is taken. try short~10.txt next */
}

This means that once the file is created, it will keep its short name until it's deleted.

这意味着一旦文件被创建,它将保留它的短名称,直到它被删除。

As soon as the file is deleted, the short name may be used again.

一旦文件被删除,短名称就可以再次使用。

If you move the file somewhere else, it may get a new short name (e.g. you're moving c:\somefilewithlongname.txt ("c:\somefi~1.txt") to d:\stuff\somefilewithlongname.txt, if there's d:\stuff\somefileelse.txt ("d:\stuff\somefi~1.txt"), the short name of the moved file will be somefi~2.txt). It seems that the short name is only persistent within a given directory on a given machine.

如果您将文件移动到其他地方,它可能会获得一个新的短名称(例如,您将 c:\somefilewithlongname.txt ("c:\somefi~1.txt") 移动到 d:\stuff\somefilewithlongname.txt,如果有 d:\stuff\somefileelse.txt ("d:\stuff\somefi~1.txt"),移动文件的短名称将是 somefi~2.txt)。似乎短名称仅在给定机器上的给定目录中持久存在。

So: the short filenames will be generated by the filesystem, usually by the method outlined above. It is better to assume that short filenames are not persistent, as c:\longfi~1.txt on one machine might be "c:\longfilename.txt", whereas on another it might be "c:\longfish_story.txt"; also, when a file is deleted, the short name is immediately available again.

所以:短文件名将由文件系统生成,通常是通过上面概述的方法。最好假设短文件名不是持久的,因为 c:\longfi~1.txt 在一台机器上可能是“c:\longfilename.txt”,而在另一台机器上它可能是“c:\longfish_story.txt”;此外,当文件被删除时,短名称会立即再次可用。

回答by Mihai Limb??an

If I were you, I would neverrely on any version of any file system driver (be it Microsoft's, be it another OS's) to be consistent about the algorithm it uses to generate short file names. The exact behavior of the Microsoft Fastfat and NTFS drivers is not "officially" documented (except as somewhat high level overviews) thus are not part of the API contract. What works today might not work tomorrow if you update the driver.

如果我是你,我永远不会依赖任何版本的任何文件系统驱动程序(无论是微软的,还是另一个操作系统的)来使其生成短文件名的算法保持一致。Microsoft Fastfat 和 NTFS 驱动程序的确切行为并未“正式”记录(除了一些高级概述),因此不是 API 合同的一部分。如果您更新驱动程序,今天有效的方法明天可能无效。

In addition, there is absolutely no requirement that short names contain tilde characters - see for example this post by Raymond Chen.

此外,绝对没有要求短名称包含波浪字符 - 例如参见Raymond Chen 的这篇文章

There's a treasure trove of info to be found about this topic in the MSDN blogs - for example:

在 MSDN 博客中可以找到有关此主题的大量信息 - 例如:

Also, do not rely on the sole presence of alphanumerical characters. Look at the Linux VFAT driverwhich says, for example, that any combination of uppercase letters, digits, and the following characters is valid: $ % ' ` - @ { } ~ ! # ( ) & _ ^. NTFS will operate in compatibility mode with that...

此外,不要仅依赖字母数字字符的存在。看看Linux VFAT 驱动程序,它说,例如,大写字母、数字和以下字符的任何组合都是有效的:$ % ' ` - @ { } ~ ! # ( ) & _ ^。NTFS 将在兼容模式下运行...

回答by VonC

I believe MSDOS stores the association between the long and the short name in a per directory file.

我相信 MSDOS 将长名称和短名称之间的关联存储在每个目录文件中。

It does not depends on the date/time.

它不取决于日期/时间。

If you move your files in a new directory... this will reset the algo mentionned by Piskvor applies itself again

如果您将文件移动到新目录中...这将重置 Piskvor 提到的算法再次适用

In the new directory (after a move), you will get:

在新目录中(移动后),您将获得:

ALONGF~1.TXT alongfilename1.txt
ALONGF~2.TXT alongfilename2.txt
ALONGF~3.TXT alongfilename3.txt

even though alongfilename2.txt has initially been created third.

尽管沿文件名2.txt 最初是第三个创建的。

回答by The Archetypal Paul

This linksays how NTFS does it. I would guess it's still the same idea on more recent version.

此链接说明 NTFS 如何做到这一点。我猜它在更新的版本上仍然是相同的想法。

In Windows 2000, both FAT and NTFS use the Unicode character set for their names, which contain several forbidden characters that MS-DOS cannot read. To generate a short MS-DOS-readable file name, Windows 2000 deletes all of these characters from the LFN and removes any spaces. Because an MS-DOS-readable file name can have only one period, Windows 2000 also removes all extra periods from the file name. Next, Windows 2000 truncates the file name, if necessary, to six characters and appends a tilde ( ~ ) and a number. For example, each non-duplicate file name is appended with ~1 . Duplicate file names end with ~2 , then ~3, and so on. After the file names are truncated, the file name extensions are truncated to three or fewer characters. Finally, when displaying file names at the command line, Windows 2000 translates all characters in the file name and extension to uppercase.

在 Windows 2000 中,FAT 和 NTFS 都使用 Unicode 字符集作为其名称,其中包含多个 MS-DOS 无法读取的禁用字符。为了生成一个简短的 MS-DOS 可读文件名,Windows 2000 从 LFN 中删除所有这些字符并删除所有空格。因为 MS-DOS 可读的文件名只能有一个句点,Windows 2000 还会从文件名中删除所有多余的句点。接下来,如有必要,Windows 2000 会将文件名截断为六个字符并附加一个波浪号 ( ~ ) 和一个数字。例如,每个非重复的文件名都附加有 ~1 。重复的文件名以 ~2 结尾,然后是 ~3 ,依此类推。文件名被截断后,文件扩展名将被截断为三个或更少的字符。最后,在命令行显示文件名时,

回答by Kevin Panko

When the files are provided by a network server which is running Samba, then the short names are generated by the server, and they do not follow a predictable pattern.

当文件由运行 Samba 的网络服务器提供时,短名称由服务器生成,它们不遵循可预测的模式。

So it is not safe to assume that you can predict the form of the short name.

因此,假设您可以预测短名称的形式是不安全的。

    G:\>dir /x *.txt

 Directory of G:\

08/25/2009  12:34 PM             1,848 S2XYYV~1.TXT strace_output.txt
03/01/2010  05:32 PM           325,428 TEY7IH~O.TXT tomcat-dump-march-1.txt
03/11/2010  12:01 AM             5,811 DI356A~S.TXT ddmget-output.txt
01/23/2009  01:03 PM           313,880 DLA94Q~K.TXT ddm-log-fn.txt
04/20/2010  07:42 PM             7,491 A50QZP~A.TXT april-20-2010.txt