Page 1 of 1 1
Topic Options
#66019 - 2002-06-03 11:14 PM Replace string within a .txt file
Aarons Offline
Lurker

Registered: 2002-06-03
Posts: 1
I am trying to search a text file (i.e. "test.txt") for a specific string (i.e. "change me") and
replace it with another string (i.e. "complete"). Does anyone know how I would go about this?

Thanks!
Aaron

Top
#66020 - 2002-06-03 11:36 PM Re: Replace string within a .txt file
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
Before writing you a script.. (have you read the manual?).. this is your basic procedure:

1. open the file
2. read a line of the file
3. Check to see if the string is in the line and replace it (using instr and substr) if it is.
4. Open a second temporary file
5. Write the line to the file.
6. Repeat from #2 until the file is finished.
7. Close both files.
8. copy the temporary file over the first file.
9. delete the temporary file.

(I would probably write you a script for this but I don't have time at the moment.. someone else may do this for you! [Big Grin] )

Brian

[ 03 June 2002, 23:38: Message edited by: BrianTX ]

Top
#66021 - 2002-06-03 11:49 PM Re: Replace string within a .txt file
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Rule #2: Search the BBS!
Here's what I get when searching for 'replace string file':
Modifying Text in a Text File
_________________________
There are two types of vessels, submarines and targets.

Top
#66022 - 2002-06-03 11:52 PM Re: Replace string within a .txt file
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
Good point, Jens... You know me.. always reinventing the wheel.. [Big Grin]

Brian

Top
#66023 - 2002-06-04 03:44 AM Re: Replace string within a .txt file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Aarons,
If you are setting up the text files for your own use, I suggest you follow the INI format. That way you can use ReadProfileSrting() and WriteProfileString() to do in-line changes without the need to rewwrite the entire file every time.

See also, in the FAQ section.Topic: File Input/Output Primer
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#66024 - 2002-06-04 04:07 AM Re: Replace string within a .txt file
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
If you want to manage the input/output using Kixtart or just get practice writing your script, then the others have provided excellent advice.

If you just have text files that someone else generated and you need to swap text checkout Qreplace.exe.

[ 04 June 2002, 04:07: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1077 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.093 seconds in which 0.066 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org