User 805

User 805

  • NA
  • 8
  • 4.5k

Move Files from one directory to another directory in FTP

Dec 7 2016 8:29 AM
In my application, i am trying to move files for this i need to take one method like this
`Public void Move files(string Sourcepath,string TargetPath,String Filename)
{
//code
}`
 
`sourcePath is ftp://ftp.com/site/Fromsite/-->It contains many main folders-->It contains subfolders-->It contains Files
Targetpath should be ftp://ftp.com/site/MoveFiles/-->It contains specific main folder-->It contains all subfolders-->todaydatefolder-->it contains files `
I need TargetPath upto here `ftp://ftp.com/site/MoveFiles/ i need to create -->It contains specific main folder-->It contains all subfolders-->todaydatefolder-->it contains files `
Can anyone please tell me how can i do this i am at learning stage of this
I tried this code it was not working but in the below code i am not taking files name as a parameter
public void MoveFiles(string sourcepath, NetworkCredential credentials, string targetpath)
{
FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(sourcepath);
listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
listRequest.Credentials = credentials;
List<string> lines = new List<string>();
using (FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse())
using (Stream listStream = listResponse.GetResponseStream())
using (StreamReader listReader = new StreamReader(listStream))
{
while (!listReader.EndOfStream)
{
lines.Add(listReader.ReadLine());
}
}
foreach (string line in lines)
{
string[] tokens =
line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
string name = tokens[8];
string permissions = tokens[0];
string datefolder = DateTime.Today.Month.ToString().Length < 2 ? "0" + DateTime.Today.Month.ToString()
: DateTime.Today.Month.ToString(); datefolder += DateTime.Today.Day.ToString().Length < 2 ? "0" +
DateTime.Today.Day.ToString() : DateTime.Today.Day.ToString(); datefolder += DateTime.Today.Year.ToString();
string FTPFilePath = Path.Combine(targetpath, name,datefolder);
string fileUrl = sourcepath + name;
if (!Directory.Exists(FTPFilePath))
{
Directory.CreateDirectory(FTPFilePath);
FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(FTPFilePath);
Request.Method = WebRequestMethods.Ftp.Rename;
Request.Credentials = credentials;
Request.RenameTo = FTPFilePath;
MoveFiles(fileUrl + "/", credentials, FTPFilePath);
}
else
{
FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(fileUrl);
Request.Method = WebRequestMethods.Ftp.Rename;
Request.Credentials = credentials;
Request.RenameTo = FTPFilePath;
using (FtpWebResponse downloadResponse =
(FtpWebResponse)Request.GetResponse())
using (Stream sourceStream = downloadResponse.GetResponseStream())
using (Stream targetStream = File.Create(FTPFilePath))
{
byte[] buffer = new byte[10240];
int read;
while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
{
targetStream.Write(buffer, 0, read);
}
}
}
}
}
please help me to complete this. Thank you

Answers (1)