[英]Easy way to automatically generate short file name
I have a very old car radio that can play music from USB fash drives when they are formatted in FAT16 or FAT32, but it sorts files by the short 8.3 file name, not by the long file name.我有一个非常旧的汽车收音机,可以播放 USB 闪存驱动器中格式化为 FAT16 或 FAT32 的音乐,但它按短 8.3 文件名而不是长文件名对文件进行排序。 I want to play audiobooks that are divided into multiple files, but Windows sometimes generates f***ed up 8.3 file names.我想播放分成多个文件的有声读物,但 Windows 有时会生成 f***ed up 8.3 文件名。 Here's an example of the output of dir /x
of a folder that contains Internet1.mp3 to Internet7.mp3:这是包含 Internet1.mp3 到 Internet7.mp3 的文件夹的dir /x
的 output 的示例:
Short Long
30.12.2020 15:59 2.186.859 INTERN~1.MP3 Internet1.mp3
30.12.2020 15:59 2.507.643 INTERN~2.MP3 Internet2.mp3
30.12.2020 15:59 2.423.319 INTERN~3.MP3 Internet3.mp3
30.12.2020 15:59 2.110.163 INTERN~4.MP3 Internet4.mp3
30.12.2020 15:59 2.007.345 IN1FAB~1.MP3 Internet5.mp3
30.12.2020 15:59 2.921.422 IN64EF~1.MP3 Internet6.mp3
30.12.2020 15:59 3.290.689 INB914~1.MP3 Internet7.mp3
As you can see, the files Internet5.mp3 to Internet7.mp3 will be played before Internet1.mp3 to Internet4.mp3 since they have the random short file names.如您所见,文件 Internet5.mp3 到 Internet7.mp3 将在 Internet1.mp3 到 Internet4.mp3 之前播放,因为它们具有随机的短文件名。 Some of my audiobooks are divided into more than 100 parts, so i'd like to have a script (Batch, Powershell, Python, whatever) that automatically sets the short file name to something usable, Ie INT1.MP3 to INT7.MP3 There is no problem regarding which folder to play.我的一些有声读物分为 100 多个部分,所以我想要一个脚本(Batch,Powershell,Python,无论如何)自动将短文件名设置为可用的东西,即 INT1.MP3 到 INT7.MP3播放哪个文件夹没问题。 The long file names contain an ascending numer (here 1 to 7) that gives away the correct order of the files.长文件名包含一个升序数字(此处为 1 到 7),它给出了文件的正确顺序。
You can use this to change all files matching the Internet*
pattern to INTxxxxx
您可以使用它来将所有匹配Internet*
模式的文件更改为INTxxxxx
$i = 0
foreach ($f in ls Internet*) {
fsutil file setShortName $f ("INT" + $i.ToString("D5") + ".MP3")
$i++
}
You can change ls Internet*
to just ls
to ignore the prefix and rename all files in the folder您可以将ls Internet*
更改为ls
以忽略前缀并重命名文件夹中的所有文件
$i = 0; foreach ($f in ls *.mp3) { fsutil file setShortName $f ($i.ToString("D8") + ".MP3"); $i++ }
Unfortunately you can set short names for files on NTFS partitions, as it's the restriction right from the SetFileShortName()
Win32 API不幸的是,您可以为 NTFS 分区上的文件设置短名称,因为这是SetFileShortName()
Win32 API 的限制
Sets the short name for the specified file.设置指定文件的短名称。 The file must be on an NTFS file system volume.该文件必须位于 NTFS 文件系统卷上。
Therefore the only way you can do for a FAT16/32 partition is rename all your files to a short 8.3 name like this因此,您可以为 FAT16/32 分区做的唯一方法是将所有文件重命名为像这样的短 8.3 名称
$i = 0; foreach ($f in ls *.mp3) { mv $f ($i.ToString("D8") + ".MP3"); $i++ }
Of course you can also use the INTxxxxx.MP3
format like above当然你也可以使用上面的INTxxxxx.MP3
格式
You can manually hex edit the partition to set the short names and recalculate the checksums but it'll be fragile unless someone writes a tool to automate all those things您可以手动十六进制编辑分区以设置短名称并重新计算校验和,但它会很脆弱,除非有人编写一个工具来自动化所有这些事情
Note that names like IN1FAB~1.MP3
or IN64EF~1.MP3
are not random.请注意,像IN1FAB~1.MP3
或IN64EF~1.MP3
这样的名称不是随机的。 They're the hash of the file names because it's obvious that the File~NUMBER
pattern doesn't work if there are more than 9 files with that prefix in the folder so something more robust must be used它们是文件名的 hash,因为很明显,如果文件夹中有超过 9 个具有该前缀的文件,则File~NUMBER
模式不起作用,因此必须使用更强大的东西
On all NT versions including Windows 2000 and later, if at least 4 files or folders already exist with the same extension and first 6 characters in their short names, the stripped LFN is instead truncated to the first 2 letters of the basename (or 1 if the basename has only 1 letter), followed by 4 hexadecimal digits derived from an undocumented hash of the filename, followed by a tilde, followed by a single digit, followed by a period
.
在包括 Windows 2000 及更高版本在内的所有 NT 版本上,如果至少有 4 个文件或文件夹具有相同的扩展名和短名称中的前 6 个字符,则剥离的 LFN 会被截断为基本名称的前 2 个字母(或 1,如果基本名称只有 1 个字母),后跟 4 个十六进制数字,这些数字源自文件名的无证 hash,后跟波浪号,后跟一个数字,然后是句点.
, followed by the first 3 characters of the extension. ,后跟扩展名的前 3 个字符。
- Example:
TextFile.Mine.txt
becomesTE021F~1.TXT
.示例:TextFile.Mine.txt
变为TE021F~1.TXT
。https://en.wikipedia.org/wiki/8.3_filename#VFAT_and_computer-generated_8.3_filenames https://en.wikipedia.org/wiki/8.3_filename#VFAT_and_computer-generated_8.3_filenames
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.