Recursive File Search Task

In this task you will create a simple application in VB.NET that will search an unknown directory structure for all occurrences of a particular file, whose name is specified by the user.

Before you write the application, you should experiment with some of the techniques you will need.

Iterate through an array with For Each

Duplicate and test the code below.  This declares and initialises an array, then uses a For Each construct to scan the array, visiting each item in turn.

Dim myarray(4) As Integer

myarray(0) = 2
myarray(1) = 4
myarray(2) = 6
myarray(3) = 8
myarray(4) = 10
 
Dim n As Integer
 
For Each n In myarray
    MsgBox(n)
Next

       

Extracting a file name from a path

The code below show demonstrates the use of a number of string handling facilities.  The Substring method returns all of the characters within a string from a specified position.  The Length property of a string returns the number of characters in the string.  The InStrRev function returns the position of the first occurrence of a specified character starting from the right.  Duplicate this code and experiment with it.  Then combine these techniques to extract the file name from a path based on the position of the last path separator.  For example you should be able to output just the file name at the end of any path.

Dim st As String = "H:\folder1\subfolder\myfile.doc"
MsgBox(st.Substring(10))
MsgBox(st.Length)
MsgBox(InStrRev(st, "\"))

     

Make a list of all the files in a folder

The code below generates a list of all the files within a folder (not including the subfolders).  To begin with, manually create a folder inside your home drive called “myfiles”.  The path to your new folder will be H:\myfiles, where H: is the drive letter of your home drive.  Now copy a few files into this folder.
In a VB.NET Windows Forms Application, write a program to create a list of the files in a folder.  To make the necessary file handling commands available, you must first type the following command at the very top of the module above the line Public Class Form1. (The need for this command is explained in detail elsewhere.)

Imports System.IO

Now declare a string variable called stOut with form level scope.

Dim stOut As String

Write this program which takes one parameter, a path as a string.  It then scans this path for the files it contains.  Notice the use of the convenient For Each construct.

Sub ScanFiles(ByVal stPath As String)
 
    Dim stFiles() As String = Directory.GetFiles(stPath)
 
    'Add the files to the output string
    stOut = ""
    For Each stFile As String In stFiles
        stOut = stOut & stFile & vbNewLine
    Next
 
   MsgBox(stOut)
 
End Sub

Now write another program to call this one when a button on the form is clicked.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 
    Call ScanFiles("H:\myfiles")
 
End Sub

You will now write a program to scan a specified path for subfolders.  First, manually create a few new folders inside your myfiles folder, and create a few sub folders inside some of these.  Then copy a few files into some of these folders.  You should have a directory structure something like this with various files contained within.

folders

Now write the program to scan the specified path for sub folders as follows:

Sub ScanFolders(ByVal stPath As String) 
    Dim stFolders() As String = Directory.GetDirectories(stPath)
 
    'Add the files to the output string
    For Each stFolder As String In stFolders
        stOut = stOut & stFolder & vbNewLine
    Next
 
    MsgBox(stOut)
 
End Sub

Test this program too by calling it on a button press and passing it the path of myfiles.

Now try to put these together by writing a program to list all of the files within a folder, including all of the files within all of its sub folders.  You should take a recursive approach.  Here is an algorithm:

ScanFolder(….)

For each file in the folder
    ‘code to process the file here
Next file

For each sub-folder in the folder
    ScanFolder()
Next sub-folder

End

When your program is working, modify it to search for a particular file and report its path if found.