Avatar of FCapo
FCapo
 asked on

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?
.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
AndyAinscow

8/22/2022 - Mon
AndyAinscow

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
it_saige

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-
FCapo

ASKER
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
AndyAinscow

@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)
it_saige

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-
it_saige

@Andy...  It's no problem, I normally blame the coffee (or lack thereof ;) )...

-saige-
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
FCapo

ASKER
is it possible to have multiple paths for it to search through instead of just one?
AndyAinscow

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.
it_saige

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-
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
FCapo

ASKER
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?
ASKER CERTIFIED SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
AndyAinscow

>>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?