Solved Batch file to replace text in file

Dell / OPTIPLEX 330
October 1, 2009 at 13:23:06
Specs: Microsoft Windows XP Professional, 1.595 GHz / 1012 MB
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


See More: Batch file to replace text in file

Report •


✔ Best Answer
October 5, 2009 at 04:08:55
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 felicity

M2



#1
October 1, 2009 at 13:41:30
What do you have so far?

Report •

#2
October 1, 2009 at 13:45:20
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:

http://www.fireflysoftware.com/TEXT...


Report •

#3
October 1, 2009 at 13:58:54
"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.


Batch Variable how to


Report •

Related Solutions

#4
October 1, 2009 at 14:15:39
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.


Report •

#5
October 1, 2009 at 14:20:25
@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 felicity

M2


Report •

#6
October 1, 2009 at 14:32:19
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.


Report •

#7
October 2, 2009 at 04:49:18
Not obvious what's going on.

Do you *HAVE* a replace.txt?


=====================================
Helping others achieve escape felicity

M2


Report •

#8
October 2, 2009 at 05:12:13
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 felicity

M2


Report •

#9
October 5, 2009 at 03:33:55
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!!!


Report •

#10
October 5, 2009 at 03:43:24
"I want to replace it in the same file."

I don't think it can be done in batch.


=====================================
Helping others achieve escape felicity

M2


Report •

#11
October 5, 2009 at 03:59:03
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
hjgj

I am tryinng to replace 601 with 500


Report •

#12
October 5, 2009 at 03:59:46
It creates a new file which is empty.

Report •

#13
October 5, 2009 at 04:08:55
✔ Best Answer
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 felicity

M2


Report •

#14
October 5, 2009 at 05:00:27
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.


Report •

#15
October 5, 2009 at 05:31:51
You need the double > and you need those two lines separate.


=====================================
Helping others achieve escape felicity

M2


Report •

#16
October 5, 2009 at 06:00:17
Thank you!!!! Thats was great help.

Report •


Ask Question