Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find and Replace Text

Status
Not open for further replies.

PU36

MIS
Mar 16, 2005
202
0
0
US
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
 
One more thing. Im going to be using this in a NT environment (NT/2000/XP/2003)
 
You'll have to use something besides a batch file to read what's already in the text file and then replace it. You can use QBASIC (still useful for simple things like this) or VBScript or JScript without having to buy anything.

Lee
 
Bummer.. I was trying to keep everything to one file. Oh well.

Any idea of what that vb script would look like?
 
Put something together and then ask if you have questions you can ask them in the VBScript forum.

Lee
 
I was thinking of something like this withn the batch fil;e.

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


But this method requires you to pull in an external file. C:> batch.bat myfile.txt>. I want to be able to rewrite without pulling from an external source.

Any ideas?
 
Do a search on Google for "batch files" to see if you can find something then. I've been working with DOS batch files since the early 90s, and haven't seen anything about them that will read from a file, parse it, then rewrite it without the help of a programming language.

Lee
 
It would be reasonably challenging in a batch file. I'd suggest UltraEdit - if it's a one-time shot (or even if it's something you'll do occasionally) - and simply create and save a macro to search for the string, go to the end of line, insert a <cr> and enter the text you want inserted. Or, if you'll need to do this a lot, do it in plain old BASIC (any flavor). Should be fairly simple.

If you have any questions, give me more specifics and I'll see what I can do to help.

Tom

"My mind is like a steel whatchamacallit ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top