Solved

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

Posted on 2012-03-10
6
255 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 45

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 45

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

 
LVL 45

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 45

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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

20 Experts available now in Live!

Get 1:1 Help Now