Anyone know the best way to search through a text file for a specific string?
Printable View
Anyone know the best way to search through a text file for a specific string?
Load the entire file into a String variable and use InStr()
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 :D ...
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
Thanks guys, you are the greatest.
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
Ahhhh BOLLOX!! :D geesh i threw that together in 5 mins cos i didnt want to face changing html in lotsa pages. I am now a convert to """ instead of chr$(34) cos it gets just too long when replacing lotsa html code
B·O·L·L·O·X
X·B·O·L·L·O
O·X·B·O·L·L
L·O·X·B·O·L
L·L·O·X·B·O
O·L·L·O·X·B
B·O·L·L·O·X
:D
So plenderj, once i determin a line is in a file, how do i delete that line or word?
Use can use Replace().
VB Code:
Text1.text=Replace(Expression As String,Find As String,Replace As String,1,-1,vbTextCompare )
heh like the avatar :)
Thx you :)