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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for Text and extracting the line to a new text file 1

Status
Not open for further replies.

NickC111

Technical User
Sep 23, 2010
44
GB
I am using the following script to search for a word and where it finds that word it extracts the text on that particular line into a new text file.

[' Writing Data to a Text File
Const ForReading = 1
Dim words(1)
Dim msg
words(0) = "REV"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set inFile = objFSO.OpenTextFile("C:\Data.txt", ForReading)
Set outFile = objFSO.OpenTextFile("C:\Data-Results.txt", 8, True)
Do Until inFile.AtEndOfStream
strSearchString = inFile.ReadLine
For i = 0 To UBound(words)-1

If InStr(strSearchString,words(i)) Then

msg = msg&strSearchString & vbNewLine
End If

next

Loop


inFile.Close

outfile.WriteLine msg]

I am searching for REV but the problem I have found is that it finds REV in any name example like TREVOR

Is there a way to find the text REV when it stands alone in the text file

Thanks
 
Well, a word is normally surrounded by whitespace either side to try changing "REV" to " REV "


In order to understand recursion, you must first understand recursion.
 
The word is surrounded by whitespace I have tried your suggestions but still not working, what happens if the whitespace is a TAB
 
In that case you'ld need 2 search string " REV " and "<TAB>REV<TAB>"

and if you wanted to find REV enclosed by commas say you'ld need another search string ",REV," and so on and so on


In order to understand recursion, you must first understand recursion.
 
When I try these suggestions it finds nothing

Source file looks like this

9865865556 XXXX XXXXX 2005 1 1,372.00 AN 0 REV 0
9865844556 TREVOR XXXXX 2012 1 792.00 GS 0

I want to extract the 1st line as it has the REV but it is extracting the 2nd line as REV is in TREVOR

Thanks
 
When I run your code I get both lines in output file

When I change the line

words(0) = "REV"

to

words(0) = " REV "

and run the code , only the second line appears in the output file.


In order to understand recursion, you must first understand recursion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top