I'm not sure if you want a batch file to:
- Rename 1 specific file
- Rename all files in a directory
- Rename files in a directory that match a particular pattern
- Something else ?
Here is an example of a batch file to do as in # 3 (Rename files in a directory that match a particular pattern).
Run the batch file to make sure it lists all the files that you want to have renamed. Initially, the old and new filenames will be displayed for all files found, but no files will be renamed. You might have to modify the value of searchpattern to display the files you want. Once you have the correct files listed, proceed with the instructions below the batch file to make renaming active.
@echo off
set "searchfor=domainname"
set "replacewith=otherdomainname"
set "searchpattern=*.%searchfor%.*"
for %%f in ("%searchpattern%") do call :work "%%~f"
set "searchfor="
set "replacewith="
set "searchpattern="
set "filematched="
set "filenewname="
goto :EOF
:work
set filematched=%~1
rem You can't do it directly like:
rem set "filenewname=%filematched:%searchfor%=%replacewith%%"
for /F "usebackq delims=" %%g in (`echo set "filenewname=%%filematched:%searchfor%=%replacewith%%%"`) do %%g
echo Renaming "%filematched%" to "%filenewname%"
rem delete the next line (goto :EOF) to make renaming active
goto :EOF
rem this line actually does the file renaming
ren "%filematched%" "%filenewname%"
goto :EOF
Note: To prevent the wrong files from being renamed, or files being renamed in a wrong way, the batch file will display the old and new filenames for all files found, but no files will be renamed.
Once you have run the batch file and are confident the correct files will be properly renamed, you can edit the file to remove the line(s) described to make renaming active, then run the batch file again.
To do that, find the two lines that are like this:
rem delete the next line (goto :EOF) to make renaming active
goto :EOF
Then, remove the line that says "goto :EOF" (or remove both lines).
Don't remove "goto :EOF" from any other place in the batch file (it can be found in a few places so be sure to remove the correct one).
If you need any of this batch file explained to you, or if it's not doing what you really want it to do, please let me know.
1BTW - normally I would choose a less masochistic language like python, perl or even vbscript for your string manipulation needs. – joel – 2012-11-16T05:03:31.327