Help with smart truncating of long filenames

I've noticed with my regular filename Action that occasionally the full Windows path + filename output will be too long and return an error within Mp3Tag.

For example here's one such path that would be too long for Windows, that I ended up truncating manually with an ellipsis:

M:\Music\CDs\Soundtracks\Studio Ghibli\Hayao Miyazaki & Joe Hisaishi Soundtrack Box Set\Ponyo on the Cliff by the Sea (Original Soundtrack)\Joe Hisaishi - Ponyo on the Cliff by the Sea (Original Soundtrack) - 36 - Ponyo on the Cliff by the Sea (Film Version).flac

The common method of truncating is $left(%_filename%,n) however this doesn't take into account the filepath, only the filename. Windows reduces the maximum allowed length of filenames based on the full path not just the filename, eg: moved to the Desktop the above file doesn't require truncation as the directory path is shorter.

Is there a way to truncate filenames to a certain number of characters taking into account the full file path + filename? ohrenkino also made the point that blindly truncating could lead to duplicates in some cases if the tracknumber gets cut, is there a way to include this prevention as well?

Any help and pointers would be appreciated.

That IS a long filename. And thanks for quoting me :wink:
If you want to have unique filename, then make sure that the number is the first bit of data in the filename generation mask.
If the path already exceeds the maximum length, then any truncation would reduce the filename to "nothing" which is just as illegal.
As brute force approach, you could use a $left() function for the fully qualified filename (which is drive, path and filename):
$left('M:\Music\CDs\Soundtracks\Studio Ghibli\Hayao Miyazaki & Joe Hisaishi Soundtrack Box Set\%album%$num(%track%,2) - %artist% - %album% - %title%',252)

Also: You could check if you could use a general abbreviation like "OST for "Original Soundtrack". And you could see, if you have to use "CDs" with an "s" at the end ... :wink: (even the little things count in this case).
Or if you have to use so many spaces in the filenames. The underscore as separator between data fields would be sufficient and even better than the hyphen as it hardly pops up in everyday writing.
"Joe Hisaishi - Ponyo on the Cliff by the Sea (Original Soundtrack) - 36 - Ponyo on the Cliff by the Sea (Film Version).flac" could become "Joe Hisaishi_Ponyo on the Cliff by the Sea (OST)_36_Ponyo on the Cliff by the Sea (Film Version).flac
In general: the tags should carry the information. Using the filenames as information backup is sometimes helpful but never the safe way as there are numerous characters that are not allowed in filenames.

There are threads in this forum that looked for a way to abbreviate artists and titles to just 3 letters as their car stereo had certain display deficiencies ... perhaps this is a way to cope with too long filenames.

In case of renaming or moving the media file by applying a new assembled filepathname to the underlying file, you should check the length of the newly created entire filepathname.

As the first step, store the new filepathname into a helper tag-field.
This helper tag-field can be displayed within a helper column in the Mp3tag listview.

A second helper column should display the current length of the new filepathname as a decimal number.

Doing so you can avoid the error message within Mp3Tag.

DD.20141231.1650.CET

It really is :smiley: Thanks to the pointer in the right direction, hadn't considered adding the full path to the $left() function for some reason. Made a working Action using the following code (which also includes the formatting of the initial filename structure):

$ifgreater($len(%_folderpath%\$if2(%albumartist%,%artist%) - $if(%album%,%album% - ,)$if(%track%,$num(%track%,2) - ,)$if2(%title%,'('Untitled')')),252,$regexp($left(%_folderpath%%_filename%,252),\s+$,)…,%_filename%)

The code looks at the entire length of the path including the filename (have to include the actual filename formatting in this section for it to calculate), then checks if it's greater than '252', if so it strips any trailing spaces and adds an ellipsis Unicode character (…). Otherwise it returns the regular filename.

Still doesn't break it up to consider truncation of tracknumber, but it's already improved my existing Action.

Reason for this is for making title-only searches in Windows, and also for being able to keep track of what a file is regardless of where it's located. As for illegal characters I solved this issue by character substitution using similar-looking Unicode:

$replace(<formatting here>,",'''',/,∕,:,:,?,?)

Substitutes double quotes for two single quotes, forward slashes with similar looking character, etc. Which reminds me to add the alternate question mark Unicode :slight_smile: Edit: added.

Hmm, not quite sure about how this would work but I did create an extra column in Mp3Tag which displays the current full path length in decimal, as per your tip in one of the other threads. The above code seems to work pretty well though.

That's the same ...

$if(%ALBUM%,%ALBUM%' - ',) [%ALBUM%' - ']

DD.20141231.1726.CET

Thanks for the reminder to update it. Noticed the redundancy when I was posting the code.

There is the $validate() function which could remove all (os-) invalid characters from a filename. So perhaps that is easier to maintain.

259 characters max. length for a filepathname in a subfolder of a disk.

For extension '.flac' ...
254 characters max. length of filepathname without extension.

For extension '.mp3' ...
255 characters max. length of filepathname without extension.

Action : Format value
Tag-Field : _FILENAME
Formatstring:

$ifgreater($len(%_folderpath%$if2(%ALBUMARTIST%,%ARTIST%)[' - '%ALBUM%][' - '$num(%TRACK%,2)]' - '$if2(%TITLE%,'(Untitled)')),$sub(259,1,$len(%_extension%)),$regexp($left(%_folderpath%$if2(%ALBUMARTIST%,%ARTIST%)[' - '%ALBUM%][' - '$num(%TRACK%,2)]' - '$if2(%TITLE%,'(Untitled)'),$sub(259,2,$len(%_extension%))),'\s+$',)'…',%_folderpath%$if2(%ALBUMARTIST%,%ARTIST%)[' - '%ALBUM%][' - '$num(%TRACK%,2)]' - '$if2(%TITLE%,'(Untitled)'))

... or written into two steps ...

Action : Format value
Tag-Field : TMP_FILENAME
Formatstring:

%_folderpath%$if2(%ALBUMARTIST%,%ARTIST%)[' - '%ALBUM%][' - '$num(%TRACK%,2)]' - '$if2(%TITLE%,'(Untitled)')

Action : Format value
Tag-Field : _FILENAME
Formatstring:

$ifgreater($len(%TMP_FILENAME%),$sub(259,1,$len(%_extension%)),$regexp($left(%TMP_FILENAME%,$sub(259,2,$len(%_extension%))),'\s+$',)'…',%TMP_FILENAME%)

DD.20141231.1828.CET, DD.20150311.1024.CET, DD.20150320.1049.CET