Hello Daniweb,
I need to write a program to change the extension of all the files in a folder from a certain extension to another.
My code is shown below, however, I have one problem:
i.e.
Say we want to change all files with the extension ".tx" to ".txt" but there are two files
of the same name but one file ends with ".tx" and the other ends with ".txt"
****************************(in the folder: one.txt and one.tx)*******************
Now I have to rename the ".txt" file after it has been changed to the ".txt" file...****
How can I do this correctly and without error?
Public Class Form1.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not My.Computer.FileSystem.DirectoryExists(TextBox1.Text.Trim()) = True Or TextBox1.Text.Trim() = "" Then
MsgBox("Please enter a valid directory.")
Else
Dim files As String()
files = IO.Directory.GetFiles(TextBox1.Text.Trim(), "*.tx")
Dim filepath_new As String
For Each filepath As String In files
filepath_new = filepath.Replace(".tx", ".txt")
If Not My.Computer.FileSystem.FileExists(filepath_new) = True Then
My.Computer.FileSystem.RenameFile(filepath, filepath_new)
Else
Dim iRenamed As Integer = 0
'///////////////////////////////////////////////////////////////////////
Dim iFileCopyNumber As Integer = 1 '// ADDED THIS.
'///////////////////////////////////////////////////////////////////////
With My.Computer.FileSystem
' For Each foundFile As String In .GetFiles(TextBox1.Text.Trim(), FileIO.SearchOption.SearchAllSubDirectories, "*tx")
'// check if File already.Exists in Destination.Folder
'If Not System.IO.File.Exists(TextBox1.Text.Trim() & System.IO.Path.GetFileName(foundFile)) Then
' System.IO.File.Move(filepath, filepath_new)
'Else '// If it Exists in Destination.Folder, .Rename.File
' '///////////////////////////////////////////////////////////////////////
Do Until Not System.IO.File.Exists(TextBox1.Text.Trim() & System.IO.Path.GetFileNameWithoutExtension(filepath) & " - Copy (" & iFileCopyNumber.ToString & ")" & System.IO.Path.GetExtension(filepath))
iFileCopyNumber += 1
Loop
.RenameFile(filepath, System.IO.Path.GetFileNameWithoutExtension(filepath) & " - Copy (" & iFileCopyNumber.ToString & ")" & System.IO.Path.GetExtension(filepath))
iRenamed += 1
'iFileCopyNumber = iFileCopyNumber + 1
'///////////////////////////////////////////////////////////////////////
'End If
' Next\
' My.Computer.FileSystem.RenameFile(filepath, filepath_new)
End With
'Dim count As Integer = 1
'filepath_new = System.IO.Path.GetFileNameWithoutExtension(filepath_new) & count & System.IO.Path.GetExtension(filepath_new)
My.Computer.FileSystem.RenameFile(filepath, filepath_new)
'count = count + 1
End If
'' My.Computer.FileSystem.DeleteFile(filepath
'System.IO.File.Move(filepath, filepath_new)
Next
End If
End Sub
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim path As String = TextBox1.Text.Trim()
If Not My.Computer.FileSystem.DirectoryExists(TextBox1.Text.Trim()) = True Or TextBox1.Text.Trim() = "" Then
MsgBox("Please enter a valid directory.")
Else
For Each foundfile As String In My.Computer.FileSystem.GetFiles _
(path, _
FileIO.SearchOption.SearchTopLevelOnly, "*.tx")
ListBox1.Items.Add(foundfile)
Next
End If
End Sub
End Class
Thanks in advance,
M1234ike