Wednesday, April 25, 2018

windows 7 - Remove a part of a list of names


I have some files and each files have distinct name but all of them have the same "-statement" at the end like this:


flower-statement.mp3
leave-statement.mp3
ball-statement.mp3
goal-statement.mp3

So I want to remove this: -statement


Is that possible?


Answer



Why can't you just do this with batch like so without any special programs? From what I tested, this will do exactly what you're looking for it to do which is parse out the -statement part of the file name and then rename each without it leaving the other parts of the file. This will work for all .mp3 files in a specific folder just fine.


In my below example where I have the C:\Users\User\Desktop\Test\*.mp3 just put your folder name in that location. Save that to a text file and rename to batch.cmd or something like that. Then just double-click to run it to perform the rename of the files in that folder parsing out the -statement part of it.


You can also make this script implicit to run the directory which you copy it to and just using *.mp3 from there rather than the full path.


EXPLICIT SCRIPT (specify the directory where *.mp3 are located)


@ECHO ON
FOR %%A IN ("C:\Users\User\Desktop\Test\*.mp3") DO (
CALL :RenameFiles "%%~A" "%%~NXA"
)
GOTO EOF
:RenameFiles
SET fname=%~2
SET renname=%fname:-statement=%
REN "%~1" "%renname%"
GOTO EOF

IMPLICIT SCRIPT (copy to and then run from the directory where *.mp3 are located)


@ECHO ON
FOR %%A IN ("*.mp3") DO (
CALL :RenameFiles "%%~A" "%%~NXA"
)
GOTO EOF
:RenameFiles
SET fname=%~2
SET renname=%fname:-statement=%
REN "%~1" "%renname%"
GOTO EOF



ADD SOMETHING TO THE END OF A RENAMED FILE BEFORE THE FILE NAME EXTENSION


(change fpart and mp3path to match what you need those to be for your usage. The fpart is what you need to add and the mp3path is the folder where the MP3 files reside.)


NOTE: This still assumes the MP3 files have -statement in the file name to be stripped out.


EXCPLICIT SCRIPT (with parse file name characters)


@ECHO ON
SET mp3path=C:\Users\User\Desktop\Test
SET fpart=_something
FOR /F %%A IN ('DIR /B "%mp3path%\*.mp3"') DO (
CALL :RenameFiles "%%~FPA" "%%~NA" "%%~XA"
)
GOTO EOF
:RenameFiles
SET fname=%~2
SET ext=%~3
SET renname=%fname:-statement=%
REN "%~1" "%renname%%fpart%%ext%"
GOTO EOF

EXCPLICIT SCRIPT (no parse just add file name characters)


@ECHO ON
SET mp3path=C:\Users\User\Desktop\Test
SET fpart=_something
FOR /F %%A IN ('DIR /B "%mp3path%\*.mp3"') DO (
CALL :RenameFiles "%%~FPA" "%%~NA" "%%~XA"
)
GOTO EOF
:RenameFiles
SET fname=%~2
SET ext=%~3
REN "%~1" "%fname%%fpart%%ext%"
GOTO EOF

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...