Link to home
Start Free TrialLog in
Avatar of Kgenis
Kgenis

asked on

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.
Avatar of sirbounty
sirbounty
Flag of United States of America image

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
Avatar of Kgenis
Kgenis

ASKER

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
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
Post a sample textfile, makes it easy to understand :-)
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...
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
I mean:
If bFoundLetters Then Exit Do

NOT

If bFoundLetters Then Exit For
Avatar of Kgenis

ASKER

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
Avatar of Kgenis

ASKER

It appears I can't count ohwell you get the idea of the textfile from the comment above
Does my solution not work?
Avatar of Kgenis

ASKER

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.
Avatar of Kgenis

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of PhilAI
PhilAI
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And you will need to put

Call lstWords.Clear

Before running the code each time ;)
Avatar of Kgenis

ASKER

still displays the whole text file in listbox ;p. Great solution anyway
Then, each file is not separated by a vbCr, but a vbLf?
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
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
Avatar of Kgenis

ASKER

Ahh :). I have it working now thanks for the excellent solution phil and thanks, to all of you who have also contributed.
Avatar of Kgenis

ASKER

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.
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...
Avatar of Kgenis

ASKER

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.
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