I have a program that needs to move a file from one directory to another on an FTP server. For example, the file is in:
ftp://1.1.1.1/MAIN/Dir1
and I need to move the file to:
ftp://1.1.1.1/MAIN/Dir2
I found a couple of articles recommending use of the Rename command, so I tried the following:
Uri serverFile = new Uri(“ftp://1.1.1.1/MAIN/Dir1/MyFile.txt"); FtpWebRequest reqFTP= (FtpWebRequest)FtpWebRequest.Create(serverFile); reqFTP.Method = WebRequestMethods.Ftp.Rename; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPass); reqFTP.RenameTo = “ftp://1.1.1.1/MAIN/Dir2/MyFile.txt"; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
But this doesn’t seem to work – I get the following error:
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
At first I thought this might relate to permissions, but as far as I can see, I have permissions to the entire FTP site (it is on my local PC and the uri is resolved to localhost).
Should it be possible to move files between directories like this, and if not, how is it possible?
To address some of the point / suggestions that have been raised:
Additionally, I have tried setting the directory path to be:
ftp://1.1.1.1/%2fMAIN/Dir1/MyFile.txt
Both for the source and target path - but this makes no difference either.
I found this article, which seems to say that specifying the destination as a relative path would help - it doesn't appear to be possible to specify an absolute path as the destination.
reqFTP.RenameTo = “../Dir2/MyFile.txt";
The FTP command to move files is "rename," in effect renaming the file path. Automate uses this same command to move a file into its new location.
If you send files using FTP, files are either uploaded or downloaded to the FTP server. When you're uploading files, the files are transferred from a personal computer to the server. When you're downloaded files, the files are transferred from the server to your personal computer.
FTP is an acronym for File Transfer Protocol. As the name suggests, FTP is used to transfer files between computers on a network. You can use FTP to exchange files between computer accounts, transfer files between an account and a desktop computer, or access online software archives.
All about File Transfer Protocol and FTP clients File Transfer Protocol (FTP) is a network protocol for transferring copies of files from one computer to another. An FTP client is a program that allows you to move files between computers.
Had the same problem and found another way to solve the problem:
public string FtpRename( string source, string destination ) { if ( source == destination ) return; Uri uriSource = new Uri( this.Hostname + "/" + source ), UriKind.Absolute ); Uri uriDestination = new Uri( this.Hostname + "/" + destination ), UriKind.Absolute ); // Do the files exist? if ( !FtpFileExists( uriSource.AbsolutePath ) ) { throw ( new FileNotFoundException( string.Format( "Source '{0}' not found!", uriSource.AbsolutePath ) ) ); } if ( FtpFileExists( uriDestination.AbsolutePath ) ) { throw ( new ApplicationException( string.Format( "Target '{0}' already exists!", uriDestination.AbsolutePath ) ) ); } Uri targetUriRelative = uriSource.MakeRelativeUri( uriDestination ); //perform rename FtpWebRequest ftp = GetRequest( uriSource.AbsoluteUri ); ftp.Method = WebRequestMethods.Ftp.Rename; ftp.RenameTo = Uri.UnescapeDataString( targetUriRelative.OriginalString ); FtpWebResponse response = (FtpWebResponse)ftp.GetResponse(); return response.StatusDescription; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With