Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been trying to make a script that will a file name and change only one word in it.

An example would be:




I've tried using the if loop to list the directory and set the delimiter to a period but it doesn't seem to be able to identify and change it. I'm using examples of renaming .txt files but it doesn't seem to work, any suggestions?

share|improve this question

You want to use DOS string substitution.

C:\>set proj=projectname.vcproj.domainname.username.user
C:\>echo %proj%

C:\>set proj=%proj:domainname=otherdomainname%
C:\>echo %proj%

You can get more ideas searching the interwebs for "DOS string manipulation" and "DOS for loop".

share|improve this answer
BTW - normally I would choose a less masochistic language like python, perl or even vbscript for your string manipulation needs. – joel Nov 16 '12 at 5:03

I'm not sure if you want a batch file to:

  1. Rename 1 specific file
  2. Rename all files in a directory
  3. Rename files in a directory that match a particular pattern
  4. 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

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.

share|improve this answer
@Rubik'sCube - How did this work for you ? If you need any modifications to the example code, just let me know. If you feel your question has been answered, you could click the "Check-mark" next to the answer you feel is best to let everyone know the question has been answered. – Kevin Fegan Nov 27 '12 at 18:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.