Computing.Net > Forums > Programming > Batch file to replace text in file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to replace text in file

Reply to Message Icon

Name: automation
Date: October 1, 2009 at 13:23:06 Pacific
OS: Microsoft Windows XP Professional
CPU/Ram: 1.595 GHz / 1012 MB
Product: Dell / OPTIPLEX 330
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: October 1, 2009 at 13:41:30 Pacific
Reply:

What do you have so far?


0

Response Number 2
Name: automation
Date: October 1, 2009 at 13:45:20 Pacific
Reply:

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


0

Response Number 3
Name: Judago
Date: October 1, 2009 at 13:58:54 Pacific
Reply:

"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


0

Response Number 4
Name: automation
Date: October 1, 2009 at 14:15:39 Pacific
Reply:

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.


0

Response Number 5
Name: Mechanix2Go
Date: October 1, 2009 at 14:20:25 Pacific
Reply:

@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


0

Related Posts

See More



Response Number 6
Name: automation
Date: October 1, 2009 at 14:32:19 Pacific
Reply:

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.


0

Response Number 7
Name: Mechanix2Go
Date: October 2, 2009 at 04:49:18 Pacific
Reply:

Not obvious what's going on.

Do you *HAVE* a replace.txt?


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

M2


0

Response Number 8
Name: Mechanix2Go
Date: October 2, 2009 at 05:12:13 Pacific
Reply:

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


0

Response Number 9
Name: automation
Date: October 5, 2009 at 03:33:55 Pacific
Reply:

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!!!


0

Response Number 10
Name: Mechanix2Go
Date: October 5, 2009 at 03:43:24 Pacific
Reply:

"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


0

Response Number 11
Name: automation
Date: October 5, 2009 at 03:59:03 Pacific
Reply:

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


0

Response Number 12
Name: automation
Date: October 5, 2009 at 03:59:46 Pacific
Reply:

It creates a new file which is empty.


0

Response Number 13
Name: Mechanix2Go
Date: October 5, 2009 at 04:08:55 Pacific
Reply:

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


0

Response Number 14
Name: automation
Date: October 5, 2009 at 05:00:27 Pacific
Reply:

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.


0

Response Number 15
Name: Mechanix2Go
Date: October 5, 2009 at 05:31:51 Pacific
Reply:

You need the double > and you need those two lines separate.


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

M2


0

Response Number 16
Name: automation
Date: October 5, 2009 at 06:00:17 Pacific
Reply:

Thank you!!!! Thats was great help.


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Batch file to replace text in file

Batch to replace text in text file www.computing.net/answers/programming/batch-to-replace-text-in-text-file/19166.html

batch file to filter text in file www.computing.net/answers/programming/batch-file-to-filter-text-in-file/14460.html

Replacing text in files BAT file www.computing.net/answers/programming/replacing-text-in-files-bat-file/16818.html