Monday, January 2, 2017

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

move-1015582_1920

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.

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

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.

Share:

0 comments: