-
Dec 3rd, 2015, 03:13 AM
#1
Thread Starter
Lively Member
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.
-
Dec 4th, 2015, 11:35 AM
#2
New Member
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
-
Dec 4th, 2015, 11:36 AM
#3
New Member
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
-
Dec 4th, 2015, 02:30 PM
#4
Re: find a certain folders and subfolders.
Originally Posted by Emeriks
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:
#Region " DirectoryExists "
Private Function DirectoryExists(Path As String, FolderName As String)
If Not Directory.Exists(Path) Then Return False
Return DoDir(Path, FolderName.ToLower)
End Function
Private Function DoDir(src As String, DirName As String) As Boolean
'DirName should be passed in all lower case
Try
Dim Entries() As String = Directory.GetFileSystemEntries(src)
For Each element As String In Entries
If Directory.Exists(element) Then
If element.ToLower.EndsWith(DirName) Then Return True
Call DoDir(element, DirName)
End If
Next element
Catch ex As Exception
MessageBox.Show(ex.ToString, "DoDir")
End Try
'Nothing was found, or an error occured
Return False
End Function
#End Region
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|