Search code examples
c#fluentftp

fluentftp upload directory skip subfolders


I use the fluentftp library to upload a folder to ftp. How i can skip the subfolders of the directory.

              // upload only PDF files
            var rules = new List<FtpRule>{
               new FtpFileExtensionRule(true, new List<string>{ "pdf" }),
               new FtpFolderNameRule(false, FtpFolderNameRule.CommonBlacklistedFolders)
               // only allow PDF files
            };
            ftp.UploadDirectory(ServiceAbrechnungPath, @"/Abrechnungen",
                FtpFolderSyncMode.Mirror, FtpRemoteExists.Skip, FtpVerify.None, rules);

Solution

  • You'll need to add a FtpFolderNameRule to exclude the subfolders.

    Using your code, it'll look something like this;

    using System.Linq
    
    //Get a list of subfolders in the root folder without their path name.
    //This should be just the folders in the root folder i.e. you don't need a recursive list of folders within these folders    
    var subfolders = Directory.GetDirectories(ServiceAbrechnungPath).Select(subDirectory => subDirectory.Remove(0, ServiceAbrechnungPath.Length)).ToList();
    
    // upload only PDF files in the root of ServiceAbrechnungPath
    var rules = new List<FtpRule>{
        new FtpFileExtensionRule(true, new List<string>{ "pdf" }), // only allow PDF files
        new FtpFolderNameRule(false, subfolders)  // exclude subfolders    
    };
    
    var uploadResult = ftp.UploadDirectory(ServiceAbrechnungPath, @"/Abrechnungen", FtpFolderSyncMode.Mirror, FtpRemoteExists.Skip,FtpVerify.None, rules);
    

    The uploadResult variable will contain a List<FtpResult> showing which files were successfully uploaded and which folders/files were skipped by the rules.