|
|
Title | Make a program that uses regular expressions to rename files matching a pattern in Visual Basic .NET |
Description | This example shows how to make a program that uses regular expressions to rename files matching a pattern in Visual Basic .NET. |
Keywords | files, regular expressions, regex, replace, rename, rename files, Visual Basic .NET, VB.NET |
Categories | Software Engineering, Files and Directories |
|
|
To use this program, enter a directory name, file pattern, and regular expression find and replace patterns. When you click Preview Changes, the program searches the directory for files matching the file pattern. For those files, it makes replacements using the regular expressions. If the result is different from the original file's name, it lists the file's original and new file names in a ListVIew.
Next review the list to ensure that it makes sense. Click on a row and click the remove button to remove that file from the list.
When you're satisfied, click the Make Changes button to rename the files.
Note: Be sure to check the list carefully. Don't click the Make Changes button if any file names will cause conflicts. For example, don't give a file a new name that is the same as that of an existing file. (Unless the existing file will be renamed first. You can see that this could get complicated so I didn't try to figure out whether there will be a problem.)
The MakeFileLists method shown in the following code does the most interesting work.
|
|
' Make a list of file names to change from and to.
Private FullFromNames, FromNames, ToNames As List(Of String)
Private Sub MakeFileLists()
Try
' Make the file name lists.
FullFromNames = New List(Of String)()
FromNames = New List(Of String)()
ToNames = New List(Of String)()
' Get the files that match the pattern.
Dim dir_info As New DirectoryInfo(txtDirectory.Text)
Dim files() As FileInfo = _
dir_info.GetFiles(txtFilePattern.Text)
Dim reg_ex As New Regex(txtOldPattern.Text)
For i As Integer = 0 To files.Length - 1
Dim new_name As String = _
reg_ex.Replace(files(i).Name, _
txtNewPattern.Text)
new_name = new_name.Replace("$i", i.ToString())
If (files(i).Name <> new_name) Then
FullFromNames.Add(files(i).FullName)
FromNames.Add(files(i).Name)
ToNames.Add(new_name)
End If
Next i
Catch ex As Exception
MessageBox.Show("Error building file list.\n" & _
ex.Message)
FullFromNames = New List(Of String)()
FromNames = New List(Of String)()
ToNames = New List(Of String)()
End Try
End Sub
|
|
This method creates lists to hold the names of the files. It uses a DirectoryInfo object's GetFiles method to get a list of the files that match the file pattern. You can change that call to recursively search subdirectories if you like.
The code then loops through those files. For each file, it uses a Regex object to make the regular expression replacements. Regular expressions are quite complex so they're not described here. Perhaps I'll cover them later. Until then, see Microsoft's article .NET Framework Regular Expressions.
Next the code replaces $i with the current file number. That makes it easy to number the files in a directory.
Finally if the new file name is not the same as the original name, the program adds the file to the lists.
See the code for more details.
|
|
|
|
|
|