How to move a file from one folder to another on FTP Server

  1 minute read

To move a file from one folder to another we normally using System.IO.File functions Move or Copy but in this scenario these functions will not work on FTP server. To achieve this on FTP we need to use FTP web request methods and below is the code i have used for that.

For your reference please check my previous post for downloading files from the FTP Server How to download files from FTP server.

The function is very simple. The tricky part over here is what method to use on FTP WebRequest to move the file, the method we have to use is ‘RENAME’ and using RenameTo property to specify the file path.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    [TryFunction] 
    LOCAL PROCEDURE MoveFileFromConcurFTPToArchive@1240060038(FileToDownload@1240060010 : Text); 
    VAR 
      FTPWebRequest@1240060000 : DotNet "'System, Version=4.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebRequest"; 
      FTPWebResponse@1240060001 : DotNet "'System, Version=4.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebResponse"; 
      NetworkCredential@1240060002 : DotNet "'System, Version=4.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.NetworkCredential"; 
      WebRequestMethods@1240060003 : DotNet "'System, Version=4.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebRequestMethods+File"; 
      ResponseStream@1240060005 : InStream; 
      FileStream@1240060006 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream"; 
      FileName@1240060007 : Text; 
    BEGIN 
      GetFTPSetup; 
      FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Download FilePath" + FileToDownload); 
      FTPWebRequest.Credentials :=  NetworkCredential.NetworkCredential(FTPSetup."FTP UserName",FTPSetup."FTP Password"); 
      FTPWebRequest.UseBinary := TRUE; 
      FTPWebRequest.UsePassive := TRUE; 
      FTPWebRequest.KeepAlive := TRUE; 
      FTPWebRequest.Method := 'RENAME'; 
      FTPWebRequest.RenameTo := 'archive/' + FileToDownload; 
      FTPWebResponse := FTPWebRequest.GetResponse(); 
    END;

I hope this will save time for others who are looking for a solution.<p>Please leave your comments, feedback or any suggestions you have for me to improve my blog and also if you have any questions, feel free to post.

Leave a comment