Results 1 to 11 of 11

Thread: Searching .dat files

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    10

    Searching .dat files

    I was wondering if it was possible in VB6 to search a folder full of .dat files for a specific word in a .dat file? For instance, would I be able to make a program that searches for the word 'wire' in all of the .dat files in a folder then return a list of the files that contain the word?

  2. #2
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Searching .dat files

    This is a pretty basic example and is probably not too efficient with large files. It also assumes that the files are text not binary.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.  
    5.     Dim find As String
    6.     Dim fldr As String
    7.    
    8.     fldr = "c:\somefolder\"
    9.     find = Dir$(fldr & "*.dat")
    10.    
    11.     Do While Len(find)
    12.         SearchFile fldr & find, "wire"
    13.         find = Dir$()
    14.     Loop
    15.        
    16.    
    17. End Sub
    18.  
    19. Public Sub SearchFile(ByVal fil As String, ByVal SearchFor As String)
    20.  
    21.     Dim dat As String
    22.     Dim ff As Integer
    23.    
    24.     ff = FreeFile()
    25.    
    26.     On Error Goto Probs
    27.     Open fil For Input As #ff
    28.     dat = Input(LOF(ff), ff)
    29.     Close #ff
    30.    
    31.     If InStr(1, dat, SearchFor, vbTextCompare) > 0 Then
    32.         MsgBox "Found it in file " & fil
    33.     End If
    34.  
    35.     On Error Goto 0
    36.     Exit Sub
    37.  
    38. Probs:
    39.     MsgBox "Errors occurred!"
    40.     On Error Goto 0
    41.  
    42. End Sub
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  3. #3
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Jar, Norway
    Posts
    372

    Re: Searching .dat files

    Here is somthing to start with:
    Last edited by Ember; Dec 6th, 2006 at 01:51 PM.

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Searching .dat files

    Quote Originally Posted by pnish
    This is a pretty basic example and is probably not too efficient with large files. It also assumes that the files are text not binary.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.  
    5.     Dim find As String
    6.     Dim fldr As String
    7.    
    8.     fldr = "c:\somefolder\"
    9.     find = Dir$(fldr & "*.dat")
    10.    
    11.     Do While Len(find)
    12.         SearchFile fldr & find, "wire"
    13.         find = Dir$()
    14.     Loop
    15.        
    16.    
    17. End Sub
    18.  
    19. Public Sub SearchFile(ByVal fil As String, ByVal SearchFor As String)
    20.  
    21.     Dim dat As String
    22.     Dim ff As Integer
    23.    
    24.     ff = FreeFile()
    25.    
    26.     On Error Goto Probs
    27.     Open fil For Input As #ff
    28.     dat = Input(LOF(ff), ff)
    29.     Close #ff
    30.    
    31.     If InStr(1, dat, SearchFor, vbTextCompare) > 0 Then
    32.         MsgBox "Found it in file " & fil
    33.     End If
    34.  
    35.     On Error Goto 0
    36.     Exit Sub
    37.  
    38. Probs:
    39.     MsgBox "Errors occurred!"
    40.     On Error Goto 0
    41.  
    42. End Sub
    Ok, that code worked prefectly... at first. I decided to use .doc files (Microsoft Word) instead of .dat files. I took my test .dat files and just changed the extention to .doc and it still worked, it still found the word 'wire' in the .doc file. When I create a new .doc file and save it the program won't find it and it will go to the 'probs' part of the code.

    Any ideas on how to fix that?

  5. #5
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Jar, Norway
    Posts
    372

    Re: Searching .dat files

    Quote Originally Posted by Radont
    When I create a new .doc file and save it the program won't find it and it will go to the 'probs' part of the code.

    Any ideas on how to fix that?
    What is 'it will go to the 'probs' part of the code'? (My english is bad) Morten

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Searching .dat files

    Quote Originally Posted by Ember
    What is 'it will go to the 'probs' part of the code'? (My english is bad) Morten
    It displays the "Error's Occurred" messagebox.

  7. #7
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Jar, Norway
    Posts
    372

    Re: Searching .dat files

    I must have zipped you the norwegian version. Sorry!
    Attached Files Attached Files

  8. #8
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Jar, Norway
    Posts
    372

    Re: Searching .dat files

    Did I almost promise you more stuff to search files and their content on the PC/Net? This is an App I made for a factory that needed to lookup earlier test results saved in ".ATR"-files. Things that has to be changed are marked 'To be changed'.
    Have fun!
    Morten
    Attached Files Attached Files
    Last edited by Ember; Dec 8th, 2006 at 06:27 PM.

  9. #9
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Searching .dat files

    Quote Originally Posted by Radont
    When I create a new .doc file and save it the program won't find it and it will go to the 'probs' part of the code.
    As I mentioned, the code I provided assumed you were working with text files. Word files are not pure text, they contain all sorts of formatting commands etc that can play havoc with that code. What was the error that occurred? Modify the error code like this and post the error...
    VB Code:
    1. Probs:
    2.     MsgBox "The following error occurred:" & vbCrLf & Error$
    3.     On Error Goto 0
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Searching .dat files

    Sorry it took so long for me to reply to this thread, I forgot to take the program home with me from work so I wasn't able to try anything over the weekend.

    At any rate, the error was "Input Past End of File."

    Also, that is a pretty slick program Ember. I'll be able to learn some stuff from it.

  11. #11
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Jar, Norway
    Posts
    372

    Re: Searching .dat files

    Quote Originally Posted by Radont
    At any rate, the error was "Input Past End of File."

    Also, that is a pretty slick program Ember. I'll be able to learn some stuff from it.
    Sorry, it's years ago I looked at these routines. I'm not working there any longer. Anyway, be my guest.

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