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.
-
Try this solution: [http://stackoverflow.com/a/26875241/3710490](http://stackoverflow.com/a/26875241/3710490) – Valijon Nov 28 '15 at 09:05
-
1Give a clear example please. – Yazid Erman Nov 28 '15 at 09:11
-
file1 = "X (2015)" --> file 1 = "X" ; file2 = "Y (2012) --> file2 = "Y" – George R. Nov 28 '15 at 09:14
2 Answers
@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 ECHO
ed 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.

- 77,302
- 8
- 62
- 84
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"

- 65,180
- 12
- 72
- 108