Batch script to rename portionof a filename

1

1

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

An example would be:

projectname.vcproj.domainname.username.user

to

projectname.vcproj.otherdomainname.username.user

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?

Rubik'sCube

Posted 2012-11-16T03:19:14.337

Reputation: 11

Answers

1

You want to use DOS string substitution.

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

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

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

http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace

joel

Posted 2012-11-16T03:19:14.337

Reputation: 255

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

0

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


: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.

Kevin Fegan

Posted 2012-11-16T03:19:14.337

Reputation: 4 077

@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 – 2012-11-27T18:03:31.347