Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for keywords in string

Status
Not open for further replies.

Joliet

IS-IT--Management
Nov 7, 2002
7
SE
Hi
Can anybody please give me an example of howto search for a keyword in a textstring? The keyword is read in to the program as an commandline parameter.

Thanks
/JJ
 

If InStr(1, Command$, "Lookfor") Then MsgBox "Are you looking for me Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Hi again,

Thanks Sunaj, that works but when i try to exchange "If InStr(1, Command$, "Lookfor")" with "If InStr(1, Command$, args)" where args is "Set args = WScript.Arguments" taken from the commandline i get an error.
Any idea why?

Thanks
/JJ
 
Let se you code... Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Ok Sunaj,
The meaning with it is that if keyword is found that line should be removed from the file.
Here goes:

Dim objFileSystem, objInputFile
Dim strInputFile, inputData, strData
Dim args

strOutputFile = "infile.txt"

Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objInputFile = objFileSystem.OpenTextFile(strOutputFile, 1)

inputData = Split(objInputFile.ReadAll, vbNewline)

Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(strOutputFile, 2, True)
Set args = WScript.Arguments

For each strData In inputData
If Len(strData) = 0 Then WScript.Quit(0)
If InStr(strData, args) = 0 Then ts.Writeline strData Else
Next

ts.Close

objInputFile.Close
Set objFileSystem = Nothing

WScript.Quit(0)

Thanks
/JJ
 
Hi Agian,
You haven't included object creation and explicit declaretion for WScript - which I'm not sure what is. I would recommend you to use explict declarations for all you variables.

But what about:

-----------------------------------
Sub main
Dim f As Byte, a() As String
f = FreeFile
Open "c:\myfile.txt" For Input As #f
a = Split(Input(LOF(f), #f), vbCrLf)
Close #f
f = FreeFile
Open "c:\myfile.txt" For Output As #f
For i = 0 To UBound(a)
If InStr(1, a(i), Command$) = 0 Then Print #f, a(0)
Next i
Close #f
end sub
-----------------------------------
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top