Solved batch rename files in directory

October 5, 2011 at 14:01:04
Specs: Windows XP
A have a bunch of files to rename. the original file name format is:
1234567.abc
I want:
R1_T234_S567.abc

I can get a batch to change the format of text as follows, but not to actually rename the files in a directory.

set filename=1234567.abc
Set NewFileName=R%FileName:~0,1%_T%FileName:~1,3%_S%FileName:~5,20%
echo %NewFileName%

any help is appreciated. thanks.


See More: batch rename files in directory

Report •

✔ Best Answer
October 6, 2011 at 07:01:52
Won't work; delayed expansion need to be in play. Also, temporary files are sloppy and require cleanup.
setlocal EnableDelayedExpansion
for %%a in (???????.abc) do (
  set name=%%~NXa
  set name=R!name:~0,1!_T!name:~1,3!_S!name:~5,20!
  ren "%%~Fa" "!name!"
)

EDIT: % =/= !
Or: Why do I even try to respond without my coffee?

How To Ask Questions The Smart Way



#1
October 5, 2011 at 14:33:45
Hi oryan. You were halfway there. This should do it...

ren %filename% %newfilename%


Report •

#2
October 5, 2011 at 14:40:30
so that worked wonderfully for one file. How do I get it to do tht for every file in the directory?

set filename=1234567.abc
Set NewFileName=R%FileName:~0,1%_T%FileName:~1,3%_S%FileName:~5,20%
ren %filename% %newfilename%


Report •

#3
October 5, 2011 at 14:41:22
Oh wait. I forgot you were asking to rename a bunch of files and not just one :). Try this...

@echo off

REM outputting list of files in directory to file
dir /b c:\directory\*.abc > filelist.tmp
REM looping through filelist.tmp to get values for loop variable a, and then renaming each
for /f "tokens=*" %%a in (filelist.tmp) do (
set filename=%%a
Set NewFileName=R%FileName:~0,1%_T%FileName:~1,3%_S%FileName:~5,20%
echo %NewFileName%
ren %filename% %newfilename%


Report •

Related Solutions

#4
October 6, 2011 at 07:01:52
✔ Best Answer
Won't work; delayed expansion need to be in play. Also, temporary files are sloppy and require cleanup.
setlocal EnableDelayedExpansion
for %%a in (???????.abc) do (
  set name=%%~NXa
  set name=R!name:~0,1!_T!name:~1,3!_S!name:~5,20!
  ren "%%~Fa" "!name!"
)

EDIT: % =/= !
Or: Why do I even try to respond without my coffee?

How To Ask Questions The Smart Way


Report •

#5
October 6, 2011 at 08:13:45
I have not been able to get either solution to work yet.

jowah's writes the tmp file just fine but won't pull from it.
Razor's doesn't seem to recognize the "new name format" and won't pull to rename the file.

Any suggestions?


Report •

#6
October 6, 2011 at 15:54:19
Razor, that worked perfectly. Much appreciated, I used this to rename 27,555 files. Nothing I could have ever accomplished manually. thanks.

Report •

#7
October 8, 2011 at 08:23:09
@echo off
setLocal EnableDelayedExpansion
for /f %%i in ('dir /b *.abc') do (
set filename=%%i
Set NewFileName=R!FileName:~0,1!_T!FileName:~1,3!_S!FileName:~5,20!
ren !filename! !newfilename!
)

Report •

Ask Question