Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

powershell - 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. I want to play audiobooks that are divided into multiple files, but Windows sometimes generates f***ed up 8.3 file names. Here's an example of the output of dir /x of a folder that contains Internet1.mp3 to Internet7.mp3:

                                  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. 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, I.e. INT1.MP3 to INT7.MP3 There is no problem regarding which folder to play. The long file names contain an ascending numer (here 1 to 7) that gives away the correct order of the files.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use this to change all files matching the Internet* pattern to 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

$i = 0; foreach ($f in ls *.mp3) { fsutil file setShortName $f ($i.ToString("D8") + ".MP3"); $i++ }

Update:

Unfortunately you can set short names for files on NTFS partitions, as it's the restriction right from the SetFileShortName() Win32 API

Sets the short name for the specified file. The file must be on an NTFS file system volume.

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

$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

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. 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

  1. 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 ., followed by the first 3 characters of the extension.

    • Example: TextFile.Mine.txt becomes TE021F~1.TXT.

https://en.wikipedia.org/wiki/8.3_filename#VFAT_and_computer-generated_8.3_filenames


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...