|
-
Nov 4th, 2003, 09:16 PM
#1
Thread Starter
Junior Member
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??
-
Nov 4th, 2003, 10:13 PM
#2
Something like:
VB Code:
Option Explicit
'Module
'*******************************************************************************
' Original Code by: KPD-Team 1999
'*******************************************************************************
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Const ColPurple = &HFF00FF
Const ColDarkGreen = &H8000&
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type WIN32_FIND_DATA
DWFILEATTRIBUTES As Long
FTCREATIONTIME As FILETIME
FTLASTACCESSTIME As FILETIME
FTLASTWRITETIME As FILETIME
NFILESIZEHIGH As Long
NFILESIZELOW As Long
DWRESERVED0 As Long
DWRESERVED1 As Long
CFILENAME As String * MAX_PATH
CALTERNATE As String * 14
End Type
Public Function FINDFILESAPI(PATH As String, SEARCHSTR As String, FileCount As Integer, DirCount As Integer)
Dim FILENAME As String 'WALKING FILENAME VARIABLE...
Dim DIRNAME As String 'SUBDIRECTORY NAME
Dim DIRNAMES() As String 'BUFFER FOR DIRECTORY NAME ENTRIES
Dim NDIR As Integer 'NUMBER OF DIRECTORIES IN THIS PATH
Dim i As Integer 'FOR-LOOP COUNTER...
Dim HSEARCH As Long 'SEARCH HANDLE
Dim WFD As WIN32_FIND_DATA
Dim CONT As Integer
'Search for Sub-Directories
If Right(PATH, 1) <> "\" Then PATH = PATH & "\"
NDIR = 0
ReDim DIRNAMES(NDIR)
CONT = True
HSEARCH = FindFirstFile(PATH & "*", WFD)
If HSEARCH <> INVALID_HANDLE_VALUE Then
Do While CONT
DIRNAME = STRIPNULLS(WFD.CFILENAME)
'Ignore the current and encompassing directories
If (DIRNAME <> ".") And (DIRNAME <> "..") Then
'Check for Directory with BitWise comparison
If GetFileAttributes(PATH & DIRNAME) And FILE_ATTRIBUTE_DIRECTORY Then
DIRNAMES(NDIR) = DIRNAME
DirCount = DirCount + 1
NDIR = NDIR + 1
ReDim Preserve DIRNAMES(NDIR)
End If
End If
'Get next Sub-Directory
CONT = FindNextFile(HSEARCH, WFD)
Loop
CONT = FindClose(HSEARCH)
End If
'Walk through this directory and Sum File sizes
HSEARCH = FindFirstFile(PATH & SEARCHSTR, WFD)
CONT = True
If HSEARCH <> INVALID_HANDLE_VALUE Then
Do
FILENAME = STRIPNULLS(WFD.CFILENAME)
If (FILENAME <> ".") And (FILENAME <> "..") Then
FINDFILESAPI = FINDFILESAPI + (WFD.NFILESIZEHIGH * MAXDWORD) + WFD.NFILESIZELOW
FileCount = FileCount + 1
'****************************************************
'Display
If InStr(FILENAME, ".") Then
MsgBox PATH & FILENAME
End If
'****************************************************
End If
'Get next File
CONT = FindNextFile(HSEARCH, WFD)
Loop Until CONT = 0
CONT = FindClose(HSEARCH)
End If
'If there are Sub_Directories, then recursively walk into them
If NDIR > 0 Then
For i = 0 To NDIR - 1
FINDFILESAPI = _
FINDFILESAPI + FINDFILESAPI(PATH & DIRNAMES(i) & "\", SEARCHSTR, FileCount, DirCount)
Next i
End If
End Function
Public Function STRIPNULLS(ORIGINALSTR As String) As String
' Remove NULL Characters
If (InStr(ORIGINALSTR, Chr(0)) > 0) Then
ORIGINALSTR = Left(ORIGINALSTR, InStr(ORIGINALSTR, Chr(0)) - 1)
End If
STRIPNULLS = ORIGINALSTR
End Function
'Form
Private Sub Form_Load()
Call FINDFILESAPI("C:\", "*.*", 0, 0)
End Sub
-
Nov 4th, 2003, 11:22 PM
#3
Thread Starter
Junior Member
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?
-
Nov 5th, 2003, 12:23 AM
#4
-= B u g S l a y e r =-
Hi iamthom,
try adding DoEvents instide your loops.
example
VB Code:
Do While CONT
DIRNAME = STRIPNULLS(WFD.CFILENAME)
'Ignore the current and encompassing directories
If (DIRNAME <> ".") And (DIRNAME <> "..") Then
'Check for Directory with BitWise comparison
If GetFileAttributes(PATH & DIRNAME) And FILE_ATTRIBUTE_DIRECTORY Then
DIRNAMES(NDIR) = DIRNAME
DirCount = DirCount + 1
NDIR = NDIR + 1
ReDim Preserve DIRNAMES(NDIR)
End If
End If
'Get next Sub-Directory
CONT = FindNextFile(HSEARCH, WFD)
[b]DoEvents[/b]
Loop
CONT = FindClose(HSEARCH)
End If
-
Nov 5th, 2003, 01:03 PM
#5
Thread Starter
Junior Member
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
-
Nov 5th, 2003, 03:45 PM
#6
-= B u g S l a y e r =-
I'm sure Bruce love you too 
mmm.. the overflow is probably an integer that hits the roof... maybe you figured it out already ?
-
Nov 5th, 2003, 03:56 PM
#7
Thread Starter
Junior Member
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.
-
May 24th, 2004, 08:18 AM
#8
Member
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
-
May 24th, 2004, 08:57 AM
#9
Addicted Member
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
-
May 24th, 2004, 10:38 AM
#10
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 25th, 2004, 02:05 AM
#11
Member
*bump*
Don't suppose anyone can advise on my query?
Thanks
simlee
-
May 25th, 2004, 09:00 AM
#12
Well you never actually use any of these constants in the code,
VB Code:
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
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.
-
May 25th, 2004, 10:33 AM
#13
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 25th, 2004, 12:34 PM
#14
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|