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

发布于 2024-07-09 08:41:57 字数 902 浏览 5 评论 0原文

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

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

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

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

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

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

我会看到:

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

很好。 它似乎是根据我创建文件的日期/时间分配“~#”。 它是否正确?

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

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

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

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

多谢你们。

I have a folder with these files:

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

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

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.

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?

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.

Thanks, guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾城花音 2024-07-16 08:41:57

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

此外,绝对不要求短名称包含波浪号字符 - 例如参见 雷蒙德·陈的这篇文章

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

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

If I were you, I would never rely 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.

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

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

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

极致的悲 2024-07-16 08:41:57

短文件名是随文件一起创建的。 该算法的工作原理如下(通常,但请参阅 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 */
}

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

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

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

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

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

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.

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.

猫腻 2024-07-16 08:41:57

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

它不取决于日期/时间。

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

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

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

即使最初已创建第三个ongfilename2.txt。

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

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

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.

陌路终见情 2024-07-16 08:41:57

此链接说明了 NTFS 的作用它。 我猜想在更新的版本中仍然是相同的想法。

在 Windows 2000 中,FAT 和 NTFS 都使用
他们的 Unicode 字符集
名称中包含一些禁止的内容
MS-DOS 无法读取的字符。 到
生成一个 MS-DOS 可读的短文件
名称,Windows 2000 删除所有
这些来自 LFN 的字符和
删除任何空格。 因为一个
MS-DOS 可读的文件名可以有
只有一期,Windows 2000也
删除所有额外的句点
文件名。 接下来,Windows 2000
如有必要,截断文件名,
最多六个字符并附加一个波形符
(~) 和一个数字。 例如,每个
附加不重复的文件名
与〜1。 重复文件名结束
先是 ~2 ,然后是 ~3 ,依此类推。 后
文件名被截断,文件
扩展名被截断为三个
或更少的字符。 最后,当
在命令中显示文件名
行,Windows 2000 翻译所有
文件名中的字符和
扩展为大写。

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

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.

我偏爱纯白色 2024-07-16 08:41:57

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

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

    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

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.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文