Rename certain filetypes

October 16, 2010 at 05:19:27
Specs: Windows 7 HPrem, 2Ghz/3Gb
I have around 1,000 Sub-folders all with files in.

One particular file which is always called <folder name>_index.htm needs to be renamed to just index.htm.

How can I do this with a batch file?


See More: Rename certain filetypes

Report •

#1
October 16, 2010 at 05:39:49
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b *.htm') do (
set F=%%a
set F=!F:*_=!
ren %%a !F!
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
October 16, 2010 at 05:45:00
will this rename all .htm files in the directory as I also have over .htm files in the folders.

I just want to rename the *_index.htm files

sorry if i didn't mention that previously


Report •

#3
October 16, 2010 at 06:00:50
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b *_index.htm') do (
set F=%%a
set F=!F:*_=!
echo ren %%a !F!
)


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
October 16, 2010 at 06:19:14
thanks so much ...

M2 your a GENIUS!


Report •

#5
October 16, 2010 at 06:37:08
Hey M2,

just tried a test run on this and comes back with an error of File Not Found


Report •

#6
October 16, 2010 at 21:40:24
So are there any *_index.htm files there?

Do this:

dir *_index.htm


=====================================
Life is too important to be taken seriously.

M2


Report •

#7
October 17, 2010 at 01:44:07
I've got one *_index.htm file in the root directory and then I have around 1,000 *_index.htm in the sub-directories so yes there are file/s in these folders/sub-folders.


Report •

#8
October 17, 2010 at 02:34:18
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b/s/a-d *_index.htm') do (
set F=%%~Na%%~Xa
set F=!F:*_=!
echo ren %%a !F!
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#9
October 17, 2010 at 03:46:00
Absolutely spot on! Cheers again M2

Report •

Ask Question