Reputation: 95
I have the following code but I need to adjust. I want the user to select a particular folder from within a project. Imagine the path "C:\Project\SomeOtherFolder\WINDOW". The below code only fills the text box if the have selected the "WINDOW" folder. I'm just using this as a check for the user, but I actually want the text box to fill with "Project".
Using fb As New FolderBrowserDialog
If fb.ShowDialog = Windows.Forms.DialogResult.OK AndAlso _
(IO.Path.GetFileName(fb.SelectedPath) = "WINDOW") Then
TextBox1.Text = IO.Path.GetFileName(fb.SelectedPath)
Else
Exit Sub
End If
End Using
How can I accomplish this please? Many Thanks!!!
Upvotes: 0
Views: 833
Reputation: 5809
This UDF, should give you what you need. I have created the function to return the name of the folder up from a specific folder location. I have included some optional parameters so you could (if required) change the requirement.
Public Function GetFolderName(FolderPath As String, _
Optional endPath As String = "WINDOW", _
Optional moveUp As Integer = 2) As String
Dim tmpArr() As String, retStr As String
tmpArr = Split(FolderPath, "\")
If InStr(FolderPath, endPath) <> 0 And moveUp <= UBound(tmpArr) Then
retStr = tmpArr(UBound(tmpArr) - moveUp)
End If
GetFolderName = retStr
End Function
So the code walk through. You send in the Path you obtain in the previous step and then you simply call the function as,
TextBox1.Text = GetFolderName(fb.SelectedPath)
'Or - However this is redundant as the Optional Parameters are declared as such by default
TextBox1.Text = GetFolderName(fb.SelectedPath, "WINDOW", 2)
The above would populate your text box as "Project". Hope this helps !
Upvotes: 1