• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

vb.net - check record from a text file

hello there,
I am trying to use this code to search for a record in a text file but its not really working on vb.net like it used to on vb6..
what can I do to make it work ?
Function FileExists(ByVal FileName As String) As Boolean
        FileExists = (Dir(FileName, vbSystem + vbHidden) <> "")
    End Function

    Function Search_inFile(ByVal sFind As String, ByVal strFileName As String) As Boolean
        Dim sFile As String, i As Long
        i = FreeFile
        If FileExists(App.Path & "\Settings\" & strFileName) Then

        Open App.Path & "\Settings\" & strFileName For Binary As #i
            sFile = Space$(LOF(i))
        Get #i, , sFile

            If InStr(sFile, sFind) <> 0 Then
                Search_inFile = True
            End If
        Close #i
        End If
    End Function

Open in new window

0
XK8ER
Asked:
XK8ER
  • 2
  • 2
  • 2
  • +3
1 Solution
 
Trideep PatelCommented:
try this

Function FileExists(ByVal FileName As String) As Boolean
    FileExists = (Dir(FileName, FileAttribute.System + FileAttribute.Hidden) <> "")
  End Function

  Function Search_inFile(ByVal sFind As String, ByVal strFileName As String) As Boolean
    Dim sFile As String
    Dim i As Integer
    i = FreeFile()
    If FileExists(My.Application.Info.DirectoryPath & "\Settings\" & strFileName) Then

      FileOpen(i, My.Application.Info.DirectoryPath & "\Settings\" & strFileName, OpenMode.Binary)
      sFile = Space(LOF(i))

      FileGet(i, sFile)

      If sFile.Contains(sFind) Then
        Search_inFile = True
      End If
      FileClose(i)
    End If
  End Function

Open in new window

0
 
Kamaraj SubramanianApplication Support AnalystCommented:
check this

http://www.experts-exchange.com/Programming/Misc/Q_21678855.html

    Private Function IsFileContainsSrting(ByVal SearchWord As String, ByVal FileName As String) As Boolean
        Dim IsWordFound As Boolean = False
        Dim FileContents As String = String.Empty
        Dim FileStreamReader As StreamReader
        Try
            FileStreamReader = New StreamReader(FileName)
            FileContents = FileStreamReader.ReadToEnd()
        Catch ex As Exception
            MsgBox(ex.ToString)
            IsWordFound = False
        Finally
            If Not (FileStreamReader Is Nothing) Then
                FileStreamReader.Close()
            End If
        End Try
        If FileContents.IndexOf(SearchWord) > 0 Then
            'IndexOf returns -1 of not found and 0 if the textVal is empty
            IsWordFound = True
        End If
        Return IsWordFound
    End Function

Open in new window

0
Technology Partners: 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!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
How about?...
    Private Function Search_inFile(ByVal sFind As String, ByVal strFileName As String) As Boolean
        Dim result As Boolean = False
        Dim FileName As String = System.IO.Path.Combine(Application.StartupPath, "Settings\" & strFileName)
        If System.IO.File.Exists(FileName) Then
            result = My.Computer.FileSystem.ReadAllText(FileName).Contains(sFind)
        End If
        Return result
    End Function

Open in new window

0
 
XK8ERAuthor Commented:
which code will read the data faster?
0
 
Kamaraj SubramanianApplication Support AnalystCommented:
why dont you put a time before and after the operation and check.. it out :)
0
 
planoczCommented:
Use RegExpressions to find a word or words in a file. A lot faster.
0
 
XK8ERAuthor Commented:
planocz, that sounds wonderful.. how can I accomplish this with RegExpressions?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If your searching for the existence of a simple string and the file is relatively small then you're not going to see a huge difference in performance with a RegEx.

The biggest bottleneck is usually the reading of the file into a string...and a RegEx still requires an entire string to parse (and thus suffers the same largest bottleneck).

How big is the file?...approx how many lines?

If you need to search for something more complex, like a multiline type structure, or the conditions become "dynamic" (find xxx before yyy only if zzz, etfc) then a RegEx approach will definitely be the way to go.
0
 
planoczCommented:
Here is a simple RegExpress that finds whatever sFind is (You have to Have the same spelling in sFind in what you are lokking for (i.e. this includes upper and lower case)
Imports System.Text.RegularExpressions
'Use code
Words("Findme")   '<---- sample text
  'USE this to find a word or words.
    Private Sub Words(ByVal sFind As String)
        Dim patternAll As String = "\b(?:" & sFind & ")\b"
        Dim regexParser As Regex = New Regex(patternAll, RegexOptions.Multiline)

        For Each matchCurrent As Match In regexParser.Matches(RichTextBox1.Text)
            Dim valueString As String = matchCurrent.Value.ToString
            MessageBox.Show(valueString)
        Next matchCurrent
    End Sub

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now