Adapting Function StringExistsInFile by killer42

Collapse
X
 
  • Time
  • Show
Clear All
new posts

  • mustakbal
    replied
    Thanks vb5prgrmr for your help. I figured out the combination through trial and error.
    It's ironic how, sometimes, the solution turns out to be very simple after believing that you are dealing with a major problem. All I had to do was turn the "If Buffer..."
    into "If Like ..." statement and it worked fine for what I need. Thanks again.

    Leave a comment:


  • vb5prgrmr
    replied
    Yes,... you will have to test for the wildcard characters and if found call the appropriate code, and if not, skip it...



    Good Luck

    Leave a comment:


  • mustakbal
    replied
    Sorry, but I don't have the vb6 help installed! I discovered that I was having problems locating a complete expression such as "To be able to" and displaying it in a label; so I switched to another code by Killer42 that checked if line exist in a file ;and it works fine for non-case sensitive searches. Here is how I incorporated it:

    Private Sub lblSearch_Click ()
    Dim filename As String, thestring As String
    Dim Fnum As Long, Buffer As String, LineNo As Long
    Fnum = FreeFile ' Get next unused file number.
    filename = ("C:\.....TheFi le.txt")
    thestring = TxtSearch.Text

    Open filename For Input Access Read Shared As #Fnum
    Do Until EOF(Fnum)
    ' Retrieve one line of text from the file a string.
    Line Input #Fnum, Buffer
    LineNo = LineNo + 1

    If LCase$(Buffer$) = LCase$(thestrin g$) Then

    index1 = LineNo

    Lbltext1.Captio n = line_value1(ind ex1 - 1)
    Lbltext2.Captio n = line_value2(ind ex1 - 1)
    Lbltext3.Captio n = line_value3(ind ex1 - 1)
    Lbltext4.Captio n = line_value4(ind ex1 - 1)

    Debug.Print "Found string at line: "; Format(LineNo)

    End If
    Loop
    Close Fnum

    End Sub
    Then I found this code that uses wild cards in the search and I am wondering if I could use some of it to make the code above cover wild cards. Hers is the code:

    Function InStrWild(Sourc e As String, FindMe As String, _
    Optional StartAt As Long = 1) As Long
    Dim X As Long
    Dim SearchMe As String
    SearchMe = Mid$(Source, StartAt)
    If Source Like "*" & FindMe & "*" Then
    For X = 1 To Len(SearchMe)
    If Mid$(SearchMe, X) Like FindMe & "*" Then
    InStrWild = X + StartAt - 1

    Exit For
    End If
    Next
    End If
    End Function
    So, the question is: Is it doable or am I on the wrong track? Your advice would surely save me a lot of time and trouble.

    Leave a comment:


  • vb5prgrmr
    replied
    Regular expressions? Go ahead...

    Leave a comment:


  • mustakbal
    replied
    The only scenarios that might come up in my situation are the case sensitive and the spaces issues -- there are no punctuation marks at all. The file to be searched is made up of one word or one short expressions on each line. So I need to be able to find "Adjusment" if I type "adjust" or "adjust*" and to find "To adopt" by typing "adopt" or "*adopt". The code as it stands right now does not do that, although I understand from your reply that it should have some of that capability. I will explore the Option Compare Text but I do not have too much confidence in my VB capabilities as of yet.

    I have been struggling with another function I found that is supposed to use wild cards but have not succeeded with that either.. I know I am doing something wrong there also. I would post that if you wouldn't mind taking a look and finding what I am doing wrong.

    Thanks in advance.

    Leave a comment:


  • vb5prgrmr
    replied
    Okay, say you have this string "This is a test." and you are searching for "est". Instr will find it so no need for use of a wildcard character at this point.

    Now, to search for whole words you would have searched for " est " and then instr would not have found it but if you were wanting to search for "*est " then you would search for "est ". But once again, instr would not find this because of punctuation (the period).

    So, you will first have to scour the string to be searched of all punctuation via the replace function, and I would suggest that you remove the .?!,#$% and replace them with spaces so that "est " would be found.

    On the index tab of help, enter option compare and press enter to read all about it. Then in the general declarations section (where Option Explicit is) it should look like this...
    Code:
    Option Explicit
    Option Compare Text


    Good Luck

    Leave a comment:


  • mustakbal
    replied
    Thanks for the quick response vb5prgrmr.Actua lly I tried to add "vbTextComp are" after TheString "InStr(1, S, TheString, vbTextCompare)" and kept getting type mismatch error, so I did it with the LCase$ and it worked ok. As far as the second part of your response, I only need the asterik to be at the beginning or the end of the word I am looking for, and not in the middle of it. If you meant I should be good with that, I would appreciate a liitle more guidance (fit for a newbie) on how to fix it with the Option Compare Text.

    Leave a comment:


  • vb5prgrmr
    replied
    To begin with, you do not need to add UCase or LCase to search a string with instr as its last optional argument allows you to specify vbTextCompare, which means it is a non case sensitive search. Also, you should check out the key phrase Option Compare Text.

    As for wild cards I see no logic in the above code to tell if you have selected the whole word or not so unless the wildcard needs to be between alpha numeric characters your good. BUT, if you really want to get fancy check out Regular Expressions.



    Good Luck

    Leave a comment:


  • Adapting Function StringExistsInFile by killer42

    I am using the code below that was posted by killer42 in order to search for words in a textfile. I added the LCase$ to (S) and (TheString) so case would be ignored. I was wondering if the code could, just as easily, be adapted to accept a wildcard (*) at the beginning or end of the string to search for; in other words to be able to look for "*djust " and get "adjust" and look for "adjust*" and get "adjustment " as one answer. Any help would be appreciated.

    Code:
    [Public Function StringExistsInFile(ByVal TheString As String, ByVal TheFile As String) As Boolean 
      Dim L As Long, S As String, FileNum As Integer 
      FileNum = FreeFile 
      Open TheFile For Binary Access Read Shared As #FileNum 
      L = LOF(FileNum) 
      S = Space$(L) 
      Get #1, , S 
      Close #FileNum 
      If InStr(1, S, TheString) Then 
        StringExistsInFile = True 
      End If 
    End Function
Working...