Solved

READ TEXT FILE AND RETURN A RECORD VB

Posted on 2003-12-10
5
25,145 Views
Last Modified: 2012-05-04
VB - i wanna search some specific text in a text file and return the record contening the text,  exemple :

i wanna search for this string : ''Milford''

i my result should be : ''4834544834569054713386    9054713386TANI08810039Markham   ON   2038784667     04550839Milford   CTA00010370000056000000000000000000T070219L  000240120031205104238000106200010386DB''

'My text file ( total of 3 record )
'********************************************************************************
4834544834565197485837    5197485837TANI08570010KitchWaterON   5199728888     06520000Windsor   ONA00016580000090000000000000000000T070219L  009711020031205104057000120400016626DA

4834544834569054713386    9054713386TANI08810039Markham   ON   2038784667     04550839Milford   CTA00010370000056000000000000000000T070219L  000240120031205104238000106200010386DB

4834544834569052829535    9052829535TANI08230003CooksvilleON41613374430       04500000Switzerlnd  K00001070000007000000000000000000T070219J  008735520031205104256000017800001086DC
'********************************************************************************

i try this code working fine but only where i write the '===' he don't return the good string !!!!

Private Sub cmdFile_Click()
    Dim strTemp As String

    txtfile = ""
    dlg.FileName = "*.txt"
    dlg.ShowOpen
    If Dir(dlg.FileName) <> "" Then
        Open dlg.FileName For Input As 1
        While Not EOF(1)
            Line Input #1, strTemp
            txtfile = txtfile & strTemp & vbCrLf
        Wend
        Close #1
    Else
        MsgBox "File not found"
    End If

    Call FindFirst("PQX01839780010732")
   
End Sub

Function FindFirst(stringLookingfor As String)

    Set Fso = CreateObject("Scripting.FileSystemObject")
    Set fileFso = Fso.CreateTextFile("C:\Documents and Settings\dlaplante\Bureau\temp.log")
           
    If stringLookingfor <> "" Then
        If InStr(txtfile, stringLookingfor) <> 0 Then
           
            'Don't return the good string !!!
            '==============================================
            txtfile.SelStart = InStr(txtfile, stringLookingfor) - 1
            txtfile.SelLength = Len(stringLookingfor)
            '==============================================        
            fileFso.WriteLine (txtfile)
            fileFso.WriteLine ("")
            fileFso.Close
        Else
            MsgBox "Text Not found"
        End If
    End If
End Function
0
Comment
Question by:dany651
5 Comments
 
LVL 14

Accepted Solution

by:
aelatik earned 125 total points
Comment Utility
Function SearchString(Keyword As String, Filename As String) As String
    SearchString = "Empty"
    Dim MyLine As Variant
    Open Filename For Input As #1
        While Not EOF(1)
            Line Input #1, MyLine
                If InStr(1, LCase(MyLine), LCase(Keyword), vbTextCompare) Then
                    SearchString = MyLine
                End If
        Wend
    Close #1
End Function

Private Sub Form_Load()
    MsgBox SearchString("milford", "c:\install.log")
End Sub
0
 

Expert Comment

by:enari
Comment Utility
First:
  fileFSO.WriteLine(textfile) will write the whole file, you need
  *** fileFso.WriteLine (txtfile.SelText) ***

Secondly:
The selection items are wrong for selStart and SelLength.
SelStart starts at the location of Milford (at the end of the line)
SelLen = the end of the line.
This code should only return the word "Milford"

You want the whole line Right?

because you put a VBCRLF at the end of the lines, we can search for that:

I would rewrite the function to look like below:

Function FindFirst(stringLookingfor As String)
    Dim i As Integer 'Instr PlaceHolder
    Set Fso = CreateObject("Scripting.FileSystemObject")
    Set fileFso = Fso.CreateTextFile("C:\temp.log")
           
    If stringLookingfor <> "" Then

        i = InStr(txtfile, stringLookingfor) 'Only do this instr once for simplicity
        If i <> 0 Then
           
            'Don't return the good string !!!
            '==============================================
            txtfile.SelStart = InStrRev(txtfile, vbCrLf, i)
            txtfile.SelLength = InStr(i, txtfile, vbCrLf) - txtfile.SelStart
            '==============================================
            fileFso.WriteLine (txtfile.SelText)
            fileFso.WriteLine ("")
            fileFso.Close
        Else
            MsgBox "Text Not found"
        End If
    End If
End Function
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
aelatik's code should work fine except that the function continues reading the file even after a match is read.  It needs to close the file and exit as soon as the record is found.

Function SearchString(Keyword As String, Filename As String) As String
    SearchString = "Empty"
    Dim MyLine As Variant
    Open Filename For Input As #1
        While Not EOF(1)
            Line Input #1, MyLine
                If InStr(1, LCase(MyLine), LCase(Keyword), vbTextCompare) Then
                    SearchString = MyLine
                    Close #1
                    Exit Function
                End If
        Wend
    Close #1
End Function

Idle_Mind
0
 
LVL 4

Expert Comment

by:MajinLoki
Comment Utility
An even simpler answer would be to use regular expressions.

Imports regex = System.Web.RegularExpressions

Dim rex As regex.TextRegex                                                     'this will find one (the first) match
Dim rexColl As System.Text.RegularExpressions.MatchCollection  'this can be used to find all instances inside the string

rex = New regex.TextRegex()

if rex.isMatch("Milford") then
    'do whatever
end if

This will be the easiest solution.  Regular expressions also have their own syntax for the strings, so I suggest you look them up in in a search engine.  Hope this helps.
0
 

Author Comment

by:dany651
Comment Utility
cool i get the answer from aelatik cause i need to make a loop for get all record don't wanna exit when i have foudn a record i wanna continu !!!
the only thing is that all my text files are in a zip file !!!!

if i use a text file the code working fine buit if my text file are in a zipped doesn't work !!!

how he can work wit h a text file in a zip file ???
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 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…

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

12 Experts available now in Live!

Get 1:1 Help Now