Solved

How to verify that a word exist on a .txt file using Visual Basic v6

Posted on 2012-03-10
6
259 Views
Last Modified: 2012-06-27
I need a code which will open a .txt file and search for a certain word and if word exist to get a msgbox.

I need something like,

Open "test.txt" for input as #1
Search for word 'example'
msgbox if word exist
do nothing if not exist

Thank you!
0
Comment
Question by:SDfatha
  • 4
  • 2
6 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 37705113
Dim strBuffer As String
Open "c:\MyPath\Test.txt" For Input As #1
strBuffer = Input(LOF(1), 1)
If Instr(1, strBuffer, "example") Then
     MsgBox "It's there'
End If
Close #1
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 37705124
Here's better code with comments.


Dim strBuffer As String
' Define an Integer to hold the file number
Dim FF As Integer

' Find a file number that's not in use
FF = FreeFile

Open "c:\temp\Test.txt" For Input As #FF

' The format of 'Input' is Input(how-many-bytes, file-number) and
' LOF is Length Of File so how-many-bytes is set at length of the file
strBuffer = Input(LOF(FF), FF)
If InStr(1, strBuffer, "example") Then
     MsgBox "It's there'"
End If
Close #FF
0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 500 total points
ID: 37706117
If your file is a really big file (too big to reasonably read the whole thing in at one time) then simply replace the Input command with LineInput and put the logic in a loop.

Dim FF as Integer
FF = FreeFile
Open "c:\temp\Test.txt" For Input as #FF
do until EOF( FF )
    strBuffer = LineInput #FF
    If instr(1, strBuffer, "example") then
        MsgBox "It's there"
    End if
Loop
Close #FF

Each LineInput command will read the file until either a Carriage Return or Carriage Return-Line Feed pair is encountered.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:Martin Liss
ID: 37706191
Wouldn't doing InStr on a large file line by line be pretty slow.

As an alternative the file could be read in large chunks using Input.
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 37706275
But if you read it in chunks, a chunk might end in the middle of the text you are looking for.  By reading a line at a time, you are assured that each 'chunk' ends on a Carriage Return.

Besides, if your computer has good disk reading buffers, there should be a huge performance difference.

Either InStr searches something like 100 byte buffers 10,000 times, or it searches 10,000 byte buffers 100 times.  Either way InStr has to process 1,000,000 of memory.  Yes, there will be some performance boost because InStr is called fewer times with larger memory chunks (and the same can be said about disk reads).  But adding a few thousand function calls should affect over all speed greatly, so long as the disk reading logic has good buffering (because I/O is going to be your bottle neck, not multiple function calls).
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 37706951
But if you read it in chunks, a chunk might end in the middle of the text you are looking for.

Good point.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

733 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