Here is a batch file solution. The batch file should be stored in directory containing all the *.mp3 files in this directory or in its subdirectories and executed with a double click.
@echo off
for /F "eol=| delims=" %%I in ('dir "* *.mp3" /A-D /B /S 2^>nul') do call :RenameFile "%%I"
set "NewName="
pause
goto :EOF
:RenameFile
set "NewName=%~nx1"
set "NewName=%NewName: = - %"
ren "%~1" "%NewName%"
goto :EOF
The command DIR searches
- just for files because of
/A-D
(not attribute directory)
- in current directory and all subdirectories because of
/S
- for *.mp3 files containing 3 spaces in series and
- outputs in bare format because of
/B
the names of the found files with file extension and full path.
The DIR command line is executed by FOR in a separate command process in background which captures the output of this command process (= command DIR) written to handle STDOUT.
A possible error output by DIR on not finding in entire directory tree any file matching the wildcard pattern to handle STDERR is suppressed by redirecting it with 2>nul
to device NUL. Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul
. The redirection operator >
must be escaped with caret character ^
on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir
command line with using a separate command process started in background.
The command FOR processes each line captured from output of DIR. The default behavior of FOR to ignore lines starting with ;
is disabled by option eol=|
. The character |
can't be used in a file/folder name and so can't be at begin of a file name or file path. The default behavior of FOR to split up each line on horizontal tabs and spaces into substrings is disabled by option delims=
specifying an empty list of delimiters. So FOR really assigns each file name with full path and file extension to loop variable I
.
Note: eol=|
would not be necessary here as DIR with option /S
outputs line by line the file names with full path which makes it impossible that a line processed by FOR starts with a semicolon. But a file name could start with a semicolon although this is very unlikely for *.mp3 files with artist and title as name on not using DIR option /S
to get just a list of *.mp3 files in current directory without path.
It is important here to run FOR on a captured list of file names instead of letting FOR itself search recursively for the MP3 files as otherwise it could happen during the file renames that some MP3 files are skipped because of the directory entry changes during the loop iterations.
For each file name the subroutine RenameFile
is called with passing the full file name as first argument enclosed in double quotes to the subroutine. The double quotes are necessary to process correct also the file names on containing a space or one of these characters &()[]{}^=;!'+,`~
in file name or file path.
A subroutine is used instead of doing the remaining code directly in the FOR loop as this would require usage of delayed environment variable expansion which would cause problems if file/folder names of the processed files contain by chance one or more exclamation marks.
In the subroutine RenameFile
the name of the current file without path is assigned to environment variable NewName
.
Next a string substitution is applied on file name to replace all occurrences of three spaces in series by space, hyphen, space. So it is not 100% guaranteed by this code that the hyphen is really inserted between artist part and title part instead of a space as no script code without access to a large database with artists and titles is able to determine what is the artist and what is the title part in each file name.
Then the file rename is executed with specifying the file to rename with full path and the new name with file extension without path as required by command REN.
The command ECHO could be inserted left to ren
to run the batch file first for just checking if the file renames would be done as expected.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
dir /?
echo /?
for /?
goto /?
pause /?
ren /?
set /?
See also Where does GOTO :EOF return to?