Monday, January 2, 2017

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


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.

LOCAL PROCEDURE MoveFileFromConcurFTPToArchive@1240060038(FileToDownload@1240060010 : Text);
  FTPWebRequest@1240060000 : DotNet "'System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebRequest";
  FTPWebResponse@1240060001 : DotNet "'System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebResponse";
  NetworkCredential@1240060002 : DotNet "'System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.NetworkCredential";
  WebRequestMethods@1240060003 : DotNet "'System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebRequestMethods+File";
  ResponseStream@1240060005 : InStream;
  FileStream@1240060006 : DotNet "'mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream";
  FileName@1240060007 : Text;
  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();

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.



aws jobs in hyderabad said...

I admire the valuable information you offer in your articles. I will bookmark your blog and have my friends check up here often. I am quite sure they will learn lots of new stuff here than anybody else! Regards aws jobs in hyderabad.