Results 1 to 15 of 15

Thread: Search Dir for text:

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Search Dir for text:

    Made this little thing so that you can search for a word in any text file in a certain path, and it will tell you if it contains the word in text1.text...kind of like a extended text search thingy
    VB Code:
    1. Option Explicit
    2. Dim ff As Integer
    3. Dim mypath As String
    4. Dim p As String
    5. Dim strbuff As String
    6.  
    7. Private Sub Command1_Click()
    8. mypath = "C:\"
    9.  p = Dir(mypath, vbDirectory)
    10.  Do Until p = ""
    11.         If InStr(p, ".txt") Then
    12.         ff = FreeFile
    13.             Open mypath & p For Binary As #ff
    14.                 strbuff = Input(LOF(ff), ff)
    15.                     If InStr(strbuff, text1.Text) Then
    16.                       List1.AddItem p
    17.                     End If
    18.         End If
    19.        
    20.         p = Dir
    21. Loop
    22. End Sub

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Search Dir for text:

    If you have :

    VB Code:
    1. p = Dir(mypath)

    instead of :

    VB Code:
    1. p = Dir(mypath, vbDirectory)

    the loop will be faster as it won't return the directories, only files


    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Search Dir for text:

    I kind of over did it, didn't I ?

    I wrote the following code as a class (because of the events)
    Check the attached project on how to use the class
    VB Code:
    1. Option Explicit
    2.  
    3. Public Event FindFilesCurrentDir(ByVal CurrentDirectory As String)
    4. Public Event FindFilesFound(ByVal Path As String, ByVal FileName As String, ByVal FirstSearchWordFound As String)
    5. Public Event FindFilesDone(ByRef ReturnList As Collection)
    6. Public Event FindFilesCancel(ByRef CancelFind As Boolean)
    7.  
    8. Public Sub FindStrInDir(ByRef ReturnList As Collection, ByVal Root As String, ByVal Recurse As Boolean, _
    9.                         ByVal FileTypes As String, ByVal StrFileSize As String, ByVal Attributes As VbFileAttribute, _
    10.                         ByVal Compare As VbCompareMethod, ParamArray StrToFind() As Variant)
    11.    
    12.     Dim LOSize As Long, HISize As Long, K As Long
    13.     Dim FTypes() As String, SFindCol As New Collection, SFind() As String
    14.    
    15.     FTypes = Split(FileTypes, ";") ' file types are delimited by ";", example "*.txt;*test*.dat;*.html;*blah blah*.htm"
    16.     FlattenParamArrayToString SFindCol, StrToFind
    17.    
    18.     If SFindCol.Count > 0 Then
    19.         ReDim SFind(SFindCol.Count - 1)
    20.        
    21.         For K = 0 To UBound(SFind)
    22.             SFind(K) = SFindCol(1)
    23.             SFindCol.Remove 1
    24.         Next K
    25.     Else
    26.         ReDim SFind(0)
    27.     End If
    28.    
    29.     If Attributes = 0 Then Attributes = vbDirectory + vbReadOnly + vbSystem + vbArchive + vbHidden
    30.     'If Compare = 0 Then Compare = vbBinaryCompare   '  vbBinaryCompare IS 0 ...
    31.    
    32.     ' Get Low and High boundries for the file size to find
    33.     If Len(StrFileSize) = 0 Then
    34.         LOSize = 0
    35.         HISize = 0
    36.     Else
    37.         K = InStr(1, StrFileSize, "-")
    38.        
    39.         If K > 0 Then
    40.             LOSize = Val(Left(StrFileSize, K - 1))
    41.             HISize = Val(Mid(StrFileSize, K + 1))
    42.         Else
    43.             LOSize = 0
    44.             HISize = Val(StrFileSize)
    45.         End If
    46.     End If
    47.    
    48.     ' replace "/" with "\"  (just in case)
    49.     If InStr(1, Root, "/") > 0 Then Root = Replace(Root, "/", "\")
    50.    
    51.     ' if recurse, list directories also
    52.     If Recurse Then Attributes = Attributes Or vbDirectory
    53.    
    54.     FindStrInDirRecurse ReturnList, Root, Recurse, FTypes, LOSize, HISize, Attributes, Compare, SFind
    55.    
    56.     RaiseEvent FindFilesDone(ReturnList)
    57. End Sub
    58.  
    59. Private Sub FlattenParamArrayToString(RetList As Collection, ParamArray Arr() As Variant)
    60.     Dim K As Long, Q As Long, Str() As String, StrVal As String
    61.    
    62.     On Error Resume Next
    63.    
    64.     For K = LBound(Arr(0)) To UBound(Arr(0))
    65.         If (VarType(Arr(0)(K)) And vbArray) = vbArray Then
    66.             For Q = LBound(Arr(0)(K)) To UBound(Arr(0)(K))
    67.                
    68.                 If (VarType(Arr(0)(K)(Q)) And vbArray) = vbArray Then
    69.                     FlattenParamArrayToString RetList, Arr(0)(K)(Q)
    70.                 Else
    71.                     StrVal = CStr(Arr(0)(K)(Q))
    72.                    
    73.                     If Err.Number <> 0 Then
    74.                         Err.Clear
    75.                     Else
    76.                         If Len(Trim(StrVal)) > 0 Then RetList.Add StrVal
    77.                     End If
    78.                 End If
    79.             Next Q
    80.         Else
    81.             StrVal = CStr(Arr(0)(K))
    82.            
    83.             If Err.Number <> 0 Then
    84.                 Err.Clear
    85.             Else
    86.                 If Len(Trim(StrVal)) > 0 Then RetList.Add StrVal
    87.             End If
    88.         End If
    89.     Next K
    90. End Sub
    91.  
    92. Private Function FindStrInDirRecurse(ByRef ReturnList As Collection, ByVal Root As String, ByVal Recurse As Boolean, _
    93.                                 FileTypes() As String, ByVal LOSize As Long, ByVal HISize As Long, ByVal Attributes As VbFileAttribute, _
    94.                                 Compare As VbCompareMethod, StrToFind() As String) As Boolean
    95.    
    96.     Dim DirX As String, K As Long, Dirs As New Collection, FF As Integer, FileData As String
    97.     Dim FileSize As Long, CancelFind As Boolean
    98.    
    99.     ' make sure we have "\" at the end of directory
    100.     If Right(Root, 1) <> "\" Then Root = Root & "\"
    101.    
    102.     On Error GoTo ExitFunction
    103.    
    104.     ' find first file, if error here, just exit function
    105.     DirX = Dir(Root & "*.*", Attributes)
    106.    
    107.     RaiseEvent FindFilesCurrentDir(Root)
    108.    
    109.     ' loop until nothing is found
    110.     Do Until Len(DirX) = 0
    111.         If DirX <> "." And DirX <> ".." Then
    112.             On Error Resume Next ' I tried with "On Error GoTo ..." but it does not work for some reason for file "pagefile.sys"
    113.            
    114.             If (GetAttr(Root & DirX) And vbDirectory) = vbDirectory Then ' if directory, add it to our list
    115.                 If Err.Number <> 0 Then GoTo NextFile
    116.                
    117.                 If Recurse Then Dirs.Add DirX
    118.             Else
    119.                 On Error GoTo NextFile
    120.                
    121.                 FileSize = FileLen(Root & DirX)
    122.                
    123.                 ' check file size
    124.                 If (LOSize = 0 And HISize = 0) Or (FileSize >= LOSize And FileSize <= HISize And FileSize > 0) Then
    125.                     ' check file type
    126.                     For K = 0 To UBound(FileTypes)
    127.                         If DirX Like FileTypes(K) Then Exit For
    128.                     Next K
    129.                    
    130.                     If K <= UBound(FileTypes) Then
    131.                         If UBound(StrToFind) = 0 And Len(StrToFind(0)) = 0 Then
    132.                             K = 0 ' don't search in file (search string array is empty)
    133.                         Else
    134.                             FF = FreeFile
    135.                            
    136.                             ' get file data
    137.                             Open Root & DirX For Binary Access Read As FF
    138.                                 FileData = String(LOF(FF), 0)
    139.                                 Get FF, , FileData
    140.                             Close FF
    141.                            
    142.                             ' find string(s) in the file
    143.                             For K = 0 To UBound(StrToFind)
    144.                                 If InStr(1, FileData, StrToFind(K), Compare) > 0 Then Exit For
    145.                             Next K
    146.                         End If
    147.                        
    148.                         ' if passed ALL the tests, and the file to return list
    149.                         If K <= UBound(StrToFind) Then
    150.                             ReturnList.Add Root & DirX
    151.                             RaiseEvent FindFilesFound(Root, DirX, StrToFind(K))
    152.                         End If
    153.                     End If
    154.                 End If
    155.             End If
    156.         End If
    157.        
    158. NextFile: ' for some system files like "hiberfil.sys" or "pagefile.sys" it will return error when you try to access it
    159.        
    160.         If Err.Number <> 0 Then Err.Number = 0
    161.        
    162.         RaiseEvent FindFilesCancel(CancelFind)
    163.        
    164.         If CancelFind Then
    165.             FindStrInDirRecurse = False
    166.             Exit Function
    167.         End If
    168.        
    169.         DirX = Dir
    170.     Loop
    171.    
    172.     On Error GoTo 0
    173.    
    174.     If Recurse Then
    175.         Do While Dirs.Count > 0
    176.             ' recurse through all dirs found in this directory
    177.            
    178.             If FindStrInDirRecurse(ReturnList, Root & Dirs(1), True, FileTypes, LOSize, HISize, Attributes, Compare, StrToFind) Then
    179.                 Dirs.Remove 1
    180.             Else
    181.                 FindStrInDirRecurse = False
    182.                 Exit Function
    183.             End If
    184.         Loop
    185.     End If
    186.    
    187. ExitFunction:
    188.     FindStrInDirRecurse = True
    189. End Function
    Attached Files Attached Files

  4. #4

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Search Dir for text:

    wow thats nuts, and thankyou manavo for that interesting fact

  5. #5
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Search Dir for text:

    Whoa! Thats a huge difference in size. Nice though, prob will make use of it soon in my next program.
    Age - 15 ::: Level - Advanced
    If you find my post useful please ::Rate It::


  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Search Dir for text:

    yep its some good code

  7. #7
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: Search Dir for text:

    this code doesnt work to well, if i search once i need to close the program and run it again to search again.

    anyways i like it, im good with restarting..

    somehow it goes only through like 50 files?? how can i make it go through 100k?
    Last edited by Justa Lol; Nov 15th, 2008 at 08:34 AM.

  8. #8
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Search Dir for text:

    To what code are you refering too ? |2eM!x or mine ?

  9. #9
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: Search Dir for text:

    |2eM!x's code.

  10. #10
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Search Dir for text:

    His code checks files only in directory specified, without going into subdirectories. So maybe that's why you may think it does not check all your files ?

    You should see my code too (see the attachment)

  11. #11
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: Search Dir for text:

    nah i have no sub directories, only over 10000 files in 1 directory, and when i search it gives me an error "to many files."
    Last edited by Justa Lol; Nov 15th, 2008 at 07:06 PM.

  12. #12
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Search Dir for text:

    At what line ?

  13. #13
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: Search Dir for text:

    i used your code too, it goes through 100 files then stops.

  14. #14
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Search Dir for text:

    Well, if you don't tell us more details, I don't see how we can help you

    Good luck on finding the problem

  15. #15
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Search Dir for text:

    Quote Originally Posted by Justa Lol
    nah i have no sub directories, only over 10000 files in 1 directory, and when i search it gives me an error "to many files."
    Too many open files maybe? I don't see any close command in that code, try closing the file after the input.

    Open mypath & p For Binary As #ff
    strbuff = Input(LOF(ff), ff)
    Close #ff

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