Solved

Recursively Rename

Posted on 2008-06-15
10
2,747 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

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…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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