[RESOLVED] Rename files with no extension to .txt
Code:
Dim Files As String() = IO.Directory.GetFiles(f.SelectedPath + "\", "*.*", IO.SearchOption.AllDirectories)
Dim fil As String
For Each fil In Files
If fil.IndexOf(".") = -1 Then
FileSystem.Rename(fil, fil & ".txt")
End If
Next
I'm trying to write some code to rename all the files in the directory and subdirectories. If the file has no extension it needs to add the extension .txt.
Any help appreciated.
Re: Rename files with no extension to .txt
What happens when you try to run your code?
Re: Rename files with no extension to .txt
The System.IO.Path.GetExtension function will get you the extension of a filename that you pass to it. That may be an easier way to check for an extension.
Re: Rename files with no extension to .txt - RESOLVED
Thanks I got it to work correctly now:
Code:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
f.ShowDialog()
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Files As String() = IO.Directory.GetFiles(f.SelectedPath + "\", "*.*", IO.SearchOption.AllDirectories)
Dim fil As String
For Each fil In Files
If System.IO.Path.GetExtension(fil) = "" Then
FileSystem.Rename(fil, fil & ".txt")
End If
Next
End Sub
End Class