How to: Copy Files with a Specific Pattern to a Directory in Visual Basic
The My.Computer.FileSystem.GetFiles Method returns a read-only collection of strings representing the path names for the files. You can use the wildCards parameter to specify a specific pattern.
An empty collection is returned if no matching files are found.
You can use the My.Computer.FileSystem.CopyFile Method to copy the files to a directory.
To copy files with a specific pattern to a directory
Use the GetFiles method to return the list of files. This example returns all .rtf files in the specified directory.
Use the CopyFile method to copy the files. This example copies the files to the directory named testdirectory.
Close the For statement with a Next statement.
The following example, which presents the above snippets in complete form, copies all .rtf files in the specified directory to the directory named testdirectory.
The following conditions may cause an exception:
The path is not valid for one of the following reasons: it is a zero-length string, it contains only white space, it contains invalid characters, or it is a device path (starts with \\.\) (ArgumentException).
The path is not valid because it is Nothing (ArgumentNullException).
The directory does not exist (DirectoryNotFoundException).
The directory points to an existing file (IOException).
The path exceeds the system-defined maximum length (PathTooLongException).
A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).