ComponentPro UltimateSftp

      Upload/download files with advanced filters

      Language Filter: AllSend comments on this topic to ComponentPro

      Use the Upload/Download method to easily upload/download multiple files and directories. There are several options to control the upload. In this example, suppose we have the following local directory structure:

      c:\mydata\file1.txt
      c:\mydata\file2.txt
      c:\mydata\file3.doc
      c:\mydata\file4.pdf
      c:\mydata\emptydir
      c:\mydata\content\contentfile1.doc
      c:\mydata\content\contentfile2.txt
      c:\mydata\content\contentfile3.pdf
      c:\mydata\content\emptyfolder
      c:\mydata\content\folder1
      c:\mydata\content\folder1\folder1_1
      
      

      We now then go through some of the most used options:

      Filter files and directories

      To limit the files that will be uploaded, you can add filtering masks to the localDirectoryPath parameter of the Upload method. However, when you want to exclude unwanted files and directories, you need to use the SearchCondition class or its Fluent API.

      To understand more about wildcard masks and search criterion, see this topic.

      The following examples show you how to solve some specific upload/download cases.

      • Include TXT files but not files with names beginning with "file":
        client.Upload("c:\\mydata", "/data", new NameSearchCondition("*.txt") - new NameSearchCondition("file*"));
        
        // or
        client.Upload("c:\\mydata", "/data", new NameSearchCondition("*.txt").ExcludeMasks("file*"));
        

        The remote server will have the following:

        /data/content/contentfile2.txt
        
      • Include TXT files but not files in "conten?" folder:
        client.Upload("c:\\mydata", "/data", new NameSearchCondition("*.txt").ExcludeMasks("conten?", SearchConditionFileTypes.Directory));
        

        The remote server will have the following:

        /data/file1.txt
        /data/file2.txt
        
      • Include PDF files with path, relative to the base path, that matches "con*":
        client.Upload("c:\\mydata", "/data", new NameSearchCondition(@"con*\*.pdf"));
        

        The remote server will have the following:

        /data/content/contentfile3.pdf
        
      • Upload TXT files EXCEPT files with 0 in size:
        client.Upload("c:\\mydata", "/data", new NameSearchCondition("*.txt").AndSizeGreaterThan(0));
        

        Suppose only remote file '/file1.txt' is empty, the remote server will have the following:

        /data/file2.txt
        /data/content/contentfile2.txt
        

      CreateEmptyDirectories setting

      By default, the component will decide when to create empty directories. You can change that behavior in the CreateEmptyDirectories setting. There are three options for this setting:

      • Auto - default value. The component will decide when to create empty directories. All the above examples use this default behavior. The destination directories are created if they contain at least one file, or the search condition indicates that empty directories should be included.
      • No - directories will not be created unless they contains at least one file.
        TransferOptions opt = new TransferOptions();
        opt.CreateEmptyDirectories = OptionValue.No;
        client.Upload("c:\\mydata", "/data", opt);
        

        That will result in the following remote directory structure:

        /data/content/contentfile1.doc
        /data/content/contentfile2.txt
        /data/content/emptyfolder
        /data/content/folder1
        /data/content/folder1/folder1_1
        
      • Yes - directories will always be created regardless whether they contain files or not.
        opt.CreateEmptyDirectories = OptionValue.Yes;
        client.Upload("c:\\mydata\\*.txt", "/data", opt);
        

        That will result in the following remote directory structure:

        /data/file1.txt
        /data/file2.txt
        /data/emptydir
        /data/content/contentfile2.txt
        /data/content/emptyfolder
        /data/content/folder1
        /data/content/folder1/folder1_1
        

      Recursive and Non-recursive Upload

      • Recursive: Files and subdirectories in a directory will be transferred.

        The following line of code will upload everything in "c:\mydata" to "/data" folder:

        client.Upload("c:\\mydata", "/data", true, FileOverwriteMode.Overwrite);
        

        The remote SFTP server will have the following:

        /data/file1.txt
        /data/file2.txt
        /data/file3.doc
        /data/emptydir
        /data/content/contentfile1.doc
        /data/content/contentfile2.txt
        /data/content/contentfile3.pdf
        /data/content/emptyfolder
        /data/content/folder1
        /data/content/folder1/folder1_1
        

        If you want to filter the files to be uploaded:

        client.Upload("c:\\mydata\\*.txt;*.doc", "/data", true, FileOverwriteMode.Overwrite);
        

        The remote server will have the following:

        /data/file1.txt
        /data/file2.txt
        /data/file3.doc
        /data/content/contentfile1.doc
        /data/content/contentfile2.txt
        
      • Non-recursive: Only files in the first level of a directory will be transferred.

        If you only need to upload files in "c:\mydata" folder to "/data" folder without traversing all the subdirectories:

        client.Upload("c:\\mydata", "/data", false, FileOverwriteMode.Overwrite);
        

        The remote server will have the following:

        /data/file1.txt
        /data/file2.txt
        /data/file3.doc
        /data/file4.pdf
        

      Include the base directory

      To include the containg directory when uploading its contents, you can set the includeBaseDirectory parameter to true as shown in the following line of code:

      client.Upload("c:\\mydata\\content", true, "/data", false, FileOverwriteMode.Overwrite);
      

      That will result in the following remote directory structure:

      /data/content/contentfile1.doc
      /data/content/contentfile2.txt
      /data/content/contentfile3.pdf
      /data/content/emptyfolder
      /data/content/folder1
      /data/content/folder1/folder1_1
      

      Control file upload/download process

      If you wish to control the file upload process, you might need to handle the TransferConfirm and Progress events. Check out this topic: Control file transfer process and SftpClient, SftpMultipleThreadsTransferFiles and SftpTransferMultipleFiles sample projects for more details on how to handle these events.

      Complete example code

      using ComponentPro;
      using ComponentPro.IO;
      using ComponentPro.Net;
      
      ...
      
      using (Sftp client = new Sftp())
      {
          // Connect to the server 
          client.Connect("myserver", 22);
      
          // Authenticate 
          client.Authenticate("user", "pass");
      
          client.EnsuresDirectoryCreated(@"/data");
      
          // Upload all files from "c:\mydata" to "/data" 
          client.Upload("c:\\mydata", "/data", true, FileOverwriteMode.Overwrite);
      
          // Upload files with TXT and DOC extensions 
          client.Upload("c:\\mydata\\*.txt;*.doc", "/data", true, FileOverwriteMode.Overwrite);
      
          // Clean "/data" folder. Only contents of "/data" are deleted. "/data" will be empty. 
          client.Delete("/data", true);
      
          // Upload files in "/" folder only. 
          client.Upload("c:\\mydata", "/data", false, FileOverwriteMode.Overwrite);
      
          // Clean "/data" folder. Only contents of "/data" are deleted. "/data" will be empty. 
          client.Delete("/data", true);
      
          // Upload the whole "/content" directory. "c:\mydata\content" will be created. 
          client.Upload("c:\\mydata\\content", true, "/data", false, FileOverwriteMode.Overwrite);
      
          // Clean "/data" folder. Only contents of "/data" are deleted. "/data" will be empty. 
          client.Delete("/data", true);
      
          #region SearchCondition
      
          // Upload TXT files EXCEPT "file*" files. 
          client.Upload("c:\\mydata", "/data", new NameSearchCondition("*.txt") - new NameSearchCondition("file*"));
      
          // or 
          client.Upload("c:\\mydata", "/data", new NameSearchCondition("*.txt").ExcludeMasks("file*"));
      
          // Clean "/data" folder. Only contents of "/data" are deleted. "/data" will be empty. 
          client.Delete("/data", true);
      
          // Upload TXT files EXCEPT files in "conten?" folder. 
          client.Upload("c:\\mydata", "/data", new NameSearchCondition("*.txt").ExcludeMasks("conten?", SearchConditionFileTypes.Directory));
      
          // Clean "/data" folder. Only contents of "/data" are deleted. "/data" will be empty. 
          client.Delete("/data", true);
      
          // Upload TXT files EXCEPT files in "conten?" folder. 
          client.Upload("c:\\mydata", "/data", new NameSearchCondition(@"con*\*.pdf"));
      
          // Clean "/data" folder. Only contents of "/data" are deleted. "/data" will be empty. 
          client.Delete("/data", true);
      
          // Upload TXT files EXCEPT files with 0 size. 
          client.Upload("c:\\mydata", "/data", new NameSearchCondition("*.txt").AndSizeGreaterThan(0));
      
          // Clean "/data" folder. Only contents of "/data" are deleted. "/data" will be empty. 
          client.Delete("/data", true);
      
          #endregion
      
      
          // Upload all files and subdirectories without copying empty directories. 
          TransferOptions opt = new TransferOptions();
          opt.CreateEmptyDirectories = OptionValue.No;
          client.Upload("c:\\mydata", "/data", opt);
      
          // Clean "/data" folder. Only contents of "/data" are deleted. "/data" will be empty. 
          client.Delete("/data", true);
      
          // Upload all TXT files and empty directories. 
          opt.CreateEmptyDirectories = OptionValue.Yes;
          client.Upload("c:\\mydata\\*.txt", "/data", opt);
      }