CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 8 FirstFirst 12345 ... LastLast
Results 16 to 30 of 112
  1. #16
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Loop through folders with shell object.

    dir *.txt /s
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  2. #17
    Join Date
    Apr 2009
    Posts
    394

    Re: Loop through folders with shell object.

    Look for recursive or recursion at that link... as there are several different examples on how to search an entire drive or a subset of folders and sub folders for files...

    (Yeah D, I've used that one before but I do something like dir C:\a *.txt /b /s >c:\output.txt)



    Good Luck

  3. #18
    Join Date
    May 2010
    Posts
    14

    Re: Loop through folders with shell object.

    Hello again,

    I have spent a little time to check some examples on that link http://vbnet.mvps.org/index.html?cod...api/index.html
    I have tested this examples, but it seems like none of them search subfolders.

    FindFirstFile: Recursive File Search for Single or Multiple File Types (minimal code)
    FindFirstFile: Recursive File Search Including/Excluding Single or Multiple File Types (minimal code)
    FindFirstFile: Recursive Search for Folders Using a Folder Mask (minimal code)
    FindFirstFile: Recursive File Search (minimal code)
    FindFirstFile: Recursive Search for Folders (minimal code)

    Is it me that see it wrong or it really is like this?

    Can anybody help with this?

  4. #19
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Loop through folders with shell object.

    Have you considered that you may need to combine 2 of these to get your results?

    Keep in mind I have not looked through these but if as you say they search for files in a single folder then you may need to use one to search for sub directories and then another to search for files in each of those sub directories.
    Always use [code][/code] tags when posting code.

  5. #20
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Loop through folders with shell object.

    btw that little method shown by vb5prgrmr is fairly quick and not to difficult to use. It will generate a file that has all the results in it. You would just need to parse out that file when it completes.
    Always use [code][/code] tags when posting code.

  6. #21
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Loop through folders with shell object.

    I don't know what you actually want, Derend.
    If you look at my sample project, it shows three methods to RECURSIVELY list all contents of a folder and its subfolders.
    For easy comprehension I have snapped together the API code to a short and rather fast procedure. It assumes a ListBox named lstFiles to accept and display the entries.
    Code:
    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 FILE_ATTRIBUTE_DIRECTORY = &H10
    
    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
    
    Private Sub ListFilesAPI(ByVal StartingPath As String, ByVal Pattern As String)
      Dim CurName As String, CurFull As String
      Dim Dirs As New Collection
      Dim hSearch As Long
      Dim WFD As WIN32_FIND_DATA
      Dim i%, cont%
      
      If Right$(StartingPath, 1) <> "\" Then StartingPath = StartingPath + "\"
      
      'walk through all entries to find the folders
      cont = 1
      hSearch = FindFirstFile(StartingPath + "*", WFD)
      If hSearch <> INVALID_HANDLE_VALUE Then
         Do While cont
            CurName = StripNulls(WFD.cFileName)
            If CurName <> "." And CurName <> ".." Then
               CurFull = StartingPath + CurName
               If GetFileAttributes(CurFull) And FILE_ATTRIBUTE_DIRECTORY Then Dirs.Add CurName
            End If
            cont = FindNextFile(hSearch, WFD)
         Loop
         cont = FindClose(hSearch)
      End If
      
      'now walk again through all entries finding files that match the search pattern
      cont = 1
      hSearch = FindFirstFile(StartingPath + Pattern, WFD)
      If hSearch <> INVALID_HANDLE_VALUE Then
         Do While cont
            CurName = StripNulls(WFD.cFileName)
            If CurName <> "." And CurName <> ".." Then lstFiles.AddItem StartingPath + CurName
            cont = FindNextFile(hSearch, WFD)
         Loop
         cont = FindClose(hSearch)
      End If
      
      'now recursively call self for all subfolders
      For i = 1 To Dirs.Count
          ListFilesAPI StartingPath + Dirs(i), Pattern
      Next
    End Sub

  7. #22
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Loop through folders with shell object.

    If you like variations on a theme, here is another little example I worked out.
    It seems to be even faster than API code using Dir$(), although it does not feature pattern matching.
    It simply lists all files in the folder and its subfolders.
    Code:
    Private Sub ListAllFilesDIR(ByVal StartingFolder As String)
      Dim CurDir As String
      Dim CurName As String
      Dim Dirs As New Collection
      Dim FullName As String
      
      If Right$(StartingFolder, 1) <> "\" Then StartingFolder = StartingFolder + "\"
      
      Dirs.Add StartingFolder
      
      While Dirs.Count
        CurDir = Dirs(1)
        Dirs.Remove 1
        CurName = Dir$(CurDir, vbDirectory)
        While CurName <> ""
           If CurName <> "." And CurName <> ".." Then
              FullName = CurDir + CurName
              If GetAttr(FullName) And vbDirectory Then Dirs.Add FullName + "\"
              lstFiles.AddItem FullName
           End If
           CurName = Dir$
        Wend
      Wend
    End Sub
    Again it will list files in a ListBox named lstFiles.
    This is a rather short, comprehensive and elegant way which does not even do recursive calling, although it still walks through all subfolders.
    For pattern matching we'd have to do it a little different.
    Last edited by WoF; May 17th, 2010 at 01:05 PM.

  8. #23
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Loop through folders with shell object.

    Would be a bit faster if you set the lstfiles.visible=false at the top and true at the bottom
    Always use [code][/code] tags when posting code.

  9. #24
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Loop through folders with shell object.

    Powershell
    Code:
    # PowerShell script to find executable in the Windows folder
    $Path = "C:\windows"
    $wind =get-Childitem $Path -recurse | where{$_.Extension -match "exe"}
    $wind
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #25
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Loop through folders with shell object.

    @DataMiser: that's true. It will be a leeettle faster then.

    @David: Hem, hem... Ys thys VB6 code? You seem to be yn the wrong forum.

    BTW.: I'm a little amazed that the Dir$() version is almost as fast as the API calls. I say almost, since if I'd have to modify the routine for pattern matching we'd have to make double passes through the folders and thus being a little slower than API, I guess.
    Last edited by WoF; May 17th, 2010 at 01:08 PM.

  11. #26
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Loop through folders with shell object.

    I don't think you would need additional passes just need to compare the text of the fullname to your search pattern before adding it to the list
    Always use [code][/code] tags when posting code.

  12. #27
    Join Date
    May 2010
    Posts
    14

    Re: Loop through folders with shell object.

    Where should I give search path and search file type in your codes WOF?

  13. #28
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Loop through folders with shell object.

    Powershell 2.0 is the new DOS. All the new Microsoft programs understand it.
    It also utilizes the Net framework, under the covers.

    Why NOT use it?

    How to access the Registry:

    c:\Powershell
    PS C:\Users\David> cd hklm:
    PS HKLM:\software> cd \software
    PS HKLM:\software> dir


    Hive: HKEY_LOCAL_MACHINE\software


    SKC VC Name Property
    --- -- ---- --------
    1 0 Acronis {}
    1 0 ASUS {}
    1 0 ATI Technologies {}
    1 0 Audible {}
    1 0 Business Objects {}
    447 0 Classes {}
    7 0 Clients {}
    3 0 Diskeeper Corporation {}
    1 0 Dolby {}
    1 0 Hauppauge {}
    1 0 InstalledOptions {}
    2 0 Intel {}
    2 0 JavaSoft {}
    1 0 Khronos {}
    1 0 MainConceptMCE {}
    4 0 MCCI {}
    188 1 Microsoft
    [removed]
    PS HKLM:\software>
    Last edited by dglienna; May 17th, 2010 at 07:29 PM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  14. #29
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Loop through folders with shell object.

    I think the intention was to do this in a vb6 project, I haven't used powershell for anything as of yet but I would be surprised if it intergrated with VB6. Also I find it hard to believe that it would be any faster or even as fast as doing it through VB code directly. There is a lot of overhead in the .net framework
    Always use [code][/code] tags when posting code.

  15. #30
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Loop through folders with shell object.

    Quote Originally Posted by Derend View Post
    Where should I give search path and search file type in your codes WOF?
    In the first example it would be "StartingPath" and "Pattern"
    The second example "StartingFolder" there is no code there for pattern but could be added with ease.
    Always use [code][/code] tags when posting code.

Page 2 of 8 FirstFirst 12345 ... 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

Featured