I am trying to use FTPWebRequest Rename method, below is my code snippet.
Dim str1 As String = "ftp://slsdevvs01.am.cdkgroup.net/%2fDataTrans/%2f"
Dim str2 As String = "Archive/Outbound/"
Dim str3 As String = "CDK_IS_20220603_092921.CSV"
Dim str4 As String = "Old/CDK_IS_20220603_092921.CSV"
Dim cdkCommandFTP As New NetworkCredential(My.Settings.FTPUser, My.Settings.FTPPass)
requestFTP = DirectCast(System.Net.WebRequest.Create(str1 & str2 & str3), FtpWebRequest)
requestFTP.Credentials = cdkCommandFTP
requestFTP.Method = WebRequestMethods.Ftp.Rename
requestFTP.RenameTo = str4
requestFTP.GetResponse()
When i am trying to run my application i am getting the below error after GetResponse()
The remote server returned an error: (550) File unavailable (e.g., file not found, no access) rename
I have passed the working credentials and the file also exists in the location
OK then you problem is that the file exists in destination.
What happens when there is a name collision depends on the server (MS, Filezilla, ...), no common way to deal with it to everyone of them, so to you need to deal with it manually.
Either check for the files existence first (by requesting it size for example), deleting it if found and then rename it OR attempt a rename, catch the exception, delete the file then rename again.