Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi All,
I would like to have a batch file which would replace a string in a file.
It would be great if the user can define the string to replace and also the one to which it needs to be replced with.
Appreciate any help on this. Thanks

I have no idea how to go about this. I am seracing online and saw a few suggestions and tried them but nothing worked.
Saw a few softwares that can do them "text crawler" but nothing that can be doen through a batch file.
Also the link below might be useful:

"Saw a few softwares that can do them "text crawler" but nothing that can be doen through a batch file."
If you are open to using third party utilities(as it seems) I would suggest sfk (just the standard version) it has LOTS of really good text parsing features(including text/binary replace) and can be run from a batch file.

Hi Judago,
Thanks for the suggestion. Will check it out. However I would prefer to do this through batch file itself rather than thrid party utility.
Apologize if my earlier post was misleading.

@echo off > newfile & setLocal enableDELAYedexpansion
set /p old=old string ?
set /p new=new string ?for /f "tokens=* delims= " %%a in (myfile) do (
set str=%%a
set str=!str:%old%=%new%!
>> newfile echo !str!
)
=====================================
Helping others achieve escape felicityM2

Hi Mechanix,
I tried your command and it gives a new file which is empy.Also tried with the below modification:
@echo off > newfile & setLocal enableDELAYedexpansion
set /p old=old string ?
set /p new=new string ?for /f "tokens=* delims= " %%a in (replace.txt) do (
set str=%%a
set str=!str:%old%=%new%!
>> replace.txt echo !str!
)
pause
It makes a new file which is empty.

Not obvious what's going on.
Do you *HAVE* a replace.txt?
=====================================
Helping others achieve escape felicityM2

In your modified code you're using the same file as source and destination.
Bad idea.
The purpose of this bit:
@echo off > newfile
is to create an empty file when the script runs.
=====================================
Helping others achieve escape felicityM2

Hi Mechanix2Go,
Thanks for your reply. I want to replace it in the same file. What I am trying to do is explode an ear bundle and then replace certain value in a file and then bundle it up again.
I checked one of your forums and tried change.com which you havd suggested to someone else. It worked and it really great.
So my issues is kinda solved. However I would like to learn how I can do this just by using DOS commands and no thrid party utility.
Appreciate all you help!!!

"I want to replace it in the same file."
I don't think it can be done in batch.
=====================================
Helping others achieve escape felicityM2

Through change.com it is possible to replace it in the same file.
However, I tried your code and it is creating a new file but there is nothing being written in it. Am I doing something wrong?
Code:
========================================
@echo off > newfile & setLocal enableDELAYedexpansion
set /p old=old string ?
set /p new=new string ?for /f "tokens=* delims= " %%a in (myfile.txt) do (
set str=%%a
echo %%str
set str=!str:%old%=%new%!> newfile echo !str!
)
pause==============================
Contents of myfile.txt
601
hjhgjghjh
hjgjI am tryinng to replace 601 with 500

You can't just slash away and hope the resulting code will work.
This is useless:
set str=!str:%old%=%new%!> newfile echo !str!
What you need is:
set str=!str:%old%=%new%!
>> newfile echo !str!
=====================================
Helping others achieve escape felicityM2

Thanks that worked. Could you please explain what i was doing wrong. I has one >
Was that the problem. Am kinda learning so forgive me for asking simple questions.
Appreciate all your help. Thanks a lot.

You need the double > and you need those two lines separate.
=====================================
Helping others achieve escape felicityM2

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |