Throwing this one in quickly… basically needed a method to rename a file through FTP in a little call monitoring app I’d written. Not as straightforward as I’d have hoped, but because of various constraints on the system it had to be done through FTP. Not a requirement here, but you may want to include code to check the new file name doesn’t exist..!
One thing to note, FtpWebRequest will not work through a proxy so I included an exception to bypass for the relevant address/OUs through Group Policy.
currentFilename is the full ftp path as a string?eg ftp://192.168.0.1/folder/oldfilename.mp3
newFilename is the new name without the path as a string eg newfilename.mp3
Imports System.Net
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Private Sub RenameFileName(ByVal currentFilename As String, ByVal newFilename As String)
Dim reqFTP As FtpWebRequest = Nothing
Dim ftpStream As Stream = Nothing
Try
reqFTP = DirectCast(FtpWebRequest.Create(New Uri(currentFilename)), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.Rename
reqFTP.RenameTo = newFilename
reqFTP.UseBinary = True
reqFTP.KeepAlive = False
reqFTP.Credentials = New NetworkCredential(“your_ftp_login“, “your_ftp_pwd“)
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()
ftpStream.Close()
response.Close()
Catch ex As Exception
If ftpStream IsNot Nothing Then
ftpStream.Close()
ftpStream.Dispose()
End If
Throw New Exception(ex.Message.ToString())
End Try
End If
End Sub