0

I'm trying to remove a certain part of a mp3's name. So if mp3 is "X (2015)" I want it to recognize the "(2" and delete the rest of the name so I can delete it for every year.

George R.
  • 17
  • 7

2 Answers2

0
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
FOR /f "delims=" %%a IN (
  'dir /b /a-d "%sourcedir%\*(*.mp3" '
  ) DO (FOR /f "tokens=1delims=(" %%g IN ("%%a") DO ECHO(REN "%sourcedir%\%%a" "%%~g%%~xa"
)

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.

The directory list in /b basic form is processed and the filenames assigned to %%a. The filemask selects only those filenames that contain (

%%a is then partitioned into the part-before and the part after the first ( and the part before assigned to %%g

It's then a matter of stringing the command together.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

Curiously enough, the most difficult part of this problem is to eliminate the space before the left parenthesis. The Batch file below just eliminate the last character of the new name; if the number of spaces before the left paren may be variable, then a different approach must be used.

@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in ('dir /B *(2*.mp3') do (
   for /F "delims=(" %%b in ("%%a") do set "name=%%b"
   ECHO ren "%%a" "!name:~0,-1!.mp3"
)

Output example:

C:\> dir /B *.mp3
X (2015).mp3
Y (2012).mp3

C:\> test.bat
ren "X (2015).mp3" "X.mp3"
ren "Y (2012).mp3" "Y.mp3"
Aacini
  • 65,180
  • 12
  • 72
  • 108