|
-
Jan 29th, 2013, 06:55 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Edit a text file
I am trying to edit a textfile:
Open the textfile, find the item I need and then replace the new item(line) to the file.
Until now I have got this:
Code:
Open "C:\TextFile.txt" For Input As #1
While Not EOF(1)
Line Input #1, strData
'Found the line to replace
Wend
Close #1
I hope someone can help me,
Regards, Kars
-
Jan 29th, 2013, 07:14 AM
#2
Re: Edit a text file
So, what exactly is your question? What's the problem with your code?
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Jan 29th, 2013, 07:20 AM
#3
Junior Member
Re: Edit a text file
To find a string you can use this code (in case if you're loading it into rtb)
Code:
Dim text2find As String
text2find = tofind.Text
Dim posicion As Integer
posicion = InStr(1, Text2.Text, text2find)
If posicion > 0 Then
With Text2
.SelStart = posicion - 1
.SelLength = Len(tofind)
.SetFocus
End With
End If
Then just Use Replace function to replace a string. Or if you need something else, please post more details.
-
Jan 29th, 2013, 07:40 AM
#4
Thread Starter
Addicted Member
Re: Edit a text file
Thanks so far, I'll explain:
I already have found the line(string) I want to replace but I don't now how to replace it with a new string ?
-
Jan 29th, 2013, 07:57 AM
#5
Re: Edit a text file
If you're processing line-by-line, then use the Replace function to change the line.
Code:
Dim hFile as Integer
Dim sFilePath as String
Dim sData as String
. . .
hFile = FreeFile()
Open sFilePath For Input As #hFile
Do While Not EOF( hFile )
Line Input #hFile, sData
If ( Instr( sData, "abc" ) > 0 ) Then
sData = Replace( sData, "abc", "xyz" )
End If
Loop
Close #hFile
Regards, Phill W.
Last edited by Phill.W; Jan 29th, 2013 at 08:00 AM.
-
Jan 29th, 2013, 08:03 AM
#6
Re: Edit a text file
Try this too:
Code:
Call ReplaceText("C:\TextFile.txt", "FindThisText", "ReplaceWithThis")
Private Sub ReplaceText(ByRef sFileName As String, ByRef sFind As String, ByRef sReplace As String)
Dim strData As String
Open sFileName For Binary As #1
strData = Space$(LOF(1))
Get #1, , strData
Close #1
strData = Replace(strData, sFind, sReplace)
Open sFileName For Binary As #2
Put #2, , strData
Close #2
End Sub
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Jan 29th, 2013, 09:51 AM
#7
Thread Starter
Addicted Member
Re: Edit a text file
That's exactly what I needed !
Thanks Bonnie,
Regards, Kars
-
Jan 29th, 2013, 09:57 AM
#8
Re: Edit a text file
Don't forget to mark this thread Resolved!
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
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
|