1

i'm trying to make a FTP Watcher using WinSCP Library, so i can detect each new file or directory added to a specific path, but not if the uploading of this current file/directory has finished !

Example :

 session.Open(sessionOptions);

 List < string > prevFiles = null;

 while (true) {
  // Collect file list
  List < string > files =
   session.EnumerateRemoteFiles(
    remotePath, "*.*", EnumerationOptions.AllDirectories)
   .Select(fileInfo => fileInfo.)
   .ToList();
  if (prevFiles == null) {
   // In the first round, just print number of files found
   Console.WriteLine("Found {0} files", files.Count);
  } else {

   // Then look for differences against the previous list
   IEnumerable < string > added = files.Except(prevFiles);

   if (added.Any()) {
    Console.WriteLine("Added files:");
    foreach(string path in added) {
     Console.WriteLine(path);
    }

   }

  }
  prevFiles = files;
  Console.WriteLine("Sleeping 10s...");
  Thread.Sleep(10000);
 }

So my question is how to detect if the uploading of the file has finished, not at the begin of adding a new file in ftp server.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
AHmedRef
  • 2,555
  • 12
  • 43
  • 75
  • no, is not duplicated, in my case im using winSCP library, Also im want to detect this changes remotely ;) – AHmedRef Nov 21 '17 at 12:43
  • OK, if i put some files in my FTP server, my script (in other server) will detect this actions remotely, but i can't detect if this actions(uploading) is finished or not. – AHmedRef Nov 21 '17 at 12:49
  • Then your question **is duplicate**. Your question has nothing to do with a client-side library. It's a generic FTP protocol problem, covered fully in the linked question. – Martin Prikryl Nov 21 '17 at 12:49
  • im assuming that there is some functions in WinSCP Library (c#) to solve thus issue i guess – AHmedRef Nov 21 '17 at 12:51
  • 2
    Again, FTP protocol does not provide you with information on status of a file upload. Hence, no matter how smart FTP library are you using, it won't give that information, as it has no source to get that data from. Please read the answer to the linked question carefully. – Martin Prikryl Nov 21 '17 at 12:52
  • if FTP protocol does not provide any information about uploaded file status it's will be a Protocol problem as you said, but WinScp Library provide an attribute for each file in a directory, it's called "LastWriteTime" it's can be useful, so we can compare this attribut value with a previous value to check if this file is done or not. (but it's not the best hack because any change in this file will change the 'LastWriteTime' value) – AHmedRef Nov 21 '17 at 12:57
  • 1
    The way I have handled this is to store the size and timestamp of the file. If nothing changes in x minutes/seconds the file is finished uploading and can be downloaded. After the download is complete one last comparison is done and if size and timestamp are still equal then delete the already downloaded file from the ftp server. – Joe C Nov 21 '17 at 13:01

0 Answers0