# My filename truncation script

**URL:** https://community.mp3tag.de/t/my-filename-truncation-script/6479
**Category:** General Discussion
**Created:** [March 19, 2008, 6:15am UTC](https://community.mp3tag.de/t/my-filename-truncation-script/6479 "2008-03-19T06:15:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![adamtoth](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@adamtoth](https://community.mp3tag.de/u/adamtoth)
#### Post date: [March 19, 2008, 6:15am UTC](https://community.mp3tag.de/t/my-filename-truncation-script/6479/1 "2008-03-19T06:15:23Z")

</div>

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

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [March 19, 2008, 8:08am UTC](https://community.mp3tag.de/t/my-filename-truncation-script/6479/2 "2008-03-19T08:08:46Z")

</div>

> [@AdamToth](#):
>
> 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. ...  
> $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%,.,-),-))  
> ...

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

---

<div class="post-metadata">

### Author: ![adamtoth](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@adamtoth](https://community.mp3tag.de/u/adamtoth)
#### Post date: [March 19, 2008, 6:04pm UTC](https://community.mp3tag.de/t/my-filename-truncation-script/6479/3 "2008-03-19T18:04:05Z")

</div>

Perfect, much better. Thanks a lot!

---

<div class="post-metadata">

### Author: ![Frobozz](https://community.mp3tag.de/user_avatar/community.mp3tag.de/frobozz/32/232_2.png) [@Frobozz](https://community.mp3tag.de/u/Frobozz)
#### Post date: [March 20, 2008, 7:57pm UTC](https://community.mp3tag.de/t/my-filename-truncation-script/6479/4 "2008-03-20T19:57:04Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [March 21, 2008, 8:34am UTC](https://community.mp3tag.de/t/my-filename-truncation-script/6479/5 "2008-03-21T08:34:46Z")

</div>

> [@Frobozz](#):
>
> 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]

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
