Solved

rename multiple files in multiple folder

Posted on 2011-09-19
17
460 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:snailcat
  • 5
  • 4
  • 4
  • +4
17 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 400 total points
Comment Utility
for /f "delims=" %F in ('dir/s/b  d:\testfolder\*') do  ren "%F"  "%~nF.ext"
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
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
 
LVL 5

Expert Comment

by:vop
Comment Utility
Bulk Rename Utility will do it.

www.bulkrenameutility.co.uk
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
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
 
LVL 70

Expert Comment

by:KCTS
Comment Utility
I always use the Lupus Rename Utility - a doddle to use http://rename.lupasfreeware.org/download.php
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 100 total points
Comment Utility
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
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
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
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Yup that sort of thing :-)
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 
LVL 12

Author Closing Comment

by:snailcat
Comment Utility
Thanks everybody.  It was an interesting discussion.
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
 
LVL 11

Expert Comment

by:paultomasi
Comment Utility
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
 
LVL 11

Expert Comment

by:paultomasi
Comment Utility
Oops! And how could I not mention FOR /F....

:)
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
 
LVL 11

Expert Comment

by:paultomasi
Comment Utility
>> "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
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
"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
 
LVL 11

Expert Comment

by:paultomasi
Comment Utility
>> "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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now