-
September 20th, 2007, 07:34 AM
#1
replace string in the file
i want to make a vb application, in which it can read one file named "service.config", when user hit the submit button, it will read this file and if there is string "c:\dev" in that file, it should replace that string to "c:\program".
Please let me know, it will be great help for me.
thanks
abhi
-
September 20th, 2007, 09:08 AM
#2
Re: replace string in the file
Here's a simple way to do it.
Code:
Private Sub Command1_Click()
Open App.Path & "\test.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, tmpString
theString = theString + tmpString + vbNewLine
Loop
Close #1
theString = Replace(theString, "c:\dev", "c:\program")
Open App.Path & "\test2.txt" For Output As #1
Print #1, theString
Close #1
End Sub
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
September 20th, 2007, 09:16 AM
#3
Re: replace string in the file
You were too fast for me, peejavery.
But since I have done it now, I post it.
Also this version replaces ALL occurrences of c:\dev. If thats not what you want, abhit, peejavery's Replace is the one to use.
This here uses FSO
Code:
Private Sub ReplaceInFile(FileName As String, sFind As String, sRepl As String)
Dim FSO As New FileSystemObject
Dim ts As TextStream
Dim a$
Set ts = FSO.OpenTextFile(FileName, ForReading)
If Not ts Is Nothing Then
a = ts.ReadAll
ts.Close
'this replaces all occurrences of sFind with sRepl regardless if it is part of a longer path
While InStr(1, a, sFind, vbTextCompare): a = Replace(a, sFind, sRepl, , , vbTextCompare): Wend
Set ts = FSO.OpenTextFile(FileName, ForWriting)
ts.Write a
ts.Close
End If
End Sub
Private Sub Command1_Click()
ReplaceInFile App.Path + "\test.txt", "c:\dev", "c:\program"
End Sub
I just cant wait for your "skin the cat" version, dglienna.
I forgot: this version is NOT case sensitive, due to the vbTextCompare options.
-
September 20th, 2007, 09:42 AM
#4
Re: replace string in the file
Originally Posted by WoF
Also this version replaces ALL occurrences of c:\dev.
So does mine, but yours is more professional.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
September 20th, 2007, 10:19 AM
#5
Re: replace string in the file
Yes, you are right. The while loop around the Replace statement is not necessary.
(Dear me )
-
September 20th, 2007, 10:33 AM
#6
Re: replace string in the file
We all have those moments!
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
September 20th, 2007, 11:53 PM
#7
Re: replace string in the file
Originally Posted by PeejAvery
We all have those moments!
thanks WoF and PeejAvery..you both are great..thanks alot...
-
September 21st, 2007, 06:27 AM
#8
Re: replace string in the file
Glad we could help. Good luck!
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
September 21st, 2007, 07:43 AM
#9
Re: replace string in the file
forgot about this one. hasn't be up in a while...
Code:
Option Explicit
' Add a reference to Riched20.dll. Browse for it!
' This will select TOM. You only need to do this once!
'
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_USER = &H400&
Private Const EM_GETOLEINTERFACE = (WM_USER + 60)
Dim tomDoc As ITextDocument
Private Enum Direction
Forward = 1
Backward = -1
End Enum
Private Sub Command1_Click()
RTFindText Text1.Text, Forward
End Sub
Private Sub Command2_Click()
RTFindText Text1.Text, Backward
End Sub
' All the hard work done here
Private Sub RTFindText(strText As String, Optional RequiredDirection As Direction = Forward)
Dim flags As Long
flags = 2 * Check1 + 4 * Check2 ' + 8 * Check3 sadly, RegExp functionality is not available on a RichTextBox
tomDoc.Selection.FindText Text1.Text, RequiredDirection * tomDoc.Selection.StoryLength, flags
End Sub
Private Sub Form_Load()
Dim myIUnknown As IUnknown ' Bet you didn't know that VB actually knows about IUnknown ...
' Get alternative TOM interface to RTB
SendMessage RichTextBox1.hwnd, EM_GETOLEINTERFACE, 0&, myIUnknown
Set tomDoc = myIUnknown ' We're doing a QueryInterface!
' Ok, set up our RTB with some text
RichTextBox1.Text = "hello and Hello and hEllo or hello with another hello just for luck saying hello there"
RichTextBox1.HideSelection = False 'Always show highlight, whether RTB has focus or not
Text1.Text = "Hello" ' Default word to search for
Command1.Caption = "Find Next"
Command2.Caption = "Find Previous"
Check1.Caption = "Whole word"
Check2.Caption = "Case"
' Check3.Caption = "RegExp" 'Sadly RegExp functionality is not available on a RichTextBox
End Sub
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
|