My filename truncation script


#1

I wrote a pretty long Tag > Filename script that truncates the Title so that it fits within the 256 path and filename character limit of Windows.

Here it is for anybody that is interested:

$num(%track%,3) - $validate($replace(%album%,.,-),-) - $iflonger(%_folderpath%$num(%track%,3) - $validate($replace(%album%,.,-),-) - $validate($replace(%title%,.,-),-).%_extension%,256,$left($validate($replace(%title%,.,-),-),$sub(230,$len(%_folderpath%$num(%track%,3) - $validate($replace(%album%,.,-),-) - ))),$validate($replace(%title%,.,-),-))

Basically it'll look like this:

001 - ValidAlbumName - ValidAndTruncatedTitle.flac

This script also cleanses album names and titles of periods and replaces them with dashes. I've had failures on some classical albums where the title is more than one sentence and has periods separated by spaces.

I arbitrarily chose 230 as my high-end character limit, which gives me some wiggle room if I have to rename or add a directory or two above the files.

I think this would fail if the path and album name already exceeded 256 characters, but in my case that would be an insanely long album name.

Hope that helps someone.

Adam


#2

Yes, you use Mp3tag scripting functionalities in the right way to fit your needs.
Let me say a few remarks ...
Because your replace work does not change the length of tag field content, and all of the involved tag fields need the same replacement, you may do $replace() only once for the complete string.
Same for $validate().
If you want to determine the length of $num(%track%,3), which is obvious, you may use alternatively just a string of three spaces or some other character.
Because the usage of hyphen as the replacement character for invalid characters the new filename may not support easy Convert Filename to Tag.

Formatstring can be shortened to:

$num(%track%,3) - $validate($replace(%album% - $iflonger(%_folderpath%nnn - %album% - %title%.%_extension%,256,$left(%title%,$sub(230,$len(%_folderpath%nnn - %album% - ))),%title%),.,-),-)

The set of involved Mp3tag scripting functions has changed from

$num
$validate
$replace
$iflonger
$num
$validate
$replace
$validate
$replace
$left
$validate
$replace
$sub
$len
$num
$validate
$replace
$validate
$replace

to

$num
$validate
$replace
$iflonger
$left
$sub
$len

which may save computing time in some cases.

DD.20080319.1008.CET


#3

Perfect, much better. Thanks a lot!


#4

A possible problem you may run into with that script is the case where the resulting pathname differs only after the 230th character. In that case you could have a filename collision and may overwrite a file (I haven't tested that scenario to see what would happen). It's not a likely scenario. But it's the unlikely things that can bite you at times.

I'm going to be doing a big rename project sometime soon. But first I need to finish ripping my classical CDs (about 70 more to go).

My plan for the renaming project is to limit my filenames to 60ish characters or so. My requirements for the filename are that it give me a hint as to what the artist and title is, no filename collisions, sort in track order for each album, and be short enough that I can burn them to a CD or DVD without worrying about having a filename that is too long.

I'm thinking about doing something like:
[artist max 20 chars] - [album max 20 chars] - [disc #] - [track #] - [title max 20 chars]

The actual script would be more complex to deal with some situations.
If the field I have for album artist is equal to "Various" then I want "Various" as the artist
If there is no disc # then skip that part
Other tidying up

So the resulting script will be more complex. That's my general idea. I'm pretty sure mp3tag will be able to handle the task.

After that is done I'll have a project to figure out how to rebuild M3U files for each album.


#5

Yes, I am going so for years.

Filename including extension is limited to 64 chars.
I've set a column value of
'$ifgreater($len(%_filename_ext%),64,!$len(%_filename_ext%),)'
to signal when a filename (maybe by manually adjustment) runs over the limit of 64 chars.

I use some special algorithm to condense the content of the envolved tag fields to have all important information at a glance giving me a hint as to what the track, title, artist, album, year is.
Different track numbers will avoid duplicate filename.
If more information is needed (for report or so), then the long contents is read directly from the tag fields.

Size Meaning
02 track
01 delimiter (underline)
26 title
01 delimiter (underline)
16 artist
01 delimiter (underline)
08 album
01 delimiter (underline)
04 year
01 delimiter (dot)
03 extension
giving a filename of maximal 64 chars including extension.

DD.20080321.1035.CET


Filter by field size (filename)