Vb.net search directory

Hi,

in vb.net, I have a project number string "BC-230902-1", there is a folder in my PC in a specifc directory that contains that project in its name, such as "Mister Cambpell stan BC-230902-1".

I'd like to from vb.net be able to search through this directory and return the path of that specific folder back to vb.net.

How could I do this?
FCapoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
https://msdn.microsoft.com/en-us/library/ms143316%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

Something like (look at the link for more details)
stringArrayOfMatches = Directory.GetFiles("C:\", "BC-230902-1", SearchOption.AllDirectories)
seems to be pretty much what you want.  That should return all matches as an array of file name AND folder path
0
it_saigeDeveloperCommented:
One correction Andy:
stringArrayOfMatches = Directory.GetFiles("C:\", "*BC-230902-1*", SearchOption.AllDirectories)

Open in new window

Characters other than the wildcard are literal characters. For example, the searchPattern string "*t" searches for all names in path ending with the letter "t". The searchPattern string "s*" searches for all names in path beginning with the letter "s".
Source

-saige-
0
FCapoAuthor Commented:
I'm sorry to be specific but can you lay out the declaration and everything I need to get this to work.

Based on the link I don't really get how to do this ,

Imports System.IO
 Public Shared Function GetFiles(ByVal path As String, ByVal searchPattern As String, ByVal searchOption As SearchOption) As String()

        Directory.GetFiles("C:\", "*BC-230902-1*", searchOption.AllDirectories)

    End Function
0
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

AndyAinscowFreelance programmer / ConsultantCommented:
@it_saige, Thanks.  I'd forgotten to add the wildcard symbols with copy/paste.  :-(

I'm more of a C++ / C# specialist but here goes (change the drive letter if you wanted something other than C):

Imports System
Imports System.IO

...
Dim stringArrayOfMatches() as String = Directory.GetFiles("C:\", "*BC-230902-1*", SearchOption.AllDirectories)
0
it_saigeDeveloperCommented:
Imports System.IO
Module Module1
	Sub Main()
		Dim path = "C:\!quick"
		Dim filter = "*DevComponents*"
		Dim options = SearchOption.AllDirectories
		Dim results = Directory.GetFiles(path, filter, options)
		For Each item In results
			Console.WriteLine(item)
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window

On my system produces the following output -Capture.JPGFor the following directory structure -Capture.JPG-saige-
0
it_saigeDeveloperCommented:
@Andy...  It's no problem, I normally blame the coffee (or lack thereof ;) )...

-saige-
0
FCapoAuthor Commented:
is it possible to have multiple paths for it to search through instead of just one?
0
AndyAinscowFreelance programmer / ConsultantCommented:
Erm, yes.  Just call it multiple times.

ps.  What I posted will check through ALL folders on drive C for that partial file name and return an array of ALL matches.
0
it_saigeDeveloperCommented:
Yes.  You have to use either a list or an array and loop through it; e.g. -
Imports System.IO
Module Module1
	Sub Main()
		Dim paths As New List(Of String) From {"C:\!quick", "C:\inetpub", "C:\_admin"}
		Dim filter = "*DevComponents*"
		Dim options = SearchOption.AllDirectories
		Dim results As New List(Of String)

		For Each path As String In paths
			results.AddRange(Directory.GetFiles(path, filter, options).AsEnumerable())
		Next

		For Each item In results
			Console.WriteLine(item)
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output on my system -Capture.JPG
You just want to make sure that you do not use a folder that is a child folder of another entry as the search option causing the method to recurse through child folders already.

-saige-
0
FCapoAuthor Commented:
I'm 100% sure that there will be only 1 result instead of a list of result, can I make it so it returns only 1 path?
0
it_saigeDeveloperCommented:
Yes you can:
Imports System.IO
Module Module1
	Sub Main()
		Dim paths As New List(Of String) From {"C:\!quick", "C:\inetpub", "C:\_admin"}
		Dim filter = "*DevComponents*"
		Dim options = SearchOption.AllDirectories
		Dim result As String = String.Empty

		For Each path As String In paths
			If String.IsNullOrEmpty(result) Then
				result = Directory.GetFiles(path, filter, options).AsEnumerable().FirstOrDefault()
			End If

			' Because the Directory.GetFiles method is time intensive and you just want one result, we will exit the for loop when we get a good result
			If Not String.IsNullOrEmpty(result) Then Exit For
		Next

		Console.WriteLine(result)
		Console.ReadLine()
	End Sub
End Module

Open in new window

Now produces the following -Capture.JPGAnother way to do it without using a linq statement is -
Imports System.IO
Module Module1
	Sub Main()
		Dim paths As New List(Of String) From {"C:\!quick", "C:\inetpub", "C:\_admin"}
		Dim filter = "*DevComponents*"
		Dim options = SearchOption.AllDirectories
		Dim result As String = String.Empty

		For Each path As String In paths
			If String.IsNullOrEmpty(result) Then
				For Each item In Directory.GetFiles(path, filter, options)
					' We will just assume the first result is the result you want
					result = item
					Exit For
				Next
			End If

			' Because the Directory.GetFiles method is time intensive and you just want one result, we will exit the for loop when we get a good result
			If Not String.IsNullOrEmpty(result) Then Exit For
		Next

		Console.WriteLine(result)
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which produces the same result as above.

-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
>>I'm 100% sure that there will be only 1 result instead of a list of result, can I make it so it returns only 1 path?

That built in function returns an array.  The array will have zero, one or many strings.  
Where is the problem?
Zero entries and nothing matched.
One entry AND that is your file.
More than one entry and something is not right, an event which you have to address.

OK, change it so you only get one entry EVEN IF THERE ARE MORE MATCHES.  What happens if it returns one that is not correct?  You don't know - do you?  Will it be a good result when you use that wrong file - probably not?  Will the end user be happy - probably not?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.