This command does the following (inspired by this question):
-
Moves all .mp3 files from the current directory into your My Music folder, using the music tags to find/create the correct directory under Albums\Genre\Artist\Title.
-
Makes the current file display go up one level to the parent.
-
Makes the window's other file display open (if not already) and go to the place that the music files were moved to. (If they ended up in multiple places, e.g. because they were not all part of the same album, then it'll pick just one of them.)
-
Deletes the original starting directory, if it is now empty.
You probably won't want to do exactly that, but the command may help you do similar things by example.
Here is the command in XML format. See How to add buttons from this forum to your toolbars for instructions on pasting it as a button on your toolbar.
<?xml version="1.0"?>
<button backcol="none" display="both" textcol="none">
<label>Sort Music</label>
<icon1>#script</icon1>
<function type="batch">
<instruction>@noexpandenv</instruction>
<instruction>@runmode hide</instruction>
<instruction />
<instruction>@set OriginalSource={sourcepath$|noterm}</instruction>
<instruction />
<instruction>Rename FILEINFO FROM "*.mp3" TO "{alias|mymusic}\Albums\{mp3genre}\{mp3artist}\{mp3album}\{mp3track|#2} {mp3title}.{ext}"</instruction>
<instruction />
<instruction>Go PATH ".." DUALPATH "%RENAME_TARGET%"</instruction>
<instruction />
<instruction>rmdir {$OriginalSource}</instruction>
<instruction />
<instruction />
<instruction>@script vbscript</instruction>
<instruction>Option Explicit</instruction>
<instruction />
<instruction>Dim fs</instruction>
<instruction>Set fs = CreateObject("Scripting.FileSystemObject")</instruction>
<instruction />
<instruction>Dim Shell</instruction>
<instruction>Set Shell = CreateObject("WScript.Shell")</instruction>
<instruction />
<instruction>Dim EnvVars</instruction>
<instruction>Set EnvVars = Shell.Environment("PROCESS")</instruction>
<instruction />
<instruction>Function Rename_GetNewName(strFileName, strFilePath, fIsFolder, strOldName, ByRef strNewName)</instruction>
<instruction />
<instruction> ' Fix any double \ chars resulting from empty tags (e.g. no genre).</instruction>
<instruction> ' If we don't do this, things can get confused about the destination path.</instruction>
<instruction />
<instruction> strNewName = Replace(strNewName, "\\", "\")</instruction>
<instruction />
<instruction> ' Set the %RENAME_TARGET% environment variable to the folder we're moving the file to.</instruction>
<instruction> ' This can be used in the main command above to go to the folder.</instruction>
<instruction> ' Note that we use "@noexpandenv" at the top of the command; if we didn't then %RENAME_TARGET%</instruction>
<instruction> ' would be expanded *before* this script had run, which obviously would not work.</instruction>
<instruction />
<instruction> EnvVars("RENAME_TARGET") = fs.GetParentFolderName(strNewName)</instruction>
<instruction />
<instruction>End Function</instruction>
</function>
</button>
Here is the main body of the command, so you can more easily look at it without leaving this post:
@noexpandenv
@runmode hide
@set OriginalSource={sourcepath$|noterm}
Rename FILEINFO FROM "*.mp3" TO "{alias|mymusic}\Albums\{mp3genre}\{mp3artist}\{mp3album}\{mp3track|#2} {mp3title}.{ext}"
Go PATH ".." DUALPATH "%RENAME_TARGET%"
rmdir {$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")
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
Points of interest
-
Sorting the MP3 files by tags is all done on the Rename line and doesn't involve the VBScript part at all (except that the VBScript part cleans things up a bit in case any tags are missing). Using things like {mp3title} is covered in several more simple rename examples so I won't go into that here. Just note that, for each file, the result of the Rename command is fed into the VBScript as the strNewName argument, and the script can then (if it wants to) modify the new name further and/or perform other actions.
-
It's also worth noting that this command ignores the file selection. It will work on *.mp3 within the current folder, whether or not those files are selected first. To make it work only on the selected files, use PATTERN instead of FROM on the Rename line.
-
The VBScript part will create an environment variable called RENAME_TARGET and set it to the path that each file is moved to. (Since the script is run for each file, the environment variable will be overwritten for each file, leaving just the last file's path at the end. The assumption is that all the files will be part of the same album and all end up in the same place. You could make a different script which opened each target folder in a new tab instead.)
-
The RENAME_TARGET environment variable is used outside of the VBScript to tell the Go command where to send the other file display.
-
@noexpandenv appears at the top of the button to ensure that %RENAME_TARGET% is expanded when the Go command runs instead of when the button first starts. If @noexpandenv was not used then %RENAME_TARGET% would be expanded before the VBScript part had run and set it up.
-
When creating the EnvVars object, the PROCESS type is specified. That means that the environment variable we set will be visible to Opus but will not be visible to any other programs and will not be saved across reboots.
-
Since the RENAME_TARGET environment variable is global to the Opus process, you should never run this script at the same time as itself (or any other which uses the same variable name). If you did then the two scripts would see and/or change each other's RENAME_TARGET variable with unpredictable results.
-
The MS-DOS rmdir command is used to delete the original directory instead of Opus's own Delete command. This is so that the directory is only deleted if it is empty. (The directory might not be empty if it had some non-MP3 files within it and blindly deleting it would mean those files were lost at the click of a button.) This use of the rmdir command is also why the function type is set to MS-DOS Batch Function and the @runmode hide line near the top ensures that a Command Prompt window does not flash on the screen when the command runs.