Need help with searching files in VB .NET

jango_fett

Member
Joined
Jun 15, 2005
Messages
23
Programming Experience
Beginner
Need help with searching files in VB .NET - [RESOLVED]

Hi, there. I'd be greatly relieved if someone could tell how can I program VB .NET to find a file within a specified directory. The search in question would have to extend to any subdirectories that the specified directory may contain.
Thanks.
 
Last edited:
You need to write a recursive function something like this:
VB.NET:
[color=Green]	''' -----------------------------------------------------------------------------
 	''' <summary>
 	''' Determines whether the specified file exists in the specified folder or any subfolder.
 	''' </summary>
 	''' <param name="folderName">
 	''' The fully qualified path of the root folder to search, e.g. "C:\MyFolder".
 	''' </param>
 	''' <param name="fileName">
 	''' The name of the file to search for, e.g. "MyFile.txt"
 	''' </param>
 	''' <returns>
 	''' True if the file is found;
 	''' False otherwise.
 	''' </returns>
 	''' -----------------------------------------------------------------------------
  [/color]   [color=Blue]Private Function[/color] IsFileInFolder([color=Blue]ByVal[/color] folderName [color=Blue]As String[/color], [color=Blue]ByVal[/color] fileName [color=Blue]As String[/color]) [color=Blue]As Boolean[/color]
		[color=Blue]If[/color] IO.File.Exists(folderName & IO.Path.DirectorySeparatorChar & fileName) [color=Blue]Then[/color]
			[color=Green]'The file exists in this folder.[/color]
			[color=Blue]Return True[/color]
		[color=Blue]End If[/color]

		[color=Green]'Get an array of subfolders.[/color]
		[color=Blue]Dim[/color] subfolderNames [color=Blue]As String[/color]() = IO.Directory.GetDirectories(folderName)

		[color=Green]'Search each subfolder.[/color]
		[color=Blue]For Each[/color] subfolderName [color=Blue]As String In[/color] subfolderNames
			[color=Blue]If Me[/color].IsFileInFolder(subfolderName, fileName) [color=Blue]Then[/color]
			    [color=Green]'The specified subfolder, or one of its subfolders, contains the file.[/color]
				[color=Blue]Return True[/color]
			[color=Blue]End If[/color]
		[color=Blue]Next[/color]

		[color=Green]'The file was not found.[/color]
		[color=Blue]Return False[/color]
	[color=Blue]End Function[/color]
If you aren't familiar with recursion, notice that this function calls itself. In this way it follows the first branch all the way to its end, then it returns back up the tree until it finds another branch, which it then follows to its end. In this way it will eventually traverse all of even the largest tree structure.
 
Thanks a lot, dude

Hi, there. I just wanted to say thanks for your piece of code. It works perfectly. I guess I need to take recursion more seriously because your function is way shorter than the one I was writing. Again, thanks. I owe you one.
 
jango_fett said:
Hi, there. I just wanted to say thanks for your piece of code. It works perfectly. I guess I need to take recursion more seriously because your function is way shorter than the one I was writing. Again, thanks. I owe you one.
Thanks for the thanks! On the subject of recursion, it is best avoided in most cases but there are certain situations where it shines, and traversing a tree structure is definitely one of those situations.
 
Back
Top