Solved

rename multiple files in multiple folder

Posted on 2011-09-19
17
469 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
ID: 36562394
for /f "delims=" %F in ('dir/s/b  d:\testfolder\*') do  ren "%F"  "%~nF.ext"
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 36562426
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
ID: 36562440
Bulk Rename Utility will do it.

www.bulkrenameutility.co.uk
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 33

Expert Comment

by:knightEknight
ID: 36562551
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
ID: 36562602
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
ID: 36562612
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
ID: 36562777
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
ID: 36562832
Yup that sort of thing :-)
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36564286
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
ID: 36564967
Thanks everybody.  It was an interesting discussion.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36565710
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
ID: 36907681
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
ID: 36907686
Oops! And how could I not mention FOR /F....

:)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36908002
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
ID: 36908581
>> "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
ID: 36908633
"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
ID: 36908927
>> "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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

756 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