|
-
Aug 31st, 2001, 01:43 PM
#1
Thread Starter
Member
find files -> alter code ? chrisjk
this " find files " code was posted by chrisjk , it works great but i would like to use this code to find a specific file format , say i wanna find only Mp3's or whatever , how would i alter the code to do that ? also , what do i change so it doesn't search in the recycle bin anymore ?
thanx.
Code:
'Create a form with a command button (command1), a list box (list1)
'and four text boxes (text1, text2, text3 and text4).
'Type in the first textbox a startingpath like c:\
'and in the second textbox you put a pattern like *.* or *.txt
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) 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
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private 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
Function StripNulls(OriginalStr As String) As String
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If
StripNulls = OriginalStr
End Function
Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
'KPD-Team 1999
'E-Mail: [email protected]
'URL: http://www.allapi.net/
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
If Right(path, 1) <> "\" Then path = path & "\"
' Search for subdirectories.
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
Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
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
While Cont
FileName = StripNulls(WFD.cFileName)
If (FileName <> ".") And (FileName <> "..") Then
FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
FileCount = FileCount + 1
List1.AddItem path & FileName
End If
Cont = FindNextFile(hSearch, WFD) ' Get next file
Wend
Cont = FindClose(hSearch)
End If
' If there are sub-directories...
If nDir > 0 Then
' Recursively walk into them...
For i = 0 To nDir - 1
FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
Next i
End If
End Function
Sub Command1_Click()
Dim SearchPath As String, FindStr As String
Dim FileSize As Long
Dim NumFiles As Integer, NumDirs As Integer
Screen.MousePointer = vbHourglass
List1.Clear
SearchPath = Text1.Text
FindStr = Text2.Text
FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
Screen.MousePointer = vbDefault
End Sub
-
Aug 31st, 2001, 02:25 PM
#2
Frenzied Member
http://forums.vb-world.net/showthrea...ight=skitchen8 i asked the very same question, but as to the recycle bin i have no idea
Government is another way to say better…than…you.
It’s like ice but no pick, a murder charge that won’t stick,
it’s like a whole other world where you can smell the food,
but you can’t touch the silverware.
Huh, what luck. Fascism you can vote for.
Humph, isn’t that sweet?
And we’re all gonna die some day, because that’s the American way
-Stone Sour
-
Aug 31st, 2001, 05:31 PM
#3
Thread Starter
Member
so i noticed , guess u don't know how to make that code search for 1 exact file huh
-
Aug 31st, 2001, 05:36 PM
#4
PowerPoster
if you follow the directions, you'll see you need to add 4 textboxes...in the second one, type the exact filename you want.
If you only want to search for mp3s, put "*.mp3" in text2
-
Aug 31st, 2001, 05:57 PM
#5
Thread Starter
Member
i understand that chrisjk , but i want the second textbox out , so the search is for one specific file , ie its for a program i made that only replace's one exact file , so there is no need to search for other files . and how do i alter the code to skip the recycle bin ?
-
Aug 31st, 2001, 06:14 PM
#6
PowerPoster
Make these changes then
VB Code:
Sub Command1_Click()
Dim SearchPath As String, FindStr As String
Dim FileSize As Long
Dim NumFiles As Integer, NumDirs As Integer
Screen.MousePointer = vbHourglass
List1.Clear
[b]SearchPath = "C:\"[/b]
[b]FindStr = "*.mp3"[/b]
FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
Screen.MousePointer = vbDefault
End Sub
As for ignoring the recycle in, this is the modified FindFiles function
VB Code:
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
If Right(path, 1) <> "\" Then path = path & "\"
' Search for subdirectories.
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 <> "..") And InStr(1, DirName, "RECYCLE") < 1 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
Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
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
While Cont
FileName = StripNulls(WFD.cFileName)
If (FileName <> ".") And (FileName <> "..") Then
FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
FileCount = FileCount + 1
List1.AddItem path & FileName
End If
Cont = FindNextFile(hSearch, WFD) ' Get next file
Wend
Cont = FindClose(hSearch)
End If
' If there are sub-directories...
If nDir > 0 Then
' Recursively walk into them...
For i = 0 To nDir - 1
FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
Next i
End If
End Function
-
Aug 31st, 2001, 06:19 PM
#7
Registered User
My guess is that find files doesn't scan any vitual folders at all, it doesn't just skip the recycle bin, which makes its performance much faster.
-
Aug 31st, 2001, 06:28 PM
#8
Thread Starter
Member
works like a charm thanks ,,
-
Aug 31st, 2001, 06:36 PM
#9
PowerPoster
What OS are you on? I tested it under 2k (where the recycle bin is C:\Recycler). Can't remeber if that is the same under 98/95.
-
Aug 31st, 2001, 06:38 PM
#10
Thread Starter
Member
SearchPath = "C:\"
FindStr = "*.mp3"
can the SearchPath use a drivelistbox ?
looks much better with a drivelistbox then a textbox imho
i'm using win 98 SE btw.
-
Aug 31st, 2001, 06:40 PM
#11
PowerPoster
Originally posted by Dutch
SearchPath = "C:\"
FindStr = "*.mp3"
can the SearchPath use a drivelistbox ?
looks much better with a drivelistbox then a textbox imho
Yes...
VB Code:
SearchPath = Drive1.Drive
-
Aug 31st, 2001, 06:49 PM
#12
Thread Starter
Member
ok , i should have figured that myself , sorry to say tho , the drive1.drive doesn't work , well not with the search file code that is
-
Aug 31st, 2001, 06:50 PM
#13
PowerPoster
-
Aug 31st, 2001, 06:57 PM
#14
Thread Starter
Member
doesn't work , strange huh
-
Aug 31st, 2001, 06:59 PM
#15
Frenzied Member
it should, drive1.drive should return something such as "C:" and adding the "\" to it should make it "C:\"
Government is another way to say better…than…you.
It’s like ice but no pick, a murder charge that won’t stick,
it’s like a whole other world where you can smell the food,
but you can’t touch the silverware.
Huh, what luck. Fascism you can vote for.
Humph, isn’t that sweet?
And we’re all gonna die some day, because that’s the American way
-Stone Sour
-
Aug 31st, 2001, 07:01 PM
#16
Thread Starter
Member
it doesn't return anything , not even a error
-
Aug 31st, 2001, 07:03 PM
#17
Frenzied Member
ok, do msgbox drive1.drive and see what it says, or paste this code in
Last edited by Skitchen8; Aug 31st, 2001 at 07:06 PM.
Government is another way to say better…than…you.
It’s like ice but no pick, a murder charge that won’t stick,
it’s like a whole other world where you can smell the food,
but you can’t touch the silverware.
Huh, what luck. Fascism you can vote for.
Humph, isn’t that sweet?
And we’re all gonna die some day, because that’s the American way
-Stone Sour
-
Aug 31st, 2001, 07:08 PM
#18
Thread Starter
Member
with ? drive1.drive it says compile error
-
Aug 31st, 2001, 07:11 PM
#19
Frenzied Member
i changed it, try again... ? is print so i had to do debug.? to make it work, that will print it into the immediate window
Government is another way to say better…than…you.
It’s like ice but no pick, a murder charge that won’t stick,
it’s like a whole other world where you can smell the food,
but you can’t touch the silverware.
Huh, what luck. Fascism you can vote for.
Humph, isn’t that sweet?
And we’re all gonna die some day, because that’s the American way
-Stone Sour
-
Aug 31st, 2001, 07:14 PM
#20
Thread Starter
Member
still a compile error "expected expression"
-
Aug 31st, 2001, 07:20 PM
#21
Frenzied Member
did you make sure you have a drive list box named drive1??
Government is another way to say better…than…you.
It’s like ice but no pick, a murder charge that won’t stick,
it’s like a whole other world where you can smell the food,
but you can’t touch the silverware.
Huh, what luck. Fascism you can vote for.
Humph, isn’t that sweet?
And we’re all gonna die some day, because that’s the American way
-Stone Sour
-
Aug 31st, 2001, 07:24 PM
#22
Thread Starter
Member
yeah , i am
Code:
Sub Command1_Click()
Dim SearchPath As String, FindStr As String
Dim FileSize As Long
Dim NumFiles As Integer, NumDirs As Integer
Screen.MousePointer = vbHourglass
List1.Clear
SearchPath = Drive1.Drive & "\"
FindStr = "*.mp3"
FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
Screen.MousePointer = vbDefault
End Sub
and its called Drive1 in the properties screen too , so that should be ok then huh
-
Aug 31st, 2001, 07:36 PM
#23
Thread Starter
Member
i don't get it , as soon as i use a textbox again , it works , but it doesn't work with a drivelistbox and Drive1.Drive ? is there a line in the code that points to a textbox ? if there is i don't see it.
-
Aug 31st, 2001, 08:37 PM
#24
PowerPoster
Here is a tested working demo
-
Aug 31st, 2001, 09:02 PM
#25
Thread Starter
Member
crap , now i'm really confused , that demo program doesn't work on my computer , i get no errors , but the program just doesn't search , it doesn't do anything when i run it .
this must have something to do with my computer
if it works ok for you.
i replaced the drivelist box in that demo for a textbox
and edited the SearchPath to Text1.Text and now it
works again.
Last edited by Dutch; Aug 31st, 2001 at 09:10 PM.
-
Aug 31st, 2001, 10:10 PM
#26
Thread Starter
Member
when i change
SearchPatch = Drive1.Drive & "\"
FindStr = "*.mp3"
to this
SearchPatch = Drive1.Drive & "\"
FindStr = "*.*"
it finds every single file , how come when i wanna search
for *.mp3 it doesn't work ??
Last edited by Dutch; Aug 31st, 2001 at 10:58 PM.
-
Sep 1st, 2001, 09:29 AM
#27
Frenzied Member
Government is another way to say better…than…you.
It’s like ice but no pick, a murder charge that won’t stick,
it’s like a whole other world where you can smell the food,
but you can’t touch the silverware.
Huh, what luck. Fascism you can vote for.
Humph, isn’t that sweet?
And we’re all gonna die some day, because that’s the American way
-Stone Sour
-
Sep 1st, 2001, 05:44 PM
#28
PowerPoster
Originally posted by Dutch
it finds every single file , how come when i wanna search
for *.mp3 it doesn't work ??
Are you sure you actually have mp3s on that drive?
Does it appear to be searching, but not display anything, or just not searching at all.
-
Sep 1st, 2001, 05:50 PM
#29
Thread Starter
Member
yes , i'm sure there are mp3's on that drive , but it doesn't matter
file format i try to look for , it only works with *.* and it leaves me
clueless why it does that. i can click the button and nothing happens . nothing at all , no hourglass , no error , nothing.
-
Sep 1st, 2001, 06:09 PM
#30
PowerPoster
Well I'm at a loss. Without having your computer I couldn't possibly work out why it works great on this, but not yours.
-
Sep 1st, 2001, 06:12 PM
#31
Thread Starter
Member
well i guess i have to use a textbox then , and not a fancy drivelistbox , i'll make the program with a drivelistbox anyway and let others test it.
-
Sep 1st, 2001, 06:13 PM
#32
PowerPoster
I tested it on a Win98SE system, works like a charm
-
Sep 1st, 2001, 06:29 PM
#33
Thread Starter
Member
i honestly don't know why it doesn't work for me , i know that it sux tho not your demo program and not my program works when i use a drivelistbox , only works with textbox in which i have to type C:\
-
Sep 2nd, 2001, 12:21 AM
#34
Hmm,
Does this help any?
VB Code:
Private Sub Command1_Click()
Dim Find_What As String
Dim Start_Where As String
Dim SKIP_THIS As String
SKIP_THIS = "C:\RECYCLED" '''Your Recycle bin
Find_What = "*.mp3" '''What you want to find
Start_Where = "c:\" '''Your initial directory
List1.Clear
Call FIND_IT(Start_Where, Find_What, SKIP_THIS)
End Sub
Private Sub FIND_IT(ByVal M_Where As String, ByVal M_What As String, ByVal Skip_What As String)
Dim MY_DIRS() As String
Dim MY_COUNT As Integer '
MY_COUNT = 0
ReDim MY_DIRS(1)
If Right(M_Where, 1) <> "\" Then
M_Where = M_Where & "\"
End If
doiexist = Dir(M_Where & "*.*", vbDirectory)
While doiexist <> ""
If doiexist <> "." And doiexist <> ".." Then
If (GetAttr(M_Where & doiexist) And vbDirectory) = vbDirectory Then
MY_COUNT = MY_COUNT + 1
ReDim Preserve MY_DIRS(MY_COUNT)
MY_DIRS(MY_COUNT) = M_Where & doiexist
Form1.Caption = MY_COUNT
DoEvents
End If
End If
doiexist = Dir
Wend
If Left(M_Where, Len(Skip_What) + 1) <> Skip_What & "\" Then
doiexist = Dir(M_Where & M_What, vbNormal)
While doiexist <> ""
List1.AddItem M_Where & doiexist
doiexist = Dir
Wend
End If
For Each Thing In MY_DIRS
If Thing <> "" Then
Call FIND_IT(Thing, M_What, Skip_What)
End If
Next Thing
End Sub
-Lou
-
Sep 2nd, 2001, 12:34 AM
#35
Thread Starter
Member
i guess this is good too , but i want to be able to
search on any drive , not juts C:\ , and i can't
figure out why a drivelistbox doesn't work , i have
send my program and the demo from Chrisjk to
several people and they all say it don't work with
a drivelistbox.
but anyhow , thanks for trying to help me out
-
Sep 2nd, 2001, 12:51 AM
#36
Thread Starter
Member
btw notLKH , very nice code , and very fast ! thanks i'm gonna
save this , might be of use someday
again if i enter a textbox and change the code to this
Start_Where = Text1.Text '''Your initial directory
i can then type in any drive and do a search , if i change it
to this
Start_Where = Drive1.Drive & "\" '''Your initial directory
the drivebox don't work
Last edited by Dutch; Sep 2nd, 2001 at 12:58 AM.
-
Sep 2nd, 2001, 11:08 AM
#37
PowerPoster
Why not do this, so when a drive is selected, it puts it into the textbox (which you can hide)
VB Code:
Private Sub Drive1_Change()
Text1.Text = Drive1.Drive & "\"
End Sub
That's the only solution I can think of.
-
Sep 2nd, 2001, 12:28 PM
#38
Thread Starter
Member
this doesn't work for me , man , do i have a stubborn computer or what ??
-
Sep 2nd, 2001, 01:49 PM
#39
PowerPoster
must be
-
Sep 2nd, 2001, 01:53 PM
#40
Thread Starter
Member
well , i used the "GetDriveTypeA" example from the MS
knowledge base , and it sees all my drive's , i guess
my program doesn't activate the drives , if that is even
possible
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
|