Scanning a textfile and checking the lines for certain letters

Hey All, as part of an app I'm working on I need it to scan a whole text file and report back to me the words (theres one word on each line) which it finds which include the letters:
A R V I E T, at somepoint in the line. Any help will me most appreciated.

Thanks
Matt.
KgenisAsked:
Who is Participating?
 
PhilAIConnect With a Mentor Commented:
JUST ADD A LISTBOX CALLED lstWords

Dim C As Long
Dim L As Byte
Dim bFoundLetters As Boolean
Dim iFile As Integer
Dim sLine As String
Dim vLetters

vLetters = Array("A", "R", "V", "I", "E", "T")

iFile = FreeFile
Open "C:\myFile.txt" For Input As #iFile
    Do While Not EOF(iFile)
        C = C + 1
        Line Input #iFile, sLine
        bFoundLetters = True
        For L = LBound(vLetters) To UBound(vLetters)
            If InStr(sLine, vLetters(L)) = 0 Then
                bFoundLetters = False
                Exit For
            End If
        Next L
        If bFoundLetters Then Call lstWords.AddItem(sLine)
    Loop
Close #iFile
0
 
sirbountyCommented:
Should do it (roughly)

open ("C:\myfile.txt") for input as #1
do while not eof(1)
   line input #1, strData
   if strData="A R V I E T" then msgbox "Found it"
loop
close #1
0
 
KgenisAuthor Commented:
hrmm Not to be rude or anything but, are you sure this searches the text file for A R V I E T in any order because the words will be like Sampleone etc. Thanks very much for the speedy reply
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
PhilAICommented:
Dim sLine as String

Open "C:\myfile.txt" For Input As #1
    Line Input #1, sLine
    If sLine Like "*A*R*V*I*E*T*" Then Msgbox "Found IT!"
Close #1
0
 
aelatikCommented:
Post a sample textfile, makes it easy to understand :-)
0
 
sirbountyCommented:
Not at all..
Can you provide an example of your text?
I think I misread the post slightly - but want to make sure we're on the same track...
0
 
PhilAICommented:
In any order, sorry, try this instead:


Dim C As Long
Dim L As Byte
Dim bFoundLetters As Boolean
Dim iFile As Integer
Dim sLine As String
Dim vLetters

vLetters = Array("A", "R", "V", "I", "E", "T")

iFile = FreeFile
Open "C:\myFile.txt" For Input As #iFile
    Do While Not EOF(iFile)
        C = C + 1
        Line Input #iFile, sLine
        bFoundLetters = True
        For L = LBound(vLetters) To UBound(vLetters)
            If InStr(sLine, vLetters(L)) = 0 Then
                bFoundLetters = False
                Exit For
            End If
        Next L
        If bFoundLetters Then Exit For
    Loop
Close #iFile

If bFoundLetters Then MsgBox "Line " & C & ": " & sLine
0
 
PhilAICommented:
I mean:
If bFoundLetters Then Exit Do

NOT

If bFoundLetters Then Exit For
0
 
KgenisAuthor Commented:
a sample of the text file's first 10 lines:

aardvark
aardwolf
aaronic
aaronical
aaronite

its basically the whole english dictionary. the program itself when finished will be a generator for word quizzes i can't remember the english term for them *hits self with english book
0
 
KgenisAuthor Commented:
It appears I can't count ohwell you get the idea of the textfile from the comment above
0
 
PhilAICommented:
Does my solution not work?
0
 
KgenisAuthor Commented:
Thanks again for all your comments, I think I've found a solution at last. PhilAI's code works but returns the whole textfile not just the word It finds which has the letters a,r,v,i,e,t in it.
0
 
KgenisAuthor Commented:
Phil, if you could refine your code to show me the one or two etc words containing all 6 letters I'll be prepared to accept your solution.
0
 
PhilAICommented:
And you will need to put

Call lstWords.Clear

Before running the code each time ;)
0
 
KgenisAuthor Commented:
still displays the whole text file in listbox ;p. Great solution anyway
0
 
PhilAICommented:
Then, each file is not separated by a vbCr, but a vbLf?
0
 
PhilAICommented:
Dim L As Byte
Dim W As Long
Dim bFoundLetters As Boolean
Dim iFile As Integer
Dim sLine As String
Dim vLetters
Dim vWords

vLetters = Array("A", "R", "V", "I", "E", "T")

iFile = FreeFile
Open "C:\myFile.txt" For Input As #iFile
    Do While Not EOF(iFile)
        Line Input #iFile, sLine
        vWords = Split(sLine, vbLf)
        For W = LBound(vWords) To UBound(vWords)
            bFoundLetters = True
            For L = LBound(vLetters) To UBound(vLetters)
                If InStr(vWords(W), vLetters(L)) = 0 Then
                    bFoundLetters = False
                    Exit For
                End If
            Next L
            If bFoundLetters Then Call lstWords.AddItem(vWords(W))
        Next W
    Loop
Close #iFile
0
 
PhilAICommented:
I mean: each WORD is separated by a vbLf character not a vbCr character which the Line Input does not pick up as a new line

A new line is vbCrLf or vbCr
0
 
KgenisAuthor Commented:
Ahh :). I have it working now thanks for the excellent solution phil and thanks, to all of you who have also contributed.
0
 
KgenisAuthor Commented:
any ideas on how I could refine the code to make it show words only containing those letters?

so if it was the previous: a,r,v,i,e,t then the words containing only a,r,v,i,e,t would be shown.
0
 
PhilAICommented:
That is what it does... If I am understanding your request correctly...

Puts all the words that contain the letters a, r, v, i, e & t (in any order) into a listbox called 'lstWords'

Not sure I'm understanding your second request here...
0
 
KgenisAuthor Commented:
Ok. Here is my request in a little more detail.

When I click the button on my form to read the file and find the results to add them into a listbox. Sometimes, words which I dont wan't are there such as:
absorptively
administratively etc. These results have a,r,v,i,e,t in them but also have other letters which arn't in the array. These words with letters which arn't in the array I want it to discard.

Thanks Again.
Matt.
0
 
PhilAICommented:
Dim L As Byte
Dim W As Long
Dim bFoundLetters As Boolean
Dim iFile As Integer
Dim sLine As String
Dim vLetters
Dim vWords

' Array of letters to find (always A, R, V, I, E, T)
vLetters = Array("A", "R", "V", "I", "E", "T")

' Open the File with the Words in
iFile = FreeFile
Open "C:\myFile.txt" For Input As #iFile
    Do While Not EOF(iFile)
        Line Input #iFile, sLine
' Split the Words separated by LineFeeds into an Array
        vWords = Split(sLine, vbLf)
        For W = LBound(vWords) To UBound(vWords)
            bFoundLetters = True
            For L = LBound(vLetters) To UBound(vLetters)
' ***** ADD THIS CODE IN *****
                If Len(vWords(W)) <> UBound(vLetters) + 1 Then
                    bFoundLetters = False
                    Exit For
' N.B. You are replacing "If InStr(vWords(W), vLetters(L)) = 0 Then" to ElseIf
                ElseIf InStr(vWords(W), vLetters(L)) = 0 Then
' ***** ADD THIS CODE IN *****
                    bFoundLetters = False
                    Exit For
                End If
            Next L
        ' IF case when the For Loop thru the Words in the file matches only and all the letters
            If bFoundLetters Then Call lstWords.AddItem(vWords(W))
        Next W
    Loop
Close #iFile
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.