Results 1 to 14 of 14

Thread: Searching an entire drive ( c:\ )

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    17

    Searching an entire drive ( c:\ )

    okay okay, my friends tell me its quite easy and to use something but i cant find anything that can help me, then i turned to searching on geocities, i found the filesystemobject stuff, yet that is not helping me either.

    I need a way to search the entire drive (sorta like what virus scanners do, yet much simplier, all i need it for is to check filenames) And fso doesnt have a function for that and i dont know what to do now, can anyone please help me??

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Something like:
    VB Code:
    1. Option Explicit
    2.  
    3. 'Module
    4.  
    5. '*******************************************************************************
    6. '  Original Code by: KPD-Team 1999
    7. '*******************************************************************************
    8.  
    9. Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    10. Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    11. Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    12. Public Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    13.  
    14. Const MAX_PATH = 260
    15. Const MAXDWORD = &HFFFF
    16. Const INVALID_HANDLE_VALUE = -1
    17. Const FILE_ATTRIBUTE_ARCHIVE = &H20
    18. Const FILE_ATTRIBUTE_DIRECTORY = &H10
    19. Const FILE_ATTRIBUTE_HIDDEN = &H2
    20. Const FILE_ATTRIBUTE_NORMAL = &H80
    21. Const FILE_ATTRIBUTE_READONLY = &H1
    22. Const FILE_ATTRIBUTE_SYSTEM = &H4
    23. Const FILE_ATTRIBUTE_TEMPORARY = &H100
    24.  
    25. Const ColPurple = &HFF00FF
    26. Const ColDarkGreen = &H8000&
    27.  
    28. Public Type FILETIME
    29.     dwLowDateTime As Long
    30.     dwHighDateTime As Long
    31. End Type
    32.  
    33. Public Type WIN32_FIND_DATA
    34.     DWFILEATTRIBUTES As Long
    35.     FTCREATIONTIME As FILETIME
    36.     FTLASTACCESSTIME As FILETIME
    37.     FTLASTWRITETIME As FILETIME
    38.     NFILESIZEHIGH As Long
    39.     NFILESIZELOW As Long
    40.     DWRESERVED0 As Long
    41.     DWRESERVED1 As Long
    42.     CFILENAME As String * MAX_PATH
    43.     CALTERNATE As String * 14
    44. End Type
    45.  
    46.  
    47. Public Function FINDFILESAPI(PATH As String, SEARCHSTR As String, FileCount As Integer, DirCount As Integer)
    48.     Dim FILENAME As String      'WALKING FILENAME VARIABLE...
    49.     Dim DIRNAME As String       'SUBDIRECTORY NAME
    50.     Dim DIRNAMES() As String    'BUFFER FOR DIRECTORY NAME ENTRIES
    51.     Dim NDIR As Integer         'NUMBER OF DIRECTORIES IN THIS PATH
    52.     Dim i As Integer            'FOR-LOOP COUNTER...
    53.     Dim HSEARCH As Long         'SEARCH HANDLE
    54.  
    55.     Dim WFD As WIN32_FIND_DATA
    56.     Dim CONT As Integer
    57.  
    58.     'Search for Sub-Directories
    59.     If Right(PATH, 1) <> "\" Then PATH = PATH & "\"
    60.  
    61.     NDIR = 0
    62.  
    63.     ReDim DIRNAMES(NDIR)
    64.  
    65.     CONT = True
    66.  
    67.     HSEARCH = FindFirstFile(PATH & "*", WFD)
    68.  
    69.     If HSEARCH <> INVALID_HANDLE_VALUE Then
    70.  
    71.         Do While CONT
    72.  
    73.             DIRNAME = STRIPNULLS(WFD.CFILENAME)
    74.  
    75.             'Ignore the current and encompassing directories
    76.             If (DIRNAME <> ".") And (DIRNAME <> "..") Then
    77.  
    78.                 'Check for Directory with BitWise comparison
    79.                 If GetFileAttributes(PATH & DIRNAME) And FILE_ATTRIBUTE_DIRECTORY Then
    80.  
    81.                     DIRNAMES(NDIR) = DIRNAME
    82.                     DirCount = DirCount + 1
    83.                     NDIR = NDIR + 1
    84.                     ReDim Preserve DIRNAMES(NDIR)
    85.  
    86.                 End If
    87.  
    88.             End If
    89.  
    90.             'Get next Sub-Directory
    91.             CONT = FindNextFile(HSEARCH, WFD)
    92.  
    93.         Loop
    94.  
    95.         CONT = FindClose(HSEARCH)
    96.  
    97.     End If
    98.  
    99.     'Walk through this directory and Sum File sizes
    100.     HSEARCH = FindFirstFile(PATH & SEARCHSTR, WFD)
    101.  
    102.     CONT = True
    103.  
    104.     If HSEARCH <> INVALID_HANDLE_VALUE Then
    105.  
    106.         Do
    107.  
    108.             FILENAME = STRIPNULLS(WFD.CFILENAME)
    109.  
    110.             If (FILENAME <> ".") And (FILENAME <> "..") Then
    111.  
    112.                 FINDFILESAPI = FINDFILESAPI + (WFD.NFILESIZEHIGH * MAXDWORD) + WFD.NFILESIZELOW
    113.                 FileCount = FileCount + 1
    114.  
    115.                 '****************************************************
    116.                 'Display
    117.                 If InStr(FILENAME, ".") Then
    118.                     MsgBox PATH & FILENAME
    119.                 End If
    120.                 '****************************************************
    121.  
    122.             End If
    123.  
    124.             'Get next File
    125.             CONT = FindNextFile(HSEARCH, WFD)
    126.  
    127.         Loop Until CONT = 0
    128.  
    129.         CONT = FindClose(HSEARCH)
    130.  
    131.     End If
    132.  
    133.     'If there are Sub_Directories, then recursively walk into them
    134.     If NDIR > 0 Then
    135.  
    136.         For i = 0 To NDIR - 1
    137.             FINDFILESAPI = _
    138.                 FINDFILESAPI + FINDFILESAPI(PATH & DIRNAMES(i) & "\", SEARCHSTR, FileCount, DirCount)
    139.         Next i
    140.  
    141.     End If
    142.  
    143. End Function
    144.  
    145. Public Function STRIPNULLS(ORIGINALSTR As String) As String
    146. ' Remove NULL Characters
    147.  
    148.     If (InStr(ORIGINALSTR, Chr(0)) > 0) Then
    149.         ORIGINALSTR = Left(ORIGINALSTR, InStr(ORIGINALSTR, Chr(0)) - 1)
    150.     End If
    151.  
    152.     STRIPNULLS = ORIGINALSTR
    153.  
    154. End Function
    155.  
    156.  
    157.  
    158.  
    159.  
    160. 'Form
    161.  
    162. Private Sub Form_Load()
    163.     Call FINDFILESAPI("C:\", "*.*", 0, 0)
    164. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    17
    thank u very much, though now the only problem is is that i changed it, instead of the message box it does other commands like Files left goes down, and that sort of stuff, but the thing is is that it freezes now (im thinking just cuz its reading many many files at one time)

    anything i can do about this?

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Hi iamthom,
    try adding DoEvents instide your loops.

    example

    VB Code:
    1. Do While CONT
    2.  
    3.             DIRNAME = STRIPNULLS(WFD.CFILENAME)
    4.  
    5.             'Ignore the current and encompassing directories
    6.             If (DIRNAME <> ".") And (DIRNAME <> "..") Then
    7.  
    8.                 'Check for Directory with BitWise comparison
    9.                 If GetFileAttributes(PATH & DIRNAME) And FILE_ATTRIBUTE_DIRECTORY Then
    10.  
    11.                     DIRNAMES(NDIR) = DIRNAME
    12.                     DirCount = DirCount + 1
    13.                     NDIR = NDIR + 1
    14.                     ReDim Preserve DIRNAMES(NDIR)
    15.  
    16.                 End If
    17.  
    18.             End If
    19.  
    20.             'Get next Sub-Directory
    21.             CONT = FindNextFile(HSEARCH, WFD)
    22.  
    23.             [b]DoEvents[/b]
    24.  
    25.         Loop
    26.  
    27.         CONT = FindClose(HSEARCH)
    28.  
    29.     End If
    -= a peet post =-

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    17
    i love you! lol thank u so much, that solved my prob =D

    yay, one step closer to making my program

    the only problem now is that it comes up with an error (overflow) but i believe i should be able to fix that, now just to edit thecode to my liking

    once again thank u both

  6. #6
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    I'm sure Bruce love you too

    mmm.. the overflow is probably an integer that hits the roof... maybe you figured it out already ?
    -= a peet post =-

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    17
    yea, i figured that out already

    it was the filecount = filecount + 1 part, and filecount was defined as a integer, so i just made it a double so it coud go higher, it works now.

  8. #8
    Member simlee's Avatar
    Join Date
    Mar 2002
    Posts
    40
    Sorry to dig up an old post folks....

    The FindFile Code is excellent and almost does exactly what I am looking for except.....

    Some files i might be finding may not have extensions, but I do not want the directories returned as a result. Is there anyway around this?

    Thanks

    Simlee

  9. #9
    Addicted Member
    Join Date
    Sep 2002
    Posts
    251

    About this code.

    Hey guys!

    I found this code wonderfull, and extremelly fast. I just was wondering about one thing...

    I have a lot of partitions (C:, D:, E:...)

    I found out by windows that my C: have 19119 files, but using a counter in the code, it only finds 17279...


    In the D: (2140 files windows says) this code found 2143 compared with what windows shows...

    why it happens?

    Thanks in advance,

    Elminster

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    It is probably not finding the hidden or system files. I found this
    out when using similar code, but what I did was to use a dos
    search executed from within a .bat file. Also very fast.
    The /s is to specify a search and the /a is to search all.
    Code:
    Dir C:\*.* /s /a > C_Drive_File_Contents.txt
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    Member simlee's Avatar
    Join Date
    Mar 2002
    Posts
    40
    *bump*

    Don't suppose anyone can advise on my query?

    Thanks

    simlee

  12. #12
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959
    Well you never actually use any of these constants in the code,

    VB Code:
    1. Const FILE_ATTRIBUTE_HIDDEN = &H2
    2. Const FILE_ATTRIBUTE_NORMAL = &H80
    3. Const FILE_ATTRIBUTE_READONLY = &H1
    4. Const FILE_ATTRIBUTE_SYSTEM = &H4
    5. Const FILE_ATTRIBUTE_TEMPORARY = &H100

    even though they're declared, so you probaly have to mod it to allow for all these files. Thats why your coming up short on the list count.
    Last edited by Jmacp; May 25th, 2004 at 11:17 AM.

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    If you want to know if you are getting all the files, use the dos
    method as a control. Then compare against your program. Usually
    the system and hidden files wont get picked up unless you
    specify the proper flags like in the above post.

    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14
    Addicted Member
    Join Date
    Sep 2002
    Posts
    251

    Hey

    Hello!

    How should I use the flags on the code?
    Its just to declare them?

    Thanks for your time,

    Elminster

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