|
-
Oct 17th, 2001, 04:02 AM
#1
Thread Starter
Lively Member
Search Text File
Anyone know the best way to search through a text file for a specific string?
-
Oct 17th, 2001, 04:06 AM
#2
Retired VBF Adm1nistrator
Load the entire file into a String variable and use InStr()
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Oct 17th, 2001, 04:10 AM
#3
PowerPoster
Hi
here is a little parser that i jsut threw together so that i could change stuff on around 150 web pages for my site relaunch ...
You can modify as required
Regards
Stuart
VB Code:
'2 command buttons, 1 label and 1 filelist box
Dim MyPath As String
Dim FileLoop As Long
Dim MyString As String
Dim MyFile As String
Dim MyLen As Long
Dim MyFind As String
Dim MyReplace As String
Dim MyCopy As String
Dim MyLenCheck As Double
Dim MyCounter As Long
Private Sub Form_Load()
MyPath = "C:\My Documents\MyWeb"
File1.Path = MyPath
File1.Pattern = "*.htm"
FileLoop = File1.ListCount
MyCounter = 0
File1.ListIndex = MyCounter
End Sub
Private Sub Command1_Click()
File1.ListIndex = MyCounter
MyFile = MyPath & "\" & File1.List(MyCounter)
MyLen = FileLen(MyFile)
Open MyFile For Input As #1
MyString = Input(MyLen, #1)
Close #1
MyFind = "Whatever"
MyReplace = "Whats It"
MyCopy = Replace(MyString, MyFind, MyReplace)
MyLenCheck = (Len(MyCopy) - Len(MyString)) / (Len(MyReplace) - Len(MyFind))
Label1 = MyLenCheck
Command2.SetFocus
End Sub
Private Sub Command2_Click()
Open MyFile For Output As #1
Print #1, MyCopy
Close #1
MyCounter = MyCounter + 1
Command1.SetFocus
If MyCounter >= FileLoop Then Command2.Enabled = False
End Sub
-
Oct 17th, 2001, 04:19 AM
#4
Thread Starter
Lively Member
Thanks guys, you are the greatest.
-
Oct 17th, 2001, 04:24 AM
#5
Retired VBF Adm1nistrator
Another (aka better ) approach :
VB Code:
Private Sub Form_Load()
MsgBox "Is " & Chr(34) & "SET" & Chr(34) & " in c:\autoexec.bat : " & isStringInFile("SET", "c:\autoexec.bat")
End Sub
Private Function isStringInFile(strString As String, strFile As String) As Boolean
Open strFile For Input As #1
isStringInFile = InStr(Input(LOF(1), 1), strString) <> 0
Close #1
End Function
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Oct 17th, 2001, 04:28 AM
#6
-
Oct 17th, 2001, 08:30 AM
#7
Thread Starter
Lively Member
So plenderj, once i determin a line is in a file, how do i delete that line or word?
-
Nov 7th, 2001, 06:37 AM
#8
Retired VBF Adm1nistrator
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Nov 7th, 2001, 06:40 AM
#9
VB Code:
Text1.text=Replace(Expression As String,Find As String,Replace As String,1,-1,vbTextCompare )
-
Nov 7th, 2001, 06:43 AM
#10
Retired VBF Adm1nistrator
heh like the avatar
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Nov 7th, 2001, 06:44 AM
#11
Thx you
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
|