Recipe 12.9. Finding Directories and Files Using Wildcards


Problem

You need to generate a list of all files and subdirectories in a specific parent directory that have names matching a designated pattern.

Solution

Sample code folder: Chapter 12\UsingWildcards

Use the wildcard features of the My.Computer.FileSystem. GetFiles() and My. Computer.FileSystem.GetDirectories() methods to retrieve the matching file and directory names.

Discussion

This recipe's sample code fills in a ListBox control with all matching directories and files of a specified base path, based on a pattern.

Begin a new Windows Forms project, and add two TextBox controls named StartPath and PathPattern, a Button control named ActMatch, and a ListBox control named MatchResults to Form1. You can add labels and provide meaningful captions if you wish, as is done in Figure 12-5.

Figure 12-5. Controls for the name-matching sample


Now add the following source code to the form's class template:

 Private Sub ActMatch_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles ActMatch.Click    ' ----- Make sure the supplied path is valid.    If (My.Computer.  FileSystem.DirectoryExists( _          StartPath.Text) = False) Then       MsgBox("Please supply a valid directory path.", _          MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, _          "Invalid Path")       Exit Sub    End If    ' ----- Clear any previous items.    MatchResults.Items.Clear( )    ' ----- First, add in the subdirectories.    For Each oneItem As String In _         My.Computer.FileSystem.GetDirectories( _         StartPath.Text, _         FileIO.SearchOption.SearchTopLevelOnly, _         PathPattern.Text)      MatchResults.Items.Add("[" & _         My.Computer.FileSystem.GetName(oneItem) & "]")    Next oneItem    ' ----- Second, add in the files.    For Each oneItem As String In _          My.Computer.FileSystem.GetFiles(StartPath.Text, _          FileIO.SearchOption.SearchTopLevelOnly, _          PathPattern.Text)       MatchResults.Items.Add( _           My.Computer.FileSystem.GetName(oneItem))    Next oneItem End Sub 

To use the program, type a valid directory path into the StartPath field, type a pattern (such as "*.txt") in the PathPattern field, and then click ActMatch to build the list of matching file and directory names. Figure 12-6 shows this form in use, listing files matching the "*.log" pattern.

Figure 12-6. Displaying files matching a wildcard pattern


The My.Computer.FileSystem.GetFiles( ) and parallel Getdirectories( ) methods normally return a list of all files or directories in a specified parent path:

 ' ----- Return all files in C:\Windows For Each oneFile As String In _    My.Computer.FileSystem.GetFiles("C:\Windows") 

However, both methods allow you to pass one or more "wildcard" pattern strings to limit the return list to just those items that match the pattern(s):

 ' ----- Return all "LOG" files in C:\Windows For Each oneFile As String In _    My.Computer.FileSystem.GetFiles("C:\Windows", _    FileIO.SearchOption.SearchTopLevelOnly, _    "*.LOG") 

The syntax is identical for Getdirectories(), but it returns a list of matching directories instead of files. The second argument indicates the depth to search for name matches. FileIO.SearchOption.SearchTopLevelOnly returns only matches found directly within the specified parent path. To include all subdirectories, use the FileIO.SearchOption.SearchAllSubDirectories value instead.

The third wildcard argument accepts any string that includes zero or more wildcard characters. The "*" wildcard matches zero or more characters at the position where it appears. The " ?" wildcard matches exactly one character at the position where it appears.

If you need to simultaneously match more than one pattern and return all files (or directories) that match any of the patterns, include each pattern as a separate argument:

 ' ----- Return all "LOG" and "TXT"   files in C:\Windows For Each oneFile As String In _    My.Computer.FileSystem.GetFiles("C:\Windows", _    FileIO.SearchOption.SearchTopLevelOnly, _    "*.LOG", "*.TXT") 

See Also

Recipe 12.8 looks at how to recursively traverse a directory tree and identify all subdirectory names.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net