Results 1 to 4 of 4

Thread: search sub dir

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Posts
    27

    Wink search sub dir

    Ok so I got this going but how can I search the whole hard drive

    If Dir$("c:\file.exe") <> "" Then
    MsgBox "file exists"
    Else
    FileCopy "R:\netop.exe", "C:\file.exe"
    End If

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    VB Code:
    1. ' Brought to you by Brad Martinez
    2. ' [url]http://members.aol.com/btmtz/vb[/url]
    3. 'Search All Drives
    4. ' Though this example has been optimized for speed,
    5. ' it's obviously not as efficient as it could be.
    6. ' Consider it a starting point...
    7. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    8.  
    9. ' This message helps speed up the initialization of list boxes that have a large number
    10. ' of items (more than 100). It preallocates the specified amount of memory so that
    11. ' subsequent LB_ADDSTRING, LB_INSERTSTRING, LB_DIR, and LB_ADDFILE
    12. ' messages take the shortest possible time. You can use estimates for the wParam and
    13. ' lParam parameters. If you overestimate, some extra memory is allocated; if you
    14. ' underestimate, the normal allocation is used for items that exceed the preallocated amount.
    15. ' wParam:          Specifies the number of items to add.
    16. ' lParam:           Specifies the amount of memory, in bytes, to allocate for item strings.
    17. ' Return Value:   The return value is the maximum number of items that the memory
    18.                        ' object can store before another memory reallocation is needed, if
    19.                        ' successful. It is LB_ERRSPACE if not enough memory is available.
    20. Private Const LB_INITSTORAGE = &H1A8
    21.  
    22. ' An application sends an LB_ADDSTRING message to add a string to a list box.
    23. ' If the list box does not have the LBS_SORT style, the string is added to the end
    24. ' of the list. Otherwise, the string is inserted into the list and the list is sorted.
    25. Private Const LB_ADDSTRING = &H180
    26. Private Const WM_SETREDRAW = &HB
    27. Private Const WM_VSCROLL = &H115
    28. Private Const SB_BOTTOM = 7
    29. 'this constant is used in displaying an item in the files list box as a tool tip
    30. 'see the mouse_move event for that control
    31. Private Const LB_ITEMFROMPOINT = &H1A9
    32.  
    33. ' If the function succeeds, the return value is a bitmask
    34. ' representing the currently available disk drives. Bit
    35. ' position 0 (the least-significant bit) is drive A, bit position
    36. ' 1 is drive B, bit position 2 is drive C, and so on.
    37. ' If the function fails, the return value is zero.
    38. Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
    39.  
    40. ' If the function succeeds, the return value is a search handle
    41. ' used in a subsequent call to FindNextFile or FindClose
    42. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    43.  
    44. 'FindFirstFile failure rtn value
    45. Private Const INVALID_HANDLE_VALUE = -1
    46.  
    47. ' Rtns True (non zero) on succes, False on failure
    48. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    49.  
    50. ' Rtns True (non zero) on succes, False on failure
    51. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    52.  
    53. Private Type FILETIME
    54.     dwLowDateTime As Long
    55.     dwHighDateTime As Long
    56. End Type
    57.  
    58. Private Const MaxLFNPath = 260
    59.  
    60. Private Type WIN32_FIND_DATA
    61.     dwFileAttributes As Long
    62.     ftCreationTime As FILETIME
    63.     ftLastAccessTime As FILETIME
    64.     ftLastWriteTime As FILETIME
    65.     nFileSizeHigh As Long
    66.     nFileSizeLow As Long
    67.     dwReserved0 As Long
    68.     dwReserved1 As Long
    69.     cFileName As String * MaxLFNPath
    70.     cShortFileName As String * 14
    71. End Type
    72.  
    73. ' A liberal use of form level variables...
    74. Private PicHeight As Integer
    75. Private hLB As Long
    76. Private FileSpec As String
    77. Private UseFileSpec As Integer
    78. Private TotalDirs As Integer
    79. Private TotalFiles As Integer
    80. Private Running As Boolean
    81.  
    82. ' SearchDirs() private constants
    83. Private Const vbBackslash = "\"
    84. Private Const vbAllFiles = "*.*"
    85. Private Const vbKeyDot = 46
    86.  
    87.  
    88. Private Sub GetDrives(LB As ListBox, ByVal IsUsed As Boolean)
    89.            
    90.             Dim DriveLtr As Long
    91.             LB.Clear
    92.             For DriveLtr = 0 To 25
    93.                 If CBool(GetLogicalDrives And (2 ^ DriveLtr)) = IsUsed Then
    94.                    LB.AddItem Chr$(Asc("A") + DriveLtr) & ":"
    95.                 End If
    96.             Next
    97. End Sub
    98.  
    99. Private Sub SearchDirs(CurPath As String)  ' curpath$ is passed w/ trailing "\"
    100.  
    101.     ' These can't be static!!! They must be
    102.     ' re-allocated on each recursive call.
    103.     Dim Dirs As Integer
    104.     Dim DirBuf() As String
    105.     Dim i As Integer
    106.     Dim j As Integer
    107.     ' These variables are declared as Static to save on
    108.     ' stack space & on variable re-allocation time
    109.     Static WFD As WIN32_FIND_DATA
    110.     Static hItem As Long
    111.     ' Display what's happening...
    112.     ' A Timer could be used instead to display status at
    113.     ' pre-defined intervals, saving on PictureBox redraw time...
    114.     lblStatus.Caption = "Searching " & CurPath$
    115.     ' Allows the PictureBox to be redrawn
    116.     ' & this proc to be cancelled by the user.
    117.     ' It's not necessary to have this in the loop
    118.     ' below since the loop works so fast...
    119.     DoEvents
    120.     If Not Running Then Exit Sub
    121.     ' This loop finds *every* subdir and file in the current dir
    122.     hItem& = FindFirstFile(CurPath$ & vbAllFiles, WFD)
    123.     If hItem& <> INVALID_HANDLE_VALUE Then
    124.         Do
    125.             ' Tests for subdirs only...
    126.             If (WFD.dwFileAttributes And vbDirectory) Then
    127.                
    128.                 ' If not a  "." or ".." DOS subdir...
    129.                 If Asc(WFD.cFileName) <> vbKeyDot Then
    130.                     ' This is executed in the cmdFind_Click()
    131.                     ' call though it isn't used...
    132.                     TotalDirs% = TotalDirs% + 1
    133.                     ' This is the heart of a recursive proc...
    134.                     ' Cache the subdirs of the current dir in the 1 based array.
    135.                     ' This proc calls itself below for each subdir cached in the array.
    136.                     ' (re-allocating the array only once every 10 itinerations improves speed)
    137.                     If (Dirs% Mod 10) = 0 Then ReDim Preserve DirBuf$(Dirs% + 10)
    138.                     Dirs% = Dirs% + 1
    139.                     DirBuf$(Dirs%) = Left$(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1)
    140.                 End If
    141.            
    142.             ' File size and attribute tests can be used here, i.e:
    143.             ' ElseIf (WFD.dwFileAttributes And vbHidden) = False Then  'etc...
    144.            
    145.             ' Get a total file count for cmdFolderInfo_Click()
    146.             ElseIf Not UseFileSpec% Then
    147.                 TotalFiles% = TotalFiles% + 1
    148.             End If
    149.        
    150.         ' Get the next subdir or file
    151.         Loop While FindNextFile(hItem&, WFD)
    152.    
    153.         ' Close the search handle
    154.         Call FindClose(hItem&)
    155.    
    156.     End If
    157.     ' When UseFileSpec% is set cmdFind_Click(),
    158.     ' SearchFileSpec() is called & each folder must be
    159.     ' searched a second time.
    160.     If UseFileSpec% Then
    161.         ' Turning off painting speeds things quite a bit...
    162.         ' Speed also would be vastly improved if the redrawing
    163.         ' & scrolling were placed in a Timer event...
    164.         SendMessage hLB&, WM_SETREDRAW, 0, 0
    165.         Call SearchFileSpec(CurPath$)
    166.         ' Keeps the currently found items scrolled into view...
    167.         SendMessage hLB&, WM_VSCROLL, SB_BOTTOM, 0
    168.         SendMessage hLB&, WM_SETREDRAW, 1, 0
    169.     End If
    170.  
    171.     ' Recursively call this proc & iterate through each subdir cached above.
    172.     For i = 1 To Dirs%
    173.         SearchDirs CurPath$ & DirBuf$(i%) & vbBackslash
    174.     Next
    175.  
    176. End Sub
    177.  
    178. Private Sub SearchFileSpec(CurPath$)   ' curpath$ is passed w/ trailing "\"
    179. ' This procedure *only*  finds files in the current folder that match the FileSpec$
    180. ' These variables are declared as Static to save on
    181. ' stack space & on variable re-allocation time
    182.     Static WFD As WIN32_FIND_DATA
    183.     Static hFile As Long
    184.    
    185.     hFile = FindFirstFile(CurPath$ & FileSpec$, WFD)
    186.     If hFile <> INVALID_HANDLE_VALUE Then
    187.        
    188.         Do
    189.             ' Use DoEvents here since we're loading a ListBox and
    190.             ' there could be hundreds of files matching the FileSpec$
    191.             DoEvents
    192.             If Not Running Then Exit Sub
    193.            
    194.             ' The ListBox's Sorted property is initially set to False.
    195.             ' Set it to True and see how things slow down a bit...
    196.             SendMessage hLB&, LB_ADDSTRING, 0, _
    197.                 ByVal CurPath$ & Left$(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1)
    198.        
    199.         ' Get the next file matching the FileSpec$
    200.         Loop While FindNextFile(hFile, WFD)
    201.        
    202.         ' Close the search handle
    203.         Call FindClose(hFile)
    204.    
    205.     End If
    206.  
    207. End Sub

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Last edited by Bruce Fox; Aug 14th, 2011 at 04:22 AM.

  4. #4
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    this is a recursive procedure that uses the filesystemobject to go through all folders.

    may not be as fast, but its nice and simple

    VB Code:
    1. Private Sub Search(oFolder As folder, strFileName As String)
    2. 'WARNING: This procedure is recursive!!!!
    3.     Dim fd As folder
    4.     Dim f As File
    5.    
    6.     For Each f In oFolder.Files
    7.         If f.Name = strFileName Then
    8.             lbxFound.AddItem f.Path
    9.             DoEvents
    10.         End If
    11.     Next f
    12.    
    13.     'This is the recursive bit...
    14.     For Each fd In oFolder.SubFolders
    15.         Search fd, strFileName
    16.     Next fd
    17. End Sub


    here's an example i made of how to use it...
    Attached Files Attached Files
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

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