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:
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.
The following examples show you how to solve some specific upload/download cases.
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
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
client.Upload("c:\\mydata", "/data", new NameSearchCondition(@"con*\*.pdf"));
The remote server will have the following:
/data/content/contentfile3.pdf
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
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:
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
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
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
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
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
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.
using ComponentPro; using ComponentPro.IO; using ComponentPro.Net; ... using (Ftp client = new Ftp()) { // Connect to the server client.Connect("myserver", 21); // 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); }