Reputation: 12851
Is There any Function For changing a file extension in .NET? Or i have to rename a file? thanks
For Example I want to rename each file in a directory with ".resxx" extension to .resx. what is the problem with my code?
Dim [option] As SearchOption = SearchOption.AllDirectories [option] = SearchOption.AllDirectories
Dim fileNames As String() = Directory.GetFiles("C:\New Folder", "*.resxx", [option])
For Each f In fileNames
Dim t As New FileInfo(f.ToString)
MsgBox(Mid(f, 1, f.Length - 4))
t.MoveTo(Mid(f, 1, f.Length - 4) + ".resx")
Next
Upvotes: 5
Views: 23672
Reputation: 1
Sub Button1_Click()
' Carl SQL Server Connection
'
' FOR THIS CODE TO WORK
' In VBE you need to go Tools References and check Microsoft Active X Data Objects 2.x library
Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Server_Name = "DAHLIA" ' Enter your server name here
Database_Name = "TestDB" ' Enter your database name here
'User_ID = "" ' enter your user ID here
'Password = "" ' Enter your password here
SQLStr = "SELECT * FROM [dbo].[TPerson]" ' Enter your SQL here
Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";"
rs.Open SQLStr, Cn, adOpenStatic
' Dump to spreadsheet
With Worksheets("sheet1").Range("a1:z500") ' Enter your sheet name and range here
.ClearContents
.CopyFromRecordset rs
End With
' Tidy up
rs.Close
Set rs = Nothing
MsgBox "Data Exported into SQL."
Cn.Close
Set Cn = Nothing
End Sub
Upvotes: 0
Reputation: 51
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myFiles As String()
myFiles = IO.Directory.GetFiles("D:\Temp\", "*.txt")
Dim newFilePath As String
For Each filepath As String In myFiles
newFilePath = filepath.Replace(".txt", ".html")
System.IO.File.Move(filepath, newFilePath)
Next
End Sub
End Class
Upvotes: 5
Reputation: 12851
Solved. Thanks All. :)
Dim [option] As SearchOption = SearchOption.AllDirectories
[option] = SearchOption.AllDirectories
Dim files As String()
files = Directory.GetFiles("C:\New Folder", "*.resxx", [option])
Dim filepath_new As String
For Each filepath As String In files
filepath_new = filepath.Replace(".resxx", ".resx")
System.IO.File.Move(filepath, filepath_new)
Next
Upvotes: 1
Reputation: 62145
Yes there is: Path.ChangeExtension
In fact the Path class in general has a whole range of useful file/directory name manipulation methods. It's surprising how many developers don't know about it/use it.
Upvotes: 12
Reputation: 81482
Changing the file extension of a file is renaming the file.
Upvotes: 1