0

I am using below code. (FluentFTP ) but Item.Modified will give the file created Date only. So its rendering based on the copied file created date. (Not when it copied) How will i get Files based on copied/Added Date in Fluent FTP.

private static void GetFiles()
    {
        using (FtpClient conn = new FtpClient())
        {
            string ftpPath = "ftp://myftp/";

            Dictionary<string, string> dirList = new Dictionary<string, string>();

            DateTime lastRunDate = DateTime.Now.AddMinutes(-2);

            string downloadFileName = @"C:\temp\FTPTest\";

            string newID = Guid.NewGuid().ToString();

            downloadFileName += newID + "\\";

            conn.Host = ftpPath;
            //conn.Credentials = new NetworkCredential("ftptest", "ftptest");
            conn.Connect();

            //Get all directories

            foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
                FtpListOption.Modify | FtpListOption.Recursive))
            {
                // if this is a file
                if (item.Type == FtpFileSystemObjectType.File)
                {
                    if (item.Modified >= lastRunDate)
                    {
                        dirList.Add(item.FullName, item.Modified.ToString());
                        conn.DownloadFile(downloadFileName + item.FullName, item.FullName);
                    }
                }
            }
        }
    }
developer_.net
  • 196
  • 3
  • 14

1 Answers1

0

Explanation:

I am downloading the files from FTP (Read permission reqd.) with same folder structure. So everytime the job runs I can check into the physical path same file(Full Path) exists or not If not exists then it can be consider as a new file. And i can do some action for the same and download as well.

Its just an alternative solution.

Code Changes:

private static void GetFiles()
    {
        using (FtpClient conn = new FtpClient())
        {
            string ftpPath = "ftp://myftp/";

            string downloadFileName = @"C:\temp\FTPTest\";

            downloadFileName +=  "\\";

            conn.Host = ftpPath;
            //conn.Credentials = new NetworkCredential("ftptest", "ftptest");
            conn.Connect();

            //Get all directories

            foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
                FtpListOption.Modify | FtpListOption.Recursive))
            {
                // if this is a file
                if (item.Type == FtpFileSystemObjectType.File)
                {
                    string localFilePath = downloadFileName + item.FullName;

                    //Only newly created files will be downloaded.
                    if (!File.Exists(localFilePath))
                    {
                        conn.DownloadFile(localFilePath, item.FullName);
                        //Do any action here.
                        Console.WriteLine(item.FullName);
                    }
                }
            }
        }
    }
developer_.net
  • 196
  • 3
  • 14
  • File besides content can differ by size, creation date not only by name hence your assumption that file exists based on File.Exists and that is the same file is wrong. – jasmin Jan 09 '20 at 00:23
  • As per our requirement this is working fine. We are checking for only new files. So date wise check is not required. Another solution is Delete file from FTP path create new folder and place it there so whenever any files come in FTP folder you can detect it easily. – developer_.net Jan 10 '20 at 06:44