Renaming directory from mp3 metadata

I have a problem with directory renaming. I believe the solution must be something simple, just for the life of me I cannot figure it out.

There is a tagged mp3 file in a directory (somedir\somefile.mp3).
Goal: rename 'somedir' to 'artist - album', basing on somefile.mp3 metadata.
This tool may be used: Batch-edit ID3-Tags of MP3-Files
Clipboard can not be used.

Can you help?

Maybe this is helpful?

Is there something about extracting mp3 metadata there? If so, I cannot see it.

The main problem here comes really down to this: how to put output of external command into DO command (at least for a solution I have on mind).

I know I could probably change the directory name from outside of DO, but I think it would confuse subsequent commands that I would like to run on the same button.

I have an answer that is almost what you are looking for.
This code will create a Directory named 'mp3artist - mp3album' and move somefile.mp3 into it.
The resultant Directory structure is then somedir\mp3artist - mp3album\somefile.mp3 .

Rename REGEXP PATTERN "(.*)" TO "{mp3artist} - {mp3album}\\\0" FILEINFO autorename

Note that this can be used to sort tagged mp3 files.

Here is another solution.
This solution also does not succeed in renaming the Parent Folder, but it does move the mp3 into a folder named mp3artist - mp3album at the directory level Xyyyz wants.

@nofilenamequoting
Rename "{Filepath}" TO "{filepath|..\..|noterm}\{mp3artist} - {mp3album}\{file}" FILEINFO

Folder.jpg and playlists are still excluded from the answer.
Personally, don't have a problem with this at all.
In fact I prefer the first solution I posted here using RegExp.
It sorts mp3s into mp3artist - mp3album folders and gives one a chance to have a look see at any folder.jpg or playlists that remain.
The first solution is based on something Leo once posted long ago.

By the way the manual needs some corrections on the {filepath|..\..} control code stuff.
The manual calls it {file|..\..}, but it doesn't work. I changed it to filepath and it started working.
Either that or it's very late and I've gone mad on this.

@Xyzzy: This one is exactly doing what you want.

It uses a VBScript-based Button from leo, inspired by my request.

You should read more about it here: Sort music files and go to the target folder

I modified the Button using the second solution of Scred.
After the Folder is created it moves the whole content of the old Folder there, deletes the empty Sourcefolder and sets the Focus of the Source-Lister to the new Path.

Here' the Code:

Rename Parent Folder based on mp3 metadata.

@noexpandenv
@runmode hide
@set OriginalSource={sourcepath$|noterm}

Rename "{Filepath}" TO "{filepath|..\..|noterm}\{mp3artist} - {mp3album}\{file}" FILEINFO
Copy MOVE * TO "%RENAME_TARGET%"
Go PATH "%RENAME_TARGET%"
Delete {$OriginalSource}

@script vbscript
Option Explicit

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")

Dim Shell
Set Shell = CreateObject("WScript.Shell")

Dim EnvVars
Set EnvVars = Shell.Environment("PROCESS")
EnvVars("RENAME_TARGET") = "zzzzz:\"

Function Rename_GetNewName(strFileName, strFilePath, fIsFolder, strOldName, ByRef strNewName)

   ' Fix any double \ chars resulting from empty tags (e.g. no genre).
   ' If we don't do this, things can get confused about the destination path.

   strNewName = Replace(strNewName, "\\", "\")

   ' Set the %RENAME_TARGET% environment variable to the folder we're moving the file to.
   ' This can be used in the main command above to go to the folder.
   ' Note that we use "@noexpandenv" at the top of the command; if we didn't then %RENAME_TARGET%
   ' would be expanded *before* this script had run, which obviously would not work.

   EnvVars("RENAME_TARGET") = fs.GetParentFolderName(strNewName)

End Function

My second solution seemed to be working well last night,
but I'm having trouble getting it to work on more than one file at a time today.
I'm confused on this :unamused:

I have rewritten my second solution using regular expressions.
This time it really is working.

Rename REGEXP PATTERN "(.*)" TO "{filepath|..\..|noterm}\\{mp3artist} - {mp3album}\\\0" FILEINFO autorename

Use this instead in Kundals script code.

@Kundal
A very interesting script !
I will study it more.

@Scred:

Yes, you're right.
The Button will give an error if more than one file is selected.

For renaming the Parent Folder the metadata of only one file can be used.
So it's not useful to select more than one file.

To get around the error it's enough to add the modifier "@firstfileonly" to my Code to use the Tags of the first selected file.

I think am close to the solution with the following command (you execute it as standard command, in directory where somefile.mp3 resides):

/windows\system32\cmd.exe FOR /F "delims=}" %N IN ('c:\temp\id3 -q "%%a - %%l" "somefile.mp3"') DO e:\dopus\dopusrt /cmd Rename FROM={sourcepath} TO="%N"

(you need the id3 tool in c:\temp and correct dopusrt path)

BUT

When I execute it in "debug" mode:

/windows\system32\cmd.exe /K FOR /F "delims=}" %N IN ('c:\temp\id3 -q "%%a - %%l" "somefile.mp3"') DO @echo e:\dopus\dopusrt /cmd Rename FROM={sourcepath} TO="%N"

it shows correct command line to run for dopusrt. When I remove /K and @echo, it just creates 4 directories in the current directory, named: %N, FROM, Rename, TO :question: :exclamation: :question: Some problem with DOS shell here?

Try replacing /K with /C instead of removing /K completely.

That was it :exclamation:
Now, the complete, nice solution with comments & doubts (to execute as standard command in mp3 file's directory). I knew it could be done in one line :slight_smile:

cmd /C FOR /F "delims=}" %N IN ('c:\temp\id3 -q "%%a - %%l" "1 *.mp3"') DO e:\dopus\dopusrt /cmd Rename FROM={sourcepath} TO="%N"

/C - No idea why it, but you cannot omit it :question: :question: :question: Otherwise the command behaves strangely.
"delims=}" - '}' should be just any character that will never appear in output from 'c:\temp\id3 -q "%%a - %%l" "1 *.mp3"' (that means in artist or album name)
%N - you can use only ONE character here!
'c:\temp\id3 -q "%%a - %%l" "1 *.mp3"' - command that returns to standard output the string used as the new name of current directory (It is assured that there is always an mp3 file starting from "1 " in the current directory.)
e:\dopus\dopusrt - dopusrt alias doesn't work here
FROM={sourcepath} - here can be seen, that DO pre-parses commands

@Xyzzy:

Really nice Solution! Works fine and quick for me.

But I prefer to replace the "1 *.mp3" you're using by "{file$}" plus adding the modifier "@firstfileonly".
With this you'll have to select the file from which the information is extracted.
If you select more than one file the first selected file is used.

This is working even if the name of the first file begins with "01" or something else and I can choose from which file I want to get the information .