Solved

Recursively Rename

Posted on 2008-06-15
10
2,740 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
  • 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

772 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