Help writing action group to rename file

I have a collection of podcast files with filenames similar to the following:


The numbers after ADV are random but the filename length is fixed, although I don't think this actually matters for what I'm trying to do.

Each file is a podcast episode whose %title% tag is formatted as: "#: Episodename", e.g. one file's %title% tag might be: "12: Foo".

I would like to write an action group that will rename these files using the following format for the above example:

12: Foo (ADV1104544195).mp3

The modified filename needs to include the %title% tag plus the original filename.

The problem I am having is that I cannot figure out a way to store or reference the original filename when specifying the new filename. I thought about storing the original filename in a temporary tag and then reading it back when using the "Format value" command, but mp3tag doesn't seem to allow you to just create and reference new tags, e.g %temp%.

Is there any way to accomplish this?

This is an invalid filename as it features the colon.

To add something to an exsiting string, use an action of the type "Format value" in this case for _FILENAME
Format string: %title% %_filename%
You would have to take care that the action is only run once as otherwise you would add the title part each time.

It would be interesting to see how you tried to create the new field. Of course, you can add new fields.

Thanks, I missed that %_filename% was one of the fields that could be read by the "Format value" command.

Is there any way to have the : be substituted with a - in the new filename, while leaving the : in the %title% field?

To create a new tag called %TEMP% and give it the value "foo", I tried this action:

Replace "%TEMP%": "" -> "foo"

Basically I'm trying to say "replace the empty text in %TEMP% (because the tag doesn't exist yet) with the text "foo"". The action runs, but when I add the "TEMP" tag to the list of visible columns in mp3tag, there is no text listed after running the action. Should I use a regex here instead?

I didn't think you could because of the text on this page:

which says:

It seems to suggest only the fields on that page can be manipulated.

yes. try
Format string $validate(%title%, -) %_filename%

The problem is: there is no reserved space for non-existent fields. So as TEMP did not exist, there were no hits.
To create a new tag field, use an action of the type "Format value" and enter the new field name without %.

In respect to the page with field names:
That page tells you which standard fields are supported by MP3tag - and hopefully a lot of players.
Any field that you create that is not included in that list becomes a user-defined field, a standard-obeying feature which hardly any player supports.

Thanks, that worked, but what is the purpose of the backtick? I omitted it and the command seems to work fine. Or did you mean to include a closing backtick to format that text as code?

Thanks, that worked.

Yeah, sorry, forgot the closing one - I editted my original post.

Thanks, I think I'm starting to get the hang of this now. I have a follow up question:

Suppose I again start with the following file:


with title tag: 12: Foo.

Now suppose I run this file through an mp3 file splitter (mp3, ogg vorbis and FLAC splitter - mp3splt-project) and end up with the following files:


Now suppose I would like the final files to look like this:

01_03_12 - Foo (ADV1104544195).mp3
02_03_12 - Foo (ADV1104544195).mp3
03_03_12 - Foo (ADV1104544195).mp3

Where the first number in the filename is the track number, the second number is the total number of files, and the third number is the number from the episode number of the %title%.

The easiest way I have found to accomplish this involves using a temporary tag (as explained above) to store the original filename. Here is the sequence of commands I came up with:

# create a temp tag
Format value "temp_filename": ""
# get the original filename and store it in the temp tag created above; also capture the track number and store it in the track tag
Guess values "%_FILENAME%": %track%_%temp_filename%
# format each split file to include number of tracks, track title, and original filename (stored in the temp tag)
Format value "_FILENAME": $num(%track%,2)_$num(%_total_files%,2)_$validate(%title%, -) (%temp_filename%)

Is that the easiest way to accomplish what I'm trying to do, or is there a more direct way to do it without using temporary tags as temporary variables?

This does not work - you cannot create a field with nothing in it.
You could use Converter>Tag-Filename with
Format string: $num(%track%,2)_$num($mid(%track%,$add($strstr(%track%,/),1)),2)_%title% (%_filename%)

I don't know what

should do in this context esp. as the target string or source string is missing.

It does seem to work - it creates the tag "temp_filename" and populates it with "". It turns out this step is unnecessary though, as the "Guess values" command will automatically create the tag "temp_filename" and populate it with whatever comes after the first _ in the filename.

It parses the filename to obtain values for %track% and %temp_filename%; everything before the _ in the filename is copied to %track% and everything after it is copied to %temp_filename%. I'm not sure if that is the intended behavior but it does work.

By creating my own tags I now have a way of using variables in this scripting language, which makes many tasks a lot easier.

Can you explain how this works? The first part is:


Which based on my understanding says "find the first occurrence of / in %track%, but / doesn't appear in track. Does $strstr default to the beginning or end of the string if the character isn't found? The documentation page (Scripting Functions – Mp3tag Documentation) doesn't specify.

If you have MP3 files like

suggests, then the total number of tracks is stored behind the backslash.
$num() cuts everything off that is not numeric so you have to move the pointer in that string to a character 1 further than the occurrance of the slash.

If you have the field TOTALTRACKS (which would be a user-defined field in MP3 files) then you can use that.

Is this just the way mp3tag works, or is this some quirk of how Windows works? Why does searching %track% for the first occurrence of / give the total number of tracks?

This is how the standard works for mp3. Both disc and track info has the total shown after a slash.

If that is not the case for your data - how do you know the total number of files?

Here is the definition from the ID3 standard:

The 'Track number/Position in set' frame is a numeric string containing the order number of the audio-file on its original recording. This may be extended with a "/" character and a numeric string containing the total numer of tracks/elements on the original recording. E.g. "4/9".

But I see that with

you overwrite any existing track number with the total files in it so there would probably not be a total.
In that case the %_total_files% variable may be accurate enough, yet, in this case it only represents the number of selected files.

Ah, OK, I see what you mean. $strstr(%track%,/) assumes %track% already includes a / and the total number of tracks, e.g. 2/4. Then $strstr(%track%,/) simply returns the position of the / and $mid(%track%,$add($strstr(%track%,/),1)) returns the character that is one after the '/' in %track%.

Question: would that line fail if %track% contained two-digit numbers both before and after the slash, e.g. if %track% was 02/04, it seems $mid(%track%,$add($strstr(%track%,/),1)) would return 0 instead of the intended 4.

Another question: if strstr(x,y) does not find string y in string x, what does it return? As a test, I have the following files (%track% for each is a single-digit number, 1, 2, or 3):


I ran the following one-liner from above on them:

Format string: $num(%track%,2)_$num($mid(%track%,$add($strstr(%track%,/),1)),2)_%title (%_filename%)

And it returned:

01_01_012 Foo (01_ADV1104544195).mp3
02_02_013 Foo (02_ADV1104544195).mp3
03_03_013 Foo (03_ADV1104544195).mp3

It looks like because $strstr(%track%,/) fails to match / in %track%, it returns position 0, and $num($mid(%track%,$add($strstr(%track%,/),1)),2) just returns the track number expressed as a 2-digit number. Is that correct?

What that is actually doing is reading the track number (not the total number of tracks) and the base filename from %_FILENAME% and storing them in %track% and %temp_filename%, respectively. Each filename is formatted as #_base_filename, where # is the track number. I use %_total_files% to get the total number of files, as you noted.

I suppose this task might be made easier if mp3splt-gtk could just format the %track% field to include the total number of tracks to start with. EDIT: apparently mp3splt-gtk does not support "total number of tracks" as one of the fields it can include in the output format, which must explain why I chose this method in the first place.

the $strstr stays at position 0 if no match is found.

probably the "1" is a little short. Try a 3 or $len(%track%)

Adding the total number of files for a folder is something you can do with the track numbering wizard. Might be a better idea to have the data in the tags and not exclusively in the filename.