0

I've used a media conversion tool to convert some videos to MP3s. Unfortunately, the tool does not follow the naming convention, and I can't find an option for filenames.

In particular, it has stripped the hyphen between artist and title. So

Smash Mouth - All Star.mp4

becomes

Smash Mouth   All Star.mp3

There are something like 2600 files to convert, so I'd rather get all the conversions done into a single folder that I can then run a batch file or command on. I tried something like

ren "*   *.mp3" "*. - *.mp3"

but it had no effect (without quotes, it generated an error, and using question marks instead of *, as I understand it, requires a fixed pattern, in terms of word length, for the filenames).

Any ideas? I have PowerShell installed, but have never used it, so if that's the way to go, please point me in the direction of some documentation (a PowerShell source that I was directed to earlier is now a broken link).

[edit]

I'm going to start with the suggestion here and see if I can modify it for my purposes.

https://superuser.com/questions/1230586/renaming-files-with-a-standard-pattern-as-and-when-they-are-added-to-a-folder

Byff70
  • 31
  • 5

3 Answers3

0

If you want to go the Powershell route, you could use Get-ChildItem and Rename-Item commands together with a regular expression.

Ex.

Get-ChildItem -Path C:\path\to\your\folder | foreach{ Rename-Item -NewName { $_.Name -replace '\[.*\]' }

There's another post on here about using PS w/regexs as well:

Use Regex / Powershell to rename files

Hope that helps!

trebleCode
  • 2,134
  • 19
  • 34
0

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?

Mofi
  • 46,139
  • 17
  • 80
  • 143
-1

EDIT: Since you mentioned using Batch/CMD, you can use the following.

@echo off

for /f "tokens=*" %%i in ('dir \path\to\files /B /A-D') DO (
        set var=%%i
        set var=%var:   = - %
        ren \path\to\"%%~i" "%var%"
)

Now, this is because you showed that the filename had three spaces with the omitted dash. Just change your path, ensure you run the script with the appropriate permissions to change the file names.

The PowerShell way would be as stated above:

gci \path\to\files|%{rni {$_.Name -replace '\[.*\]'}}
BenK
  • 11
  • 4
  • Thank you. I didn't test it with any filenames that contained `!, so that makes sense. I edited my solution to reflect your changes. – BenK Jan 05 '18 at 21:14
  • But without delayed expansion the batch file solution as posted now does not work at all. `%var%` in the command lines `set var=%var: = - %` and `ren \path\to\"%%~i" "%var%"` are processed/expanded already by Windows command interpreter before __FOR__ is executed at all. Doing this file rename task is not possible in this way. It would be possible to do `setlocal EnableDelayedExpansion` after `set var=%%i` and insert `endlocal` after the __REN__ command and reference between `var` using delayed expansion, i.e. with exclamation marks. – Mofi Jan 05 '18 at 21:23
  • This one will evidently require some tweaking. Once I corrected the path and saved this to .bat and ran it, I got one rename and three "duplicate file name exists" (on four files in the destination folder). The one renamed file ended up as `- = -` or something similar, without the extension. – Byff70 Jan 05 '18 at 23:04
  • The PowerShell method above is missing a closing bracket, but it gave me a start on using PowerShell, so that's something. The edited batch file posted by Mofi now works, so I'll be using that for the time being. While looking up PowerShell documentation, I came across a script that watches the output folder for changes, so I'll be incorporating that into my eventual PS script. – Byff70 Jan 06 '18 at 02:14