Results 1 to 4 of 4

Thread: find a certain folders and subfolders.

  1. #1

    Thread Starter
    Lively Member elmnas's Avatar
    Join Date
    Jul 2009
    Posts
    127

    find a certain folders and subfolders.

    I have made a program that collects all used cell-values in column "A" from an excelfile then merge each cell as own row in a datasource.
    (each cell value presents a foldername)

    the next step is to find the folder in a hierarchy with subfolders then open the folder.

    How do I search for a folder with name "test1123" in the path C:\data\
    I want the function to search through all subfolders too then when found open the folder.

    could someone help me?


    Thank you in advance.
    Last edited by elmnas; Dec 3rd, 2015 at 09:00 AM.

  2. #2
    New Member
    Join Date
    Dec 2015
    Posts
    4

    Re: find a certain folders and subfolders.

    something like this ?

    For Each Folder As String in Directory.GetDirectories("C:\data", "test123", SearchOption.AllDirectories)
    Process.Start(Folder)
    Next
    Emeriks

  3. #3
    New Member
    Join Date
    Dec 2015
    Posts
    4

    Re: find a certain folders and subfolders.

    Code:
    For Each Folder As String in Directory.GetDirectories("C:\data", "test123", SearchOption.AllDirectories)
     Process.Start(Folder)
    Next

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: find a certain folders and subfolders.

    Quote Originally Posted by Emeriks View Post
    Code:
    For Each Folder As String in Directory.GetDirectories("C:\data", "test123", SearchOption.AllDirectories)
     Process.Start(Folder)
    Next
    The only issue with that is if there's any issue with any of the folders (or sub folders) then the whole function fails. For example if you don't have permission to a folder, then the function fails with an exception. So long as you don't have that scenario that code will work just fine, however if it could happen (and it happens a lot, in my experience) then elmnas will probably want to write a function that traverses the folder tree and can omit any that cause problems.
    To do that he'll need to understand a concept called recursion.
    Here's an example:
    vb.net Code:
    1. #Region " DirectoryExists "
    2.  
    3.     Private Function DirectoryExists(Path As String, FolderName As String)
    4.         If Not Directory.Exists(Path) Then Return False
    5.         Return DoDir(Path, FolderName.ToLower)
    6.     End Function
    7.  
    8.     Private Function DoDir(src As String, DirName As String) As Boolean
    9.         'DirName should be passed in all lower case
    10.         Try
    11.             Dim Entries() As String = Directory.GetFileSystemEntries(src)
    12.             For Each element As String In Entries
    13.                 If Directory.Exists(element) Then
    14.                     If element.ToLower.EndsWith(DirName) Then Return True
    15.                     Call DoDir(element, DirName)
    16.                 End If
    17.             Next element
    18.         Catch ex As Exception
    19.             MessageBox.Show(ex.ToString, "DoDir")
    20.         End Try
    21.  
    22.         'Nothing was found, or an error occured
    23.         Return False
    24.     End Function
    25.  
    26. #End Region
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Tags for this Thread

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