-1

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

ns046
  • 41
  • 5
  • Set the values of `My.Settings.FTPConn & My.Settings.FTPOutbound & strFile` and `"Old/" & strFile` to local variables and post their content here. – Jimi Jun 20 '22 at 14:21
  • Even better, post a [log file](https://stackoverflow.com/q/9664650/850848). – Martin Prikryl Jun 20 '22 at 14:27
  • @Jimi I have updated my post as per your suggestion – ns046 Jun 20 '22 at 15:11
  • Did you have the /Old directory created? or did you have the CDK_IS_20220603_092921.CSV file already there? different FTP servers have little but significative differences in behaviour. – J.Salas Jun 20 '22 at 15:14
  • Do you see something weird in that path? Maybe use the `UriBuilder` class to build your network path (add the host separated from the path - no `%2f`). Where is `Old` in the tree? Did you want to write `/Old/CDK_IS_20220603_092921.CSV`? Or something else? – Jimi Jun 20 '22 at 15:32
  • @J.Salas There is already a directory /Old and the file CDK_IS_20220603_092921.CSV is also present in that directory – ns046 Jun 20 '22 at 16:12
  • @Jimi Old directory is present inside the Outbound directory, the file CDK_IS_20220603_092921.CSV is also there inside the Old directory.. i will try to use UriBuilder as you suggested – ns046 Jun 20 '22 at 16:20
  • If you have access to the server running the FTP server and it's a Microsoft server, then you could run Sysinternals ProcMon, and monitor the folder to see which process is trying to access the file and whether is has permissions to do so. The issue may simply be a permissions one. – Fawlty Jun 20 '22 at 16:23

1 Answers1

0

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.

J.Salas
  • 1,268
  • 1
  • 8
  • 15