Results 1 to 4 of 4

Thread: Searching text files for a string

  1. #1
    Guest

    Post

    Is there a function in VB to search a text file for a specified string?

    I've tried to input from the text file into a variable then use InStr to compare the two before looping and testing the next input - but it doesn't work...

    If no-one has any ideas I can send this code and see if anyone knows what's wrong with it...

    Thanks

  2. #2
    Guest

    Post

    Use this code to read in the text file...
    Code:
    Dim sTextFile as string
    On Error Resume Next
    Open sTextFile For Input As #1
    OpenFile = Input(LOF(1), 1)
    Close #1
    Then use InStr$ to find your text in "sTextFile".

    ------------------
    Matthew Ralston
    E-Mail: [email protected]
    ICQ:31422892
    Web Sites:The Blue Link My Home Page

  3. #3
    Guest

    Post

    This is similar to the code I have used - but the problem with it now is that each time I call Input# it inputs only one line (or something) but I want it to input the entire contents of the file (I know they aren't massive) because I need to search multiple files at a time in a loop...

    Also could you explain this line of code:

    OpenFile = Input(LOF(1), 1)

    Thanks for the help

  4. #4
    Guest

    Post

    That code should load the entire text file into "sTextFile". I've got it in a module that I use regularly...
    Code:
    Option Explicit
    
    
    Function OpenFile(Filename As String) As String
        On Error Resume Next
        Open Filename For Input As #1
        OpenFile = Input(LOF(1), 1)
        Close #1
    End Function
    
    
    Sub SaveFile(Filename As String, Text As String)
        On Error Resume Next
        Open Filename For Output As #1
        Print #1, Text
        Close #1
    End Sub
    ------------------
    Matthew Ralston
    E-Mail: [email protected]
    ICQ:31422892
    Web Sites:The Blue Link My Home Page (Not up at the moment!)


    [This message has been edited by matthewralston (edited 01-06-2000).]

    [This message has been edited by matthewralston (edited 01-06-2000).]

    [This message has been edited by matthewralston (edited 01-06-2000).]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width