vb.net rename file through ftp

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
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

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()
Catch ex As Exception
If ftpStream IsNot Nothing Then
End If
Throw New Exception(ex.Message.ToString())
End Try
End If
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.