Archived Forum Post

Index of archived forum posts

Question:

FTP recursion question

Jun 29 '15 at 16:01

Hello. I'm trying to read a remote FTP site then for each folder I want to check whether or not I have it marked in my database and to scan all the files in the folder against the database. I'm running into problems though in the beginning when I'm just doing the scan. I'm sure I just screwed up my logic somewhere, but I'm not sure what it was that I did wrong. Here is the FTP server file system:

/Root/Category1 (folder)
/Root/Category2 (folder)
/Root/Category3 (folder)
/Root/Category1/Subcategory1 (folder)
/Root/Category1/Subcategory2 (folder)
/Root/Category1/Subcategory1/testfile.c1.s1.01.txt
/Root/Category1/Subcategory1/testfile.c1.s1.02.txt
/Root/Category1/Subcategory1/testfile.c1.s1.03.txt
/Root/Category1/Subcategory1/testfile.c1.s1.04.txt
/Root/Category1/Subcategory1/testfile.c1.s1.05.txt
/Root/Category1/Subcategory2/testfile.c1.s2.01.txt
/Root/Category3/testfile.c3.01.txt

The code I am using to do the scan is:

void Scan() {
  try {
    using (Chilkat.Ftp2 ftp = new Ftp2()) {
      if (!ftp.UnlockComponent("xxxx")) { Write("** Problem activating the FTP component!"); return; }
      ftp.Hostname = cfg["f_host"];
      ftp.Username = cfg["f_user"];
      ftp.Password = DecryptString(cfg["f_pass"], "xxx");
      if (ftp.Connect()) {
        if (cfg["f_root"].Trim() != "") {
          if (!ftp.ChangeRemoteDir(cfg["f_root"])) {
            Write("** Could not change to specified root folder!");
            ftp.Disconnect();
            return;
          }
          ftpDirectory = cfg["f_root"];
        } else {
          ftpDirectory = "/";
        }
        ScanFolder(ftp, ftpDirectory, true);
        ftp.Disconnect();
      } else {
        Write("** Could not connect to FTP server!");
        Write(ftp.LastErrorText);
      }
    }
  } catch (Exception ex) {
    Write("** EXCEPTION: " + ex.Message);
  }
}

private void ScanFolder(Chilkat.Ftp2 ftp, string folderpath, bool recurse) {
  if (!ftp.ChangeRemoteDir(folderpath)) {
    Write("** Could not change directory to " + folderpath);
    return;
  } else {
    int n = ftp.GetDirCount();
    Write(n.ToString() + ": Scanning path: " + folderpath);
    for (int x = 0; x < n; x++) {
      if (ftp.GetIsDirectory(x)) {
        if (recurse) {
          ScanFolder(ftp, folderpath + "/" + ftp.GetFilename(x), true);
        }
      } else {
        Write("Search DB for " + ftp.GetFilename(x) + " in path " + folderpath);
      }
    }
  }
}

My code logs in and changes to "/Root" as specified in cfg["f_root"] so that is the folder it starts running in. The output is:

06/27/15 15:38: 3: Scanning path: /Root
06/27/15 15:38: 2: Scanning path: /Root/Category1
06/27/15 15:38: 5: Scanning path: /Root/Category1/Subcategory1
06/27/15 15:38: Search DB for testfile.c1.s1.01.txt in path /Root/Category1/Subcategory1
06/27/15 15:38: Search DB for testfile.c1.s1.02.txt in path /Root/Category1/Subcategory1
06/27/15 15:38: Search DB for testfile.c1.s1.03.txt in path /Root/Category1/Subcategory1
06/27/15 15:38: Search DB for testfile.c1.s1.04.txt in path /Root/Category1/Subcategory1
06/27/15 15:38: Search DB for testfile.c1.s1.05.txt in path /Root/Category1/Subcategory1
06/27/15 15:38: Search DB for testfile.c1.s1.02.txt in path /Root/Category1
06/27/15 15:38: Search DB for testfile.c1.s1.02.txt in path /Root
06/27/15 15:38: Search DB for testfile.c1.s1.03.txt in path /Root

The first subcategory folder worked fine, shows all the right files but then the last three dir scans show files when there aren't any files there. Why is it doing that?

Thanks, sorry for writing a book lol.


Answer

I am not sure if the .net version works the same as the ActiveX version. In the ActiveX, the methods return 1 or 0 instead of true or false. I have also done a similar project and found that iterating over an xml document is easier than ftp. I used .GetXmlDirListing to fill a chilkat xml object.