Solved

Scanning a textfile and checking the lines for certain letters

Posted on 2004-04-26
23
282 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:Kgenis
  • 11
  • 9
  • 2
  • +1
23 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 10920335
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
 

Author Comment

by:Kgenis
ID: 10920354
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
 
LVL 6

Expert Comment

by:PhilAI
ID: 10920384
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 14

Expert Comment

by:aelatik
ID: 10920400
Post a sample textfile, makes it easy to understand :-)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10920403
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
 
LVL 6

Expert Comment

by:PhilAI
ID: 10920451
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
 
LVL 6

Expert Comment

by:PhilAI
ID: 10920463
I mean:
If bFoundLetters Then Exit Do

NOT

If bFoundLetters Then Exit For
0
 

Author Comment

by:Kgenis
ID: 10920951
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
 

Author Comment

by:Kgenis
ID: 10920957
It appears I can't count ohwell you get the idea of the textfile from the comment above
0
 
LVL 6

Expert Comment

by:PhilAI
ID: 10921028
Does my solution not work?
0
 

Author Comment

by:Kgenis
ID: 10921030
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
 

Author Comment

by:Kgenis
ID: 10921105
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
 
LVL 6

Accepted Solution

by:
PhilAI earned 50 total points
ID: 10921136
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
 
LVL 6

Expert Comment

by:PhilAI
ID: 10921142
And you will need to put

Call lstWords.Clear

Before running the code each time ;)
0
 

Author Comment

by:Kgenis
ID: 10921174
still displays the whole text file in listbox ;p. Great solution anyway
0
 
LVL 6

Expert Comment

by:PhilAI
ID: 10921240
Then, each file is not separated by a vbCr, but a vbLf?
0
 
LVL 6

Expert Comment

by:PhilAI
ID: 10921262
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
 
LVL 6

Expert Comment

by:PhilAI
ID: 10921279
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
 

Author Comment

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

Author Comment

by:Kgenis
ID: 11025844
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
 
LVL 6

Expert Comment

by:PhilAI
ID: 11026939
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
 

Author Comment

by:Kgenis
ID: 11026962
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
 
LVL 6

Expert Comment

by:PhilAI
ID: 11027071
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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question