Solved

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

Posted on 2012-03-10
6
257 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 46

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 46

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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 46

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 46

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 code to programmatically convert pdf to excel 21 90
Cannot enter table caption text in Word protected document 6 124
Problem to skip loop 6 57
message box in access 4 46
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 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…

770 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