Solved

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

Posted on 2012-03-10
6
258 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

831 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