Batch Renamer not actually renaming

June 25, 2009 at 23:34:36
Specs: Windows Vista, Core2Quad Q6700 4GB RAM
Hello, I am trying to write a batch file that will enable me to remove parts of several file names.

i.e. Led-Zeppelin_Led-Zeppelin-IV_Black Dog.mp3 changes to Black Dog.mp3

I would like the prefix to be user input when the batch file runs.

What I have so far seems to work, but the files don't actually rename. I am thinking it may be a Vista problem, or do I have a code error?

@echo off
setlocal enabledelayedexpansion
set /p PREFIX=Input the part of the file name to remove:
for %%* in (%prefix%*.*) do (
set "name=%%~nx*"
echo ^
REN %%~fx* !name:%prefix%=!
)
echo Done & pause


See More: Batch Renamer not actually renaming

Report •


#1
June 26, 2009 at 03:44:44
@echo off & setLocal enableDELAYedexpansion

set /p PREFIX=Input the part of the file name to remove:

for /f "tokens=* delims= " %%a in (' dir/b *.mp3 ^| find /i "%PREFIX%" ') do (
set name=%%~Na
set name=!name:%PREFIX%=!
ren "%%a" "!name!.mp3"
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
June 26, 2009 at 11:49:32
Thanks man, it works. Would you mind explaining what you did in order to make it work?

What are For /f commands used for?

It looks like you are breaking up the filename into "tokens" at the spaces. What purpose does this serve (or is it so that you can use a For /f command)? It looks like you are building a list of files with their long names using dir/b of type .mp3, though I am not sure what the "^| find /i" is doing.

I edited it a little bit so that it works for all file types...

for /f "tokens=* delims= " %%a in (' dir/b *.* ^| find /i "%PREFIX%" ') do (
set name=%%~Na
set ext=%%~Xa
set name=!name:%PREFIX%=!
ren "%%a" "!name!!ext!"
)


Report •

#3
June 26, 2009 at 13:12:28
"not sure what the "^| find /i" is doing"

It's weeding out the files which don't contain the string.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions


Ask Question