Solved

Recursively Rename

Posted on 2008-06-15
10
2,757 Views
Last Modified: 2010-04-21
I need a PURE DOS solution to rename files recursively based upon a mask:

*.config.*

Searching through the site I came across this code:

FOR /F "tokens=*" %A in ('dir /A:-D /S /B fid') DO REN "%A" fid.mrc

This works only if you need to rename one file recursively. I need to rename multiple files, in particular, files matching the above mask: *.config.*

I would also like to be able to add exclusions; for instance, .config, .xml, .dll. I would also like to be able to store the files renamed into a text file.

I need to add the extension .config to files matching this mask excluding files with extension .config, .xml, .dll, and so on. For example,

web.config            ------>    DO NOT RENAME   ---->   web.config
web.config.bak     ------>          RENAME           ---->   web.config.bak.config
web.config.7634   ------>          RENAME           ---->   web.config.7634.config
program.config.34 ------>          RENAME           ---->   program.config.34.config
program.config.dll  ------>    DO NOT RENAME   ---->   program.config.dll
0
Comment
Question by:mastermindg89
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 35

Expert Comment

by:torimar
ID: 21788789
Please check out this freeware utility: http://www.brothersoft.com/lupas-rename-download-61151.html

If it should be capable to do what you need done, you can have it generate a .BAT file for use in a DOS environment.

Hope this helps.
0
 

Author Comment

by:mastermindg89
ID: 21788822
I've tried Lupas and it is an impressive tool. However, it does not offer exclusions.

The mask *.config.* brings up all .config files and I don't want to rename these files just those with added extensions.
0
 
LVL 8

Expert Comment

by:devil_himself
ID: 21790332
SCRIPT TEST

These Were The Files In Folder
==
web.config
web.config.bak
web.config.7634
program.config.34
program.config.dll
==

SCREEN OUTPUT OF SCRIPT
==
ren "C:\Source\program.config.34" "program.config.34.config"
ren "C:\Source\web.config.7634" "web.config.7634.config"
ren "C:\Source\web.config.bak" "web.config.bak.config"
==



@echo off
setlocal
set source=c:\source
set dest=c:\dest
set excludeExt=.config .xml .dll
pushd "%source%"
for /f "tokens=*" %%a in ('dir "*.config.*" /A:-D /S /B ^|findstr /I /E /V "%excludeExt%"') do echo ren "%%a" "%%~nxa.config"

Open in new window

0
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 

Author Comment

by:mastermindg89
ID: 21791309
The script works great. It lacks only one thing. There may be some files renamed like this:

web.config2234
web.configconfig.test

The script doesn't rename these, more than likely because of the .config in the extension.
0
 
LVL 8

Expert Comment

by:devil_himself
ID: 21791349
"web.config2234" is not included in mask --> *.config.*
0
 
LVL 8

Expert Comment

by:devil_himself
ID: 21791359
*.config* will solve all your problems
@echo off
setlocal
set source=c:\source
set dest=c:\dest
set excludeExt=.config .xml .dll
pushd "%source%"
for /f "tokens=*" %%a in ('dir "*.config*" /A:-D /S /B ^|findstr /I /E /V "%excludeExt%"') do echo ren "%%a" "%%~nxa.config"

Open in new window

0
 

Author Comment

by:mastermindg89
ID: 21791378
I also altered the script to rename rather than print the rename commands. I need the files renamed as well as a list of the files renamed in a file.

so %dest% would be a text file and the script would do rename followed by an echo to the text file.
0
 
LVL 8

Accepted Solution

by:
devil_himself earned 500 total points
ID: 21791403

@echo off
setlocal
set source=c:\source
set logfile=log.txt
set excludeExt=.config .xml .dll
pushd "%source%"
for /f "tokens=*" %%a in ('dir "*.config*" /A:-D /S /B ^|findstr /I /E /V "%excludeExt%"') do ren "%%a" "%%~nxa.config" & echo "%%a" --^> "%%~nxa.config" >> %logfile%

Open in new window

0
 

Author Comment

by:mastermindg89
ID: 21791527
Thanks for all the help and your patience.
0
 

Author Closing Comment

by:mastermindg89
ID: 31467355
Thanks again.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reusing a former RAID Member as a data drive 18 189
For loop to add numbers 3 105
how to get the largest file size? 33 63
Date variable in batch file 2 83
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 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