rename multiple files in multiple folder

I have multiple files in multiple folders (20+) and I would like to bulk rename the files by changing the extensions of all files.

Can this be done from the command window in Windows XP?
How about from terminal in Linux?

For example let's say I have a folder "testfolder" on the D drive.  In d:\testfolder there are multiple folders and I want to rename the files inside all of those folders.

Thanks
LVL 12
snailcatAsked:
Who is Participating?
 
knightEknightConnect With a Mentor Commented:
for /f "delims=" %F in ('dir/s/b  d:\testfolder\*') do  ren "%F"  "%~nF.ext"
0
 
knightEknightCommented:
where "ext" is the new extension.  You may also want to change the filespec in the command above to something besides * like *.txt or what ever the matching files are that you are trying to rename.

Also, if you want to use this in a .bat file instead of a command prompt, you need to double the percent symbols like this:

for /f "delims=" %%F in ('dir/s/b  d:\testfolder\*') do  ren "%%F"  "%%~nF.ext"
0
 
vopCommented:
Bulk Rename Utility will do it.

www.bulkrenameutility.co.uk
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
knightEknightCommented:
also, let's remove the possibility of including directories in our script ... add /a-d to the dir command like this:   dir/s/b/a-d
0
 
Brian PiercePhotographerCommented:
I always use the Lupus Rename Utility - a doddle to use http://rename.lupasfreeware.org/download.php
0
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
Nowt wrong with above, is what I would normally do.  You can also use the /r option on for command to work down all files, e.g.

@Echo off
cd /d C:\startdir
for /r %%a in ("*.txt") do echo ren "%%~Fa" "%%~na.ext"

Would rename all .txt fies to .ext

If there are a LOT of files and less subdirs it might be better to use a dir or for /d command on the dirs and then use a rename *.* on the whole dir perhaps?

Steve
0
 
knightEknightCommented:
Building on Steve's last suggestion, if the files you are renaming all have the same initial extention (like .txt for example) you can do this:

cd/d "d:\testfolder"
for /d /r %D in (*) do  ren  "%D\*.txt"  *.ext
0
 
Steve KnightIT ConsultancyCommented:
Yup that sort of thing :-)
0
 
Bill PrewCommented:
This was an interesting one for me.  It looks like we've covered the basic approaches here, so I wont add to that.

But I was interested in the FOR /D /R construct.  Wondering if knight or Steve know what other switched can be used in combination on the FOR command?  I typically think of them as only one allowed at a time, but clearly sometimes more than one are allowed...

~bp
0
 
snailcatAuthor Commented:
Thanks everybody.  It was an interesting discussion.
0
 
Steve KnightIT ConsultancyCommented:
A good question bill.... Can't see how /f or /l could combine with anything for starters and it does moan quickly if you get option in wrong order for each option etc.... i suppose r and d are special case here and better idea than using dir /ad /s /b as it will process as it goes along rather than after a whole dir listing is completed - fairly sure I've seen that used before here, possibly even me too?!  something to play with when bored at some point.... No doubt paultomasi has a lengthy response?
0
 
Paul TomasiCommented:
Arrived here from http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_27376922.html#a36905071

Just looked at FOR /R /D %%....

No lengthy response here other than to say my research tells me it is undocumented and buggy.

In any event, FOR /L, FOR /D and FOR /R (and not forgetting FOR all by itself) are enough for all my batch needs.

I would suggest not using FOR /R /D %%.... in the future.
0
 
Paul TomasiCommented:
Oops! And how could I not mention FOR /F....

:)
0
 
Steve KnightIT ConsultancyCommented:
what leads you to see it as buggy paul?  it gave me the same results on a windows 2000, XP, and windows 7 machine and was faster than for /f against for /f with dir /b /ad.

Frankly I have always used for /f as I feel more in control than with for /r or for /d or now /r  /d but they all have their place.

Main problem with for /f version, lets say processing against massive drive is nothing happens for few mins while it processes dir then it does the job.

Steve
0
 
Paul TomasiCommented:
>> "what leads you to see it as buggy..."

http://ss64.com/nt/for_d.html

It states: "The option /D /R is undocumented and somewhat buggy, it fails for root folders. FOR /R is often a better choice".


FOR /D on it's own is faster than FOR /F with DIR /B /AD as well - and processing start immediately unlike that of DIR.

I've noticed there are occasions where experts have used FOR /F instead of FOR /D or FOR /R and I suspect this is due to either laziness or inexperience.

I do like I idea of FOR /D /R though. It saves having to write a recursive function...
0
 
Steve KnightIT ConsultancyCommented:
"I suspect this is due to either laziness or inexperience."

Come on Paul...  like I said all have their place.... for instance for /f against dir /b etc. can be used with filters using find findstr etc. and has the benefit that you can do the dir first to see what would be included  etc.

One thing I wonder is if you are, say, deleting folders or files and using for /r or for /d or for /r /d does it deal with that?  Doing a for /f against dir /b /s /ad for instance would have a pre determined list already.

Steve
0
 
Paul TomasiCommented:
>> "Come on Paul..."

Okay, I take it back... Actually, after second thoughts (see below), I'm going to change that to 'convenience'. 'Laziness' was a little harsh!

FOR /F with DIR is great. For small to medium numbers of files/folders it's good however, for massive amounts of files/folders it can take an apprecaible while for FOR to start looping.

I've just spent a little time playing with the various FORs only to remind myself why I dislike the quirkiness of FOR /R and the restrictiveness of FOR /D. FOR /R /D addresses some of these problems and it may prove to be a popular choice even with me! - All I can say at this point is watch this space!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.