1

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.

  • How do you know what order to put the files in? This is also a 'write a script for me that does x ' type of question, and is likely to be closed. Please revise to ask for specific help with a change to preexisting code. – FoxDeploy Dec 30 '20 at 17:14
  • 1
    Simply rename the files to any name 8 characters or shorter :) – Mathias R. Jessen Dec 30 '20 at 17:17
  • 1
    you cannot generate your own short names. that is done by the OS. what you CAN do is rename your files with shortened standard names. – Lee_Dailey Dec 30 '20 at 17:17
  • are you sure that it sorted by 8.3 name? Most (old) MP3 players use the order in the FAT because they don't have enough resources for sorting. See [How to reorder the files of a FAT32 file system?](https://superuser.com/q/376577/241386) – phuclv Dec 30 '20 at 17:27
  • @Lee_Dailey that's not true. You can set your own 8.3 name by running [`fsutil file setShortName`](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/fsutil-file) – phuclv Dec 30 '20 at 17:31
  • @phuclv Yes I did, since I already tried changing the order in the FAT. Needles to say that it didn't work. – Adamantium23 Dec 30 '20 at 17:31
  • @phuclv - ooo! i learned something ... thank you! [*grin*] – Lee_Dailey Dec 30 '20 at 18:19

1 Answers1

1

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

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Thanks a lot! That Script worked on my NTFS-hard drive, but not on the FAT32-Flash drive. When I copy the renamed files from my hard drive to the flash drive, they get the same 8.3 Names as before (i.e. IN1FAB~1.MP3). Trying to run the script you posted on the flash drive only gives the error that fsutil only works on NTFS volumes. – Adamantium23 Dec 30 '20 at 19:27
  • Yes I knew that some fsutil commands don't work well for FAT32 but I didn't think `file setshortname` falls to this category. I'm trying to see how to do it in FAT32. Anyway in this case it'll be a duplicate of [How to set FAT32 short names in Powershell or Windows Command Shell](https://stackoverflow.com/q/4115519/995714) – phuclv Dec 31 '20 at 01:19
  • @Adamantium23 see my edit. `SetFileShortName` only works on NTFS so you must rename the file to a short name – phuclv Dec 31 '20 at 03:16
  • I did and so Iset all file names to the 8.3 pattern. It works now, and since its only for audiobooks in a car stereo, I'll live with the solution! Thank you for your help! I already saw the tread you linked - Its still unanswered... – Adamantium23 Dec 31 '20 at 10:11