Page 1 of 2 12 LastLast
Results 1 to 40 of 67

Thread: find files -> alter code ? chrisjk

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54

    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

  2. #2
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943
    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

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    so i noticed , guess u don't know how to make that code search for 1 exact file huh

  4. #4
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    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

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    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 ?

  6. #6
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Make these changes then
    VB Code:
    1. Sub Command1_Click()
    2.     Dim SearchPath As String, FindStr As String
    3.     Dim FileSize As Long
    4.     Dim NumFiles As Integer, NumDirs As Integer
    5.     Screen.MousePointer = vbHourglass
    6.     List1.Clear
    7.     [b]SearchPath = "C:\"[/b]
    8.     [b]FindStr = "*.mp3"[/b]
    9.     FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
    10.     Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
    11.     Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
    12.     Screen.MousePointer = vbDefault
    13. End Sub
    As for ignoring the recycle in, this is the modified FindFiles function
    VB Code:
    1. Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
    2.  
    3.     Dim FileName As String ' Walking filename variable...
    4.     Dim DirName As String ' SubDirectory Name
    5.     Dim dirNames() As String ' Buffer for directory name entries
    6.     Dim nDir As Integer ' Number of directories in this path
    7.     Dim i As Integer ' For-loop counter...
    8.     Dim hSearch As Long ' Search Handle
    9.     Dim WFD As WIN32_FIND_DATA
    10.     Dim Cont As Integer
    11.     If Right(path, 1) <> "\" Then path = path & "\"
    12.     ' Search for subdirectories.
    13.     nDir = 0
    14.     ReDim dirNames(nDir)
    15.     Cont = True
    16.     hSearch = FindFirstFile(path & "*", WFD)
    17.     If hSearch <> INVALID_HANDLE_VALUE Then
    18.         Do While Cont
    19.         DirName = StripNulls(WFD.cFileName)
    20.         ' Ignore the current and encompassing directories.
    21.         If (DirName <> ".") And (DirName <> "..") And InStr(1, DirName, "RECYCLE") < 1 Then
    22.             ' Check for directory with bitwise comparison.
    23.             If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
    24.                 dirNames(nDir) = DirName
    25.                 DirCount = DirCount + 1
    26.                 nDir = nDir + 1
    27.                 ReDim Preserve dirNames(nDir)
    28.             End If
    29.         End If
    30.         Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
    31.         Loop
    32.         Cont = FindClose(hSearch)
    33.     End If
    34.     ' Walk through this directory and sum file sizes.
    35.     hSearch = FindFirstFile(path & SearchStr, WFD)
    36.     Cont = True
    37.     If hSearch <> INVALID_HANDLE_VALUE Then
    38.         While Cont
    39.             FileName = StripNulls(WFD.cFileName)
    40.             If (FileName <> ".") And (FileName <> "..") Then
    41.                 FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
    42.                 FileCount = FileCount + 1
    43.                 List1.AddItem path & FileName
    44.             End If
    45.             Cont = FindNextFile(hSearch, WFD) ' Get next file
    46.         Wend
    47.         Cont = FindClose(hSearch)
    48.     End If
    49.     ' If there are sub-directories...
    50.     If nDir > 0 Then
    51.         ' Recursively walk into them...
    52.         For i = 0 To nDir - 1
    53.             FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
    54.         Next i
    55.     End If
    56. End Function

  7. #7
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    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.

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    works like a charm thanks ,,

  9. #9
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    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.

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    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.

  11. #11
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    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:
    1. SearchPath = Drive1.Drive

  12. #12

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    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

  13. #13
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Sorry, it should be this
    VB Code:
    1. Drive1.Drive & "\"

  14. #14

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    doesn't work , strange huh

  15. #15
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943
    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

  16. #16

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    it doesn't return anything , not even a error

  17. #17
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943
    ok, do msgbox drive1.drive and see what it says, or paste this code in
    VB Code:
    1. debug.? drive1.drive
    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

  18. #18

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    with ? drive1.drive it says compile error

  19. #19
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943
    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

  20. #20

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    still a compile error "expected expression"

  21. #21
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943
    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

  22. #22

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    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

  23. #23

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    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.

  24. #24
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Here is a tested working demo

  25. #25

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    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.

  26. #26

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    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.

  27. #27
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943
    try MP3 maybe
    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

  28. #28
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    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.

  29. #29

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    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.

  30. #30
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    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.

  31. #31

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    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.

  32. #32
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    I tested it on a Win98SE system, works like a charm

  33. #33

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    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:\

  34. #34
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Hmm,

    Does this help any?


    VB Code:
    1. Private Sub Command1_Click()
    2. Dim Find_What As String
    3. Dim Start_Where As String
    4. Dim SKIP_THIS As String
    5. SKIP_THIS = "C:\RECYCLED" '''Your Recycle bin
    6. Find_What = "*.mp3"  '''What you want to find
    7. Start_Where = "c:\"   '''Your initial directory
    8. List1.Clear
    9. Call FIND_IT(Start_Where, Find_What, SKIP_THIS)
    10. End Sub
    11.  
    12.  
    13. Private Sub FIND_IT(ByVal M_Where As String, ByVal M_What As String, ByVal Skip_What As String)
    14. Dim MY_DIRS() As String
    15. Dim MY_COUNT As Integer '
    16. MY_COUNT = 0
    17. ReDim MY_DIRS(1)
    18.  
    19. If Right(M_Where, 1) <> "\" Then
    20.     M_Where = M_Where & "\"
    21. End If
    22.  
    23. doiexist = Dir(M_Where & "*.*", vbDirectory)
    24. While doiexist <> ""
    25.     If doiexist <> "." And doiexist <> ".." Then
    26.         If (GetAttr(M_Where & doiexist) And vbDirectory) = vbDirectory Then
    27.             MY_COUNT = MY_COUNT + 1
    28.             ReDim Preserve MY_DIRS(MY_COUNT)
    29.             MY_DIRS(MY_COUNT) = M_Where & doiexist
    30.             Form1.Caption = MY_COUNT
    31.             DoEvents
    32.         End If
    33.     End If
    34.     doiexist = Dir
    35. Wend
    36. If Left(M_Where, Len(Skip_What) + 1) <> Skip_What & "\" Then
    37.     doiexist = Dir(M_Where & M_What, vbNormal)
    38.     While doiexist <> ""
    39.         List1.AddItem M_Where & doiexist
    40.         doiexist = Dir
    41.     Wend
    42. End If
    43. For Each Thing In MY_DIRS
    44.     If Thing <> "" Then
    45.         Call FIND_IT(Thing, M_What, Skip_What)
    46.     End If
    47. Next Thing
    48. End Sub

    -Lou

  35. #35

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    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

  36. #36

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    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.

  37. #37
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Why not do this, so when a drive is selected, it puts it into the textbox (which you can hide)
    VB Code:
    1. Private Sub Drive1_Change()
    2.  
    3. Text1.Text = Drive1.Drive & "\"
    4.  
    5. End Sub
    That's the only solution I can think of.

  38. #38

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    this doesn't work for me , man , do i have a stubborn computer or what ??

  39. #39
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    must be

  40. #40

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    54
    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

Page 1 of 2 12 LastLast

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