Here's a simpler way to do it, but you'll need
IanM's Matrix1Utils Plugin. That plugin is pretty much a must have for any DBPro user anyway, since it provides so many useful commands that DBPro doesn't have.
Anyway, here's the example. The only reason you need the plugin is for the
Replace All$ command, which replaces any text in a string with some other text. It's heavily commented, but let me know if there's something you don't understand
.
`Make an array that will hold all of the lines of our text file. This way we can store them all, and go through them individually.
DIM OurTextFile$(1000) `Supports up to 1000 lines. (You could use ARRAY INSERT AT BOTTOM for as many lines as needed, but I'm doing it this way to keep it simple.)
`Say that the following lines are the original text file (THIS IS A VISUAL AID).
Print "Here is our original text file:"
Print "-----------------------------------------------------------------------------------------------------------------------------"
`Start on line 0 of the file.
LineNum=0
`Open to file for reading, so we can store all of the lines.
Open To Read 1,"TestFile.txt"
`Go through the file and read each line. Then store it in the corresponding slot of the array. Line 1 gets stored in array slot 1, line 2 in array slot 2, etc.
Repeat
`Read the line
Read String 1,ThisLine$
`Increase our array's "line number" (a.k.a. its slot)
LineNum=LineNum+1
`Set this slot of the array equal to the line we just read.
OurTextFile$(LineNum)=ThisLine$
`Print the line to the screen. (THIS HAS NOTHING TO DO WITH THE REPLACEMENT, IT IS JUST A VISUAL AID.)
Print OurTextFile$(LineNum)
`Keep doing this until there are no more lines to read.
Until File End(1)
`Close the file. We're done reading it.
Close File 1
`Say we're going to make some replacements (THIS IS A VISUAL AID).
Print "-----------------------------------------------------------------------------------------------------------------------------"
Print "Now we're going to make some replacements..."
Print "After the replacements have been made, our new text file will be printed below:"
Print "-----------------------------------------------------------------------------------------------------------------------------"
`Now, go through each line and replace what we're trying to find with what we're replacing it with. The Replace All$ command will need IanM's Matrix1Utils plugin.
`We are currently replacing "Hello" with "Goodbye". Keep in mind it's case sensitive.
`If you want to replace more things, just add another line with what else you want to replace. I have commented out the line that would replace the letter "a" with the letter "q". Try uncommenting it and see what happens.
for l = 1 to LineNum
OurTextFile$(l)=Replace All$(OurTextFile$(l),"Hello","Goodbye")
`OurTextFile$(l)=Replace All$(OurTextFile$(l),"a","q")
next l
`If the file we are about to create already exists, then delete it. OPEN TO WRITE won't work if the file already exists.
if file exist("TestFileReplaced.txt") then delete file "TestFileReplaced.txt"
`Now, let's make a new file with our new text.
Open To Write 1,"TestFileReplaced.txt"
`Go through each line, and write the line in the file. We just replaced the lines, so the new lines are being written.
for l = 1 to LineNum
Write String 1,OurTextFile$(l)
next l
`Close the file since we're done writing to it.
Close File 1
`Now, to prove we saved it and everything, let's read the file we just made and print its contents to the screen. (THIS IS A VISUAL AID.)
Open To Read 1,"TestFileReplaced.txt"
repeat
Read String 1,ThisLine$
Print ThisLine$
until file end(1)
`Wait for the user to press a key before we exit.
Wait Key
I have attached a text file for use, but feel free to make your own. The code will open the text file (it's called "TestFile.txt"), replace "Hello" with "Goodbye", and then save it as "TestFileReplaced.txt".
EDIT: Looks like Grog beat me to it
. Both examples do pretty much the same thing, only his doesn't need arrays.