Computing.Net > Forums > Programming > Find and Replace text in Batch 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.

Find and Replace text in Batch File

Reply to Message Icon

Name: MagnumVP
Date: March 16, 2005 at 18:06:01 Pacific
OS: Windows XP/2003
CPU/Ram: 3.0Ghz
Comment:

I need to figure out a method to find and replace text within a text file.

I have a file named, "myfile.txt" that has the following;

testing=1
database=500
rules=30

I want the batch file to find database=500 and insert destination=300 under it.

I want it to look like this;

testing=1
database=500
destination=300
rules=30

I'll be using the NT platform for this. (NT,2000,XP,2003)

Thanks

MagnumVP



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: March 17, 2005 at 00:40:27 Pacific
Reply:

The following batch for NT systems fits your need

For /F "tokens=1,2 delims==" %%A in (%1) Do (
Echo %%A=%%B>> %1.tmp
If "%%A"=="database" (
Echo destination=300>> %1.tmp))
Move %1.tmp %1

I named it Atxt.bat and to run just type

Atxt Pathname/FileName (e.g. Atxt Myfile.txt)


0

Response Number 2
Name: MagnumVP
Date: March 17, 2005 at 13:50:20 Pacific
Reply:

That gets me REALLY close. I appologize, I forgot one portion of the file. There is a [configuration] along with it, that seems to be messing with the batch.

Here is what it was;

[configuration]
testing=1
database=500
rules=30

Here is what I get; Is there a way to get the "=" from after the [configuration]?

[configuration]=
testing=1
database=500
destination=300
rules=30

ALSO... Is there a way to assign variables so you don't have to pull it from %1?

Example:

SET Filename=Myfile.txt
For /F "tokens=1,2 delims==" %%A in (%Filename%) Do (
Echo %%A=%%B>> %Filename%.tmp
If "%%A"=="database" (
Echo destination=300>> %Filename%.tmp))
Move %Filename%.tmp %Filename%

Thanks

MagnumVP


0

Response Number 3
Name: dtech10
Date: March 17, 2005 at 15:37:40 Pacific
Reply:

Hi Magnum
Doe's this help

@echo off
if exist newfile.txt del newfile.txt
for /f "tokens=*" %%a in (myfile.txt) do call :AddText "%%a
del myfile.txt
rename newfile.txt myfile.txt
exit /b

:AddText %1
set Text=%~1%
echo %Text% >> newfile.txt
if "%Text%"=="database=500" echo desination=300 >> newfile.txt
exit /b


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Find and Replace text in Batch File

find and replace string in a file www.computing.net/answers/programming/find-and-replace-string-in-a-file/19987.html

CMD script find and replace text www.computing.net/answers/programming/cmd-script-find-and-replace-text/15412.html

batch command search replace quoted www.computing.net/answers/programming/batch-command-search-replace-quoted/15879.html