Link to home
Start Free TrialLog in
Avatar of snailcat
snailcatFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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"
Avatar of vop
vop

Bulk Rename Utility will do it.

www.bulkrenameutility.co.uk
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
I always use the Lupus Rename Utility - a doddle to use http://rename.lupasfreeware.org/download.php
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Yup that sort of thing :-)
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
Avatar of snailcat

ASKER

Thanks everybody.  It was an interesting discussion.
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?
Arrived here from https://www.experts-exchange.com/questions/27376922/Batch-Script-to-Parse-Folders-within-Subfolders-to-delete-specific-Folders-and-Files-within.html?anchorAnswerId=36905071#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.
Oops! And how could I not mention FOR /F....

:)
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
>> "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...
"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
>> "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!