locked
moving a file from one folder to another - FTP

    Question

  • Hi All,

     

    how can i move a file from one folder to another(some times to subfolders) inside the same FTP server

    (or can i move a file from one ftp folder to another ftp folder directly ?)


    i have a ftp folder with all the permissions say - ftp://191.108.2.178/ORBIT/

    inside the ftp root folder (ORBIT)
    i have two folders

    "VOC" - [ftp://191.108.2.178/ORBIT/VOC]
    "VOC_Backup_04032008" - [ftp://191.108.2.178/ORBIT/VOC_Backup_04032008]


    UPloading,Downloading (to local path),deleting files etc are working fine for me.

    now i need to move all the files inside the folder "VOC" to the folder "VOC_Backup_04032008"

    when i tried with File.Move, ... etc its showing the path format is invalid

    ---------------------------------


    i am using C# , .Net 2005

    also i am using FtpWebRequest & FtpWebResponse

    FtpWebRequest reqFTP;
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(newPath));
    reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
    reqFTP.UseBinary = true;
    reqFTP.Credentials = new NetworkCredential("admin", "admin");
    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
    Stream ftpStream = response.GetResponseStream();
    ftpStream.Close();
    response.Close();

     


    Thanks in Advance.....

     

    Thursday, May 22, 2008 7:25 AM

Answers

  • you shouldnt be using File.Move as this is only for local file operations, not remote. the correct way is using the FtpWebRequest/Response methods.

     

    What you may need to do is read the file, then upload that file to the new directory, finally deleting the original file in the original directory.

    Thursday, May 22, 2008 8:48 AM
    Moderator

All replies

  • you shouldnt be using File.Move as this is only for local file operations, not remote. the correct way is using the FtpWebRequest/Response methods.

     

    What you may need to do is read the file, then upload that file to the new directory, finally deleting the original file in the original directory.

    Thursday, May 22, 2008 8:48 AM
    Moderator
  • ahmedilyas,

     

    thanks for your reply.....

    Thursday, May 22, 2008 9:21 AM
  • your very welcome Smile

    Thursday, May 22, 2008 9:27 AM
    Moderator
  • There is a better way than getting the file then putting it again -

    NetworkCredential User = new NetworkCredential("UserName", "password");
    FtpWebRequest Wr = (FtpWebRequest)FtpWebRequest.Create("ftp://Somwhere.com/somedirectory/Somefile.file");
    					Wr.UseBinary = true;
    					Wr.Method = WebRequestMethods.Ftp.Rename;
    					Wr.Credentials = User;
    					Wr.RenameTo = "/someotherDirectory/Somefile.file";
    					back = (FtpWebResponse)Wr.GetResponse();
    					bool Success = back.StatusCode == FtpStatusCode.CommandOK || back.StatusCode == FtpStatusCode.FileActionOK;
    
    • Proposed as answer by Mr. Javaman Wednesday, January 26, 2011 3:51 PM
    Tuesday, January 25, 2011 6:45 PM
  • The user asked for FTP solution not .NET solution using File.IO


    Javaman, Cowboy Coders Unite!
    Wednesday, January 26, 2011 3:52 PM
  • thanks, only I had to add something:

    FtpWebRequest Wr = (FtpWebRequest)FtpWebRequest.Create("ftp://somewwhere.com/somedirectory/file.txt");
                                     NetworkCredential User = new NetworkCredential("username", "password");
                                    
                                     Wr.UseBinary = true;
                                     Wr.Method = WebRequestMethods.Ftp.Rename;
                                     Wr.Credentials = User;
                                     Wr.RenameTo = "/"+"someotherDirectory" + "/" +"file.txt";
                                     FtpWebResponse back = (FtpWebResponse)Wr.GetResponse();/*this is what I changed if helps anyone*/
                                   //  back = (FtpWebResponse)Wr.GetResponse();
                                     bool Success = back.StatusCode == FtpStatusCode.CommandOK || back.StatusCode == FtpStatusCode.FileActionOK;


    Wednesday, January 02, 2013 10:02 PM