|
-
Oct 11th, 2000, 07:51 AM
#1
Thread Starter
New Member
Hi,
I'm looking for a way to search in txt files - I'm looking for a certain string - and then replace that string with another one.
I'm trying to figure out how this goes with Get and Put, but things haven't gone too well.
Someone out there could help me a hand?
Thanks,
Alain
-
Oct 11th, 2000, 08:28 AM
#2
Junior Member
Code:
Text1 = Replace(Text1.Text, "string", "string2") ' This will replace string with string2
-
Oct 13th, 2000, 08:21 AM
#3
Thread Starter
New Member
Need Change in FILE - not just in string
Well, off course I thought about this solution myself
May I didn't make myself clear enough: I don't need a certain string from a file and then take it and change it for some use. I need the string IN THE FILE to be changed!
Replace will just change the string I took with Line Input, but it won;t change the tet in the file itself - and that is
what needs to be done. I guess I need to work with get and put, but the question is how. My problem is not finding and changing the string, but having it replaced/written in the file itself.
alain
-
Oct 13th, 2000, 08:37 AM
#4
Junior Member
Save the file after replacing the string
Code:
Open "FileName" For Append As #1
Print #1, Text1.text
Close #1
-
Oct 13th, 2000, 08:43 AM
#5
Fon't use Append, use Output.
-
Oct 13th, 2000, 09:13 AM
#6
_______
<?>
Code:
'search for a particular line and do something with it
'read the file into an array
'find the line you are looking for
' then read the array back into the file using Output
'so you can overwrite the original file
Option Explicit
Option Compare Text
Private Sub Form_Load()
Dim myArr() As Variant, myLine As String
Dim myComp As String
Dim i As Integer, intNum As Integer
myComp = "The line I am Looking for"
intNum = FreeFile
'
'open file
Open "C:\myfile.txt" For Input As intNum
Do While Not EOF(intNum)
i = i + 1
ReDim Preserve myArr(1 To i) As Variant
Line Input #intNum, myLine
myArr(i) = myLine
myArr(i) = Trim(myArr(i))
If myArr(i) = myComp Then
'do whatever you want to here I change it to read "My Fixed Line"
myArr(i) = "My Fixed Line"
End If
Loop
Close #intNum
'open file and save new contents
Open "c:\myfile.txt" For Output As intNum
For i = 1 To UBound(myArr)
Print #intNum, myArr(i)
Next
Close #intNum
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Oct 13th, 2000, 12:20 PM
#7
transcendental analytic
Could be done easier too :
Code:
Dim buffer as string
Open "c:\myfile.txt" for binary as #1
buffer=space(lof(1))
Get#1,,buffer
Replace(buffer, "what you want to find", "what you want to replace it with")
Put#1,1,buffer
close #1
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 13th, 2000, 03:24 PM
#8
You forgot to replace the buffer with the updated version.
Code:
Buffer = Replace(buffer, "what you want to find", "what you want to replace it with")
-
Oct 13th, 2000, 03:33 PM
#9
transcendental analytic
Sorry, things happens, maybe i'm just tired
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 14th, 2000, 12:02 AM
#10
There is a problem with your method. Eg:
The text-file contains: "One big step for man"
Your two buffers might be for example:
1) "One big st"
2) "ep for man"
What happens if the user wants to find the string "step" ?........
-
Oct 14th, 2000, 06:40 AM
#11
transcendental analytic
I guess you don't mean my method? Hesaidjoe's search for whole lines...
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|