Solved

Scanning a textfile and checking the lines for certain letters

Posted on 2004-04-26
23
279 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 14

Expert Comment

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

Expert Comment

by:sirbounty
Comment Utility
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
Comment Utility
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
Comment Utility
I mean:
If bFoundLetters Then Exit Do

NOT

If bFoundLetters Then Exit For
0
 

Author Comment

by:Kgenis
Comment Utility
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
Comment Utility
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
Comment Utility
Does my solution not work?
0
 

Author Comment

by:Kgenis
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Kgenis
Comment Utility
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
Comment Utility
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
Comment Utility
And you will need to put

Call lstWords.Clear

Before running the code each time ;)
0
 

Author Comment

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

Expert Comment

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

Expert Comment

by:PhilAI
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now