CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 8 1234 ... LastLast
Results 1 to 15 of 112
  1. #1
    Join Date
    May 2010
    Posts
    14

    Loop through folders with shell object.

    Hello,

    I have this code that loops through networks folder/subfolders and search all files with plan in its name.

    But it is take a long time to find all files, because of network folder, many subfolders and many files.

    I have heard that it is possible to speed code up with shell object. Can anybody help me to get it to work with shell object?


    Dim fso As FileSystemObject

    Private Sub Command1_Click()

    Dim fld As Folder

    Dim ParentFolder As String

    ParentFolder = "R: \Documents\Plans\"

    ' Set fso = New FileSystemObject

    ' Set fld = fso.GetFolder(""R: \Documents\Plans\")


    Set objShell = CreateObject("Shell.Application")

    Set fld = objShell.Namespace(ParentFolder)


    RecursiveSearch fld, "plan.doc"


    Set fld = Nothing

    Set fso = Nothing

    End Sub

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

    Re: Loop through folders with shell object.

    This uses RECURSION, and is FAST. The network permission problems might be slowing your down. Try MAPPING the drive first.

    This will load all .JPG file names (in the \TEMP folder) into a listbox.

    Code:
    Option Explicit
    ' posted by Wokawidget
    
    Private Sub cmdSearch_Click()
      Dim colFiles    As Collection
      Dim objFile     As File
      Dim lngIndex    As Long
      Screen.MousePointer = vbHourglass
      DoEvents
      Set colFiles = New Collection
      SearchFolders "D:\temp\", "*.jpg", True, colFiles
      For lngIndex = 1 To colFiles.Count
          Set objFile = colFiles.Item(lngIndex)
          lstfiles.AddItem objFile.ParentFolder & "\" & objFile.Name
      Next lngIndex
      Screen.MousePointer = vbDefault
    End Sub
    
    Public Sub SearchFolders(ByVal pstrFolder As String, ByVal pstrFileSearch As String, ByVal pblnSearchSubFolders As Boolean, ByRef pcolFiles As Collection)
      Dim objFolder           As Folder
      Dim objSubFolder        As Folder
      Dim objSubFolders       As Folders
      Dim objFile             As File
      Dim objFiles            As Files
      Dim objFSO              As FileSystemObject
      Set objFSO = New FileSystemObject
      If objFSO.FolderExists(pstrFolder) Then
        Set objFolder = objFSO.GetFolder(pstrFolder)
        Set objFiles = objFolder.Files
        For Each objFile In objFiles
            If objFile.Name Like pstrFileSearch Then
                pcolFiles.Add objFile
            End If
        Next objFile
        Set objFiles = Nothing
        If pblnSearchSubFolders Then
          Set objSubFolders = objFolder.SubFolders
          For Each objSubFolder In objSubFolders
              SearchFolders objSubFolder.ParentFolder & "\" & objSubFolder.Name, pstrFileSearch, pblnSearchSubFolders, pcolFiles
          Next objSubFolder
          Set objSubFolders = Nothing
        End If
        Set objFolder = Nothing
      End If
      Set objFSO = Nothing
    End Sub
    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!

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

    Re: Loop through folders with shell object.

    Like it a little bit shorter?
    Code:
    Private Sub Command1_Click()
    
      Dim ParentFolder As String, fld As Folder, fso As FileSystemObject
      ParentFolder = "c:\Test"
      Set fso = New FileSystemObject
      Set fld = fso.GetFolder("c:\test")
      RecursiveSearch fld, "*.*"
    End Sub
    
    Private Sub RecursiveSearch(Fold As Folder, FileName As String)
    'will add all occurrences of FileName to a listbox List1
      Dim fil As File, fld As Folder
      For Each fil In Fold.Files
        If fil.Name Like FileName Then List1.AddItem fil.Path
      Next
      For Each fld In Fold.SubFolders
        RecursiveSearch fld, FileName
      Next
    End Sub

  4. #4
    Join Date
    May 2010
    Posts
    14

    Re: Loop through folders with shell object.

    Why I get a error "object required" at the line List1.AddItem fil.Path

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

    Re: Loop through folders with shell object.

    Maybe because I tested mine?

    Code:
    lstfiles.AddItem objFile.ParentFolder & "\" & objFile.Name
    Your form needs a Listbox control called lstfiles for mine
    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!

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

    Re: Loop through folders with shell object.

    Yes. Mine would require a ListBox named List1
    But you can change the line nto: If fil.Name Like FileName Then Debug.Print fil.Path
    It depends onwhat you want to do with the found filename.

  7. #7
    Join Date
    May 2010
    Posts
    14

    Re: Loop through folders with shell object.

    It is working but it is very slowly... Because of many subfolders and many files.
    Is there any way to speed up this code?

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

    Re: Loop through folders with shell object.

    Maybe you will find something useful on this page
    http://vbnet.mvps.org/index.html?cod...api/index.html
    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Apr 2009
    Posts
    394

    Re: Loop through folders with shell object.

    Yeah, datamiser has it... BTW: Wof, D, the fso is the slowest possible way to do a search, even the drive, dir, and file list boxes are faster (when they are not visible and just barely slower when they are only part of the time...)...

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

    Re: Loop through folders with shell object.

    I kind of suspected that it may be a bit slow. I have never used it in any real VB code so could not be sure. I have used it in scripting and in embedded VB only then because the file methods I would normally use are not available.
    Always use [code][/code] tags when posting code.

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

    Re: Loop through folders with shell object.

    Well, everything runs fast on this machine. Probably no difference for 500gb
    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!

  12. #12
    Join Date
    May 2010
    Posts
    14

    Re: Loop through folders with shell object.

    I find out that if I want to have extremely fast routine then I must to have FindFirstFile and FindNextFile API solution or solution with Dir.
    With the first one I can get 7400 files for 1.22 seconds. It is really fast!

    Has anybody some suggestion about this to solution?

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

    Re: Loop through folders with shell object.

    Quote Originally Posted by Derend View Post
    I find out that if I want to have extremely fast routine then I must to have FindFirstFile and FindNextFile API solution or solution with Dir.
    With the first one I can get 7400 files for 1.22 seconds. It is really fast!

    Has anybody some suggestion about this to solution?
    Did you see the link I posted? It has lots of file API stuff
    Always use [code][/code] tags when posting code.

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

    Re: Loop through folders with shell object.

    I'm aware that FSO is the slowest of available methods for that, but it allows for short and comprehensive code.

    I once made this little program to compare API, VB Dir$ and FSO. It contains all three methods, where I took the FindFilesAPI() from allapi.net

    You have to enter your own path in the txtPath textbox. It is hardcoded. It is only to compare speeds.
    After clicking one of the buttons it takes a while to fill the listbox.
    When done the bottom left textfield shows the elapsed time in milliseconds.
    To measure the runtime I make use of a little dll (GetTime.dll) a friend of mine wrote for me in C++. It is not for distribution, please.

    The program shows how API recursive walk through folders is done and should help understand and rewrite for own purposes.
    Attached Files Attached Files

  15. #15
    Join Date
    May 2010
    Posts
    14

    Re: Loop through folders with shell object.

    Thanks guys for comments!

    I found many usefull examples on that page http://vbnet.mvps.org/index.html?cod...api/index.html

    But I could not find the right exampel that search files through folder and subfolders.

    The most example search a specified folder.

Page 1 of 8 1234 ... 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