More .NET libraries
-
Rebex FTP
.NET FTP client
-
Rebex SSH Shell
.NET SSH Shell
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Multiple files operations
On this page:
Multi-file upload and download
To transfer multiple files (such as all ".txt" files), use Upload
and Download
methods.
Both take several arguments:
- 1. Source path - path to a file or directory, or a mask (wildcard pattern)
- 2. Target path - path to an existing directory
- 3. Traversal mode (optional) - specifies how to transfer directories; if the source path is a mask, specifies how to search for files (default is recursive and shallow search)
- 4. Transfer method (optional) - specifies whether to copy or move files
- 5. Action on existing files (optional) - specifies what to do when a file already exists in target destination
TraversalMode
, see the
Advanced Download and Upload blog post.
Uploading
// upload a local file sftp.Upload(@"C:\MyData\file.txt", "/MyData"); // upload all files from "MyData" directory only // (files from subdirectories are not transferred) sftp.Upload(@"C:\MyData\*", "/MyData", TraversalMode.MatchFilesShallow); // upload all ".txt" files from "MyData" directory only // (files from subdirectories are not transferred) sftp.Upload(@"C:\MyData\*.txt", "/MyData", TraversalMode.MatchFilesShallow); // upload all ".txt" files under "MyData" directory anywhere // (files from subdirectories are transferred as well) sftp.Upload(@"C:\MyData\*.txt", "/MyData", TraversalMode.MatchFilesDeep);
' upload a local file sftp.Upload("C:\MyData\file.txt", "/MyData") ' upload all files from "MyData" directory only ' (files from subdirectories are not transferred) sftp.Upload("C:\MyData\*", "/MyData", TraversalMode.MatchFilesShallow) ' upload all ".txt" files from "MyData" directory only ' (files from subdirectories are not transferred) sftp.Upload("C:\MyData\*.txt", "/MyData", TraversalMode.MatchFilesShallow) ' upload all ".txt" files under "MyData" directory anywhere ' (files from subdirectories are transferred as well) sftp.Upload("C:\MyData\*.txt", "/MyData", TraversalMode.MatchFilesDeep)
Downloading
// download a remote file sftp.Download("/MyData/file.txt", @"C:\MyData"); // download all files from "/MyData" directory only // (files from subdirectories are not transferred) sftp.Download("/MyData/*", @"C:\MyData", TraversalMode.MatchFilesShallow); // download all "*.txt" files from "/MyData" directory only // (files from subdirectories are not transferred) sftp.Download("/MyData/*.txt", @"C:\MyData", TraversalMode.MatchFilesShallow); // download all "*.txt" files under "/MyData" directory anywhere // (files from subdirectories are transferred as well) sftp.Download("/MyData/*.txt", @"C:\MyData", TraversalMode.MatchFilesDeep);
' download a remote file sftp.Download("/MyData/file.txt", "C:\MyData") ' download all files from "/MyData" directory only ' (files from subdirectories are not transferred) sftp.Download("/MyData/*", "C:\MyData", TraversalMode.MatchFilesShallow) ' download all "*.txt" files from "/MyData" directory only ' (files from subdirectories are not transferred) sftp.Download("/MyData/*.txt", "C:\MyData", TraversalMode.MatchFilesShallow) ' download all "*.txt" files under "/MyData" directory anywhere ' (files from subdirectories are transferred as well) sftp.Download("/MyData/*.txt", "C:\MyData", TraversalMode.MatchFilesDeep)
Recursive directory upload and download
Upload
and Download
methods make it possible to easily transfer whole directories with all their content (with all the subfolders and files).
Uploading
// upload the contents of a local directory // - upload goes into "/MyData" // - "/MyData/MyData" is not created at the server sftp.Upload(@"C:\MyData\*", "/MyData"); // upload a whole local directory // - upload goes into "/MyData/Exports" // - "/MyData/Exports" is created at the server sftp.Upload(@"C:\MyData\Exports", "/MyData");
' upload the contents of a local directory ' - upload goes into "/MyData" ' - "/MyData/MyData" is not created at the server sftp.Upload("C:\MyData\*", "/MyData") ' upload a whole local directory ' - upload goes into "/MyData/Exports" ' - "/MyData/Exports" is created at the server sftp.Upload("C:\MyData\Exports", "/MyData")
Downloading
// download content of a remote directory // - download goes into "C:\MyData" // - "C:\MyData\MyData" is not created at the client sftp.Download("/MyData/*", @"C:\MyData"); // download a whole remote directory // - download goes into "C:\MyData\Exports" // - "C:\MyData\Exports" is created at the client sftp.Download("/MyData/Exports", @"C:\MyData");
' download content of a remote directory ' - download goes into "C:\MyData" ' - "C:\MyData\MyData" is not created at the client sftp.Download("/MyData/*", "C:\MyData") ' download a whole remote directory ' - download goes into "C:\MyData\Exports" ' - "C:\MyData\Exports" is created at the client sftp.Download("/MyData/Exports", "C:\MyData")
Deleting multiple files
To delete multiple files (such as all ".tmp" files), use Delete
method:
// delete all files in "/MyData" directory only // (keep files in subdirectories) sftp.Delete("/MyData/*", TraversalMode.MatchFilesShallow); // delete all ".tmp" files in "/MyData" directory only // (keep ".tmp" files in subdirectories) sftp.Delete("/MyData/*.tmp", TraversalMode.MatchFilesShallow); // delete all ".tmp" files anywhere // (delete ".tmp" files in subdirectories as well) sftp.Delete("/MyData/*.tmp", TraversalMode.MatchFilesDeep);
' delete all files in "/MyData" directory only ' (keep files in subdirectories) sftp.Delete("/MyData/*", TraversalMode.MatchFilesShallow) ' delete all ".tmp" files in "/MyData" directory only ' (keep ".tmp" files in subdirectories) sftp.Delete("/MyData/*.tmp", TraversalMode.MatchFilesShallow) ' delete all ".tmp" files anywhere ' (delete ".tmp" files in subdirectories as well) sftp.Delete("/MyData/*.tmp", TraversalMode.MatchFilesDeep)
Deleting directories recursively
Delete
method makes it possible to delete whole directories (with all their content) as well:
// only delete the contents of a remote directory // (keep the directory itself) sftp.Delete("/MyData/Temp/*", TraversalMode.Recursive); // delete a remote directory with its whole content recursively sftp.Delete("/MyData/Temp", TraversalMode.Recursive);
' only delete the contents of a remote directory ' (keep the directory itself) sftp.Delete("/MyData/Temp/*", TraversalMode.Recursive) ' delete a remote directory with its whole content recursively sftp.Delete("/MyData/Temp", TraversalMode.Recursive)
Using wildcards
Wildcards are used to specify multiple files or directories whose names match a pattern. Supported wildcards are:
- asterisk ('*') - matches any number (0, 1 or more) of any characters except a directory separator ("a*b" matches "ab", "axb", "axyzb", but not "a/b" or "ax/yb")
- question mark ('?') - matches exactly one character except a directory separator ("a?b" matches "axb" and "ayb", but not "ab", "axyzb" or "a/b")
TraversalMode
argument. For more information,
see the Advanced Download and Upload blog post.
// download all January ".csv" exports from 2000 sftp.Download("/MyData/Exports/2000-01-??.csv", @"C:\MyData"); // upload content of "C:\MyData" to "/MyData" // (subdirectories are uploaded as well) sftp.Upload(@"C:\MyData\*", "/MyData"); // upload all ".txt" files from "C:\MyData" only // (files from subdirectories are not uploaded) sftp.Upload(@"C:\MyData\*.txt", "/MyData", TraversalMode.MatchFilesShallow); // upload all ".txt" files from "C:\MyData" and under // (files from subdirectories are uploaded as well) sftp.Upload(@"C:\MyData\*.txt", "/MyData", TraversalMode.MatchFilesDeep);
' download all January ".csv" exports from 2000 sftp.Download("/MyData/Exports/2000-01-??.csv", "C:\MyData") ' upload content of "C:\MyData" to "/MyData" ' (subdirectories are uploaded as well) sftp.Upload("C:\MyData\*", "/MyData") ' upload all ".txt" files from "C:\MyData" only ' (files from subdirectories are not uploaded) sftp.Upload("C:\MyData\*.txt", "/MyData", TraversalMode.MatchFilesShallow) ' upload all ".txt" files from "C:\MyData" and under ' (files from subdirectories are uploaded as well) sftp.Upload("C:\MyData\*.txt", "/MyData", TraversalMode.MatchFilesDeep)
Using FileSet
When simple wildcard patters are not powerful enough, use Rebex.IO.FileSet
object.
It makes it possible to combine multiple matching criteria (such as all ".txt" and ".csv" files) and specify exclude criteria as well.
In addition to common wildcards and
TraversalMode
criteria,
a special "**" wildcard is available that matches any character, including directory separators ("/MyData/**/file.txt" matches "/MyData/file.txt" and also "/MyData/a/b/c/file.txt").
// prepare file set var fileSet = new Rebex.IO.FileSet(@"C:\MyData"); // include some files - the path or mask is relative to the C:\MyData base path fileSet.Include("*.txt", TraversalMode.MatchFilesShallow); // include some directories fileSet.Include("*Web*/"); // exclude ".tmp" files (in any location) fileSet.Exclude("*.tmp", TraversalMode.MatchFilesDeep); // exclude "Backups" directories (in any location) fileSet.Exclude("**/Backups/"); // exclude "C:\MyData\Ignore" directory fileSet.Exclude("Ignore"); // display all the files to be uploaded Console.WriteLine("Uploading files:"); foreach (LocalItem item in fileSet.GetLocalItems()) { Console.WriteLine(item.FullPath); } // upload all files and directories matching the file set sftp.Upload(fileSet, "/MyData");
' prepare file set Dim fileSet = New Rebex.IO.FileSet("C:\MyData") ' include some files fileSet.Include("*.txt", TraversalMode.MatchFilesShallow) ' include some directories fileSet.Include("*Web*/") ' exclude ".tmp" files (in any location) fileSet.Exclude("*.tmp", TraversalMode.MatchFilesDeep) ' exclude "Backups" directories (in any location) fileSet.Exclude("**/Backups/") ' display all the files to be uploaded Console.WriteLine("Uploading files:") For Each item As LocalItem In fileSet.GetLocalItems() Console.WriteLine(item.FullPath) Next ' upload all files and directories matching the file set sftp.Upload(fileSet, "/MyData")
Extending FileSet class
When wildcard patterns are still not powerful enough, you can easily extend the FileSet
class and add custom matching code.
Just inherit from Rebex.IO.FileSet
and override the IsMatch
method.
FileSet
implementation which uses regular expression to filter files.
Merging multiple directories
FileSet
object can even be used to upload (or download) files
from multiple directories (or even a whole directory tree) into a single directory - just set Rebex.IO.FileSet.Flatten
to true
and call the Upload
or Download
method.
For example, when uploading files "A\a.txt" and "B\b.txt" to "/MyData" directory, both the files end up in the "/MyData" directory (and no "A" or "B" directories are created).
// prepare a file set // (all files from "C:\MyData" and its subdirectories) var fileSet = new Rebex.IO.FileSet(@"C:\MyData", "*"); fileSet.Flatten = true; // upload the files sftp.Upload(fileSet, "/MyData");
' prepare a file set ' (all files from "C:\MyData" and its subdirectories) Dim fileSet = New Rebex.IO.FileSet("C:\MyData", "*") fileSet.Flatten = True ' upload the files sftp.Upload(fileSet, "/MyData")
Moving files between client and server
Upload
and Download
methods support move mode,
in which files (and optionally directories) are deleted once successfully transferred -
just specify TransferMethod.Move
mode in the method call.
To further refine move behavior, set Settings.MultiFileMoveMode
to:
MoveMode.All
(default) - move both files and directoriesMoveMode.FilesOnly
- move files only (keep source directory structure)
// this is the default mode (it's not necessary to set it) sftp.Settings.MultiFileMoveMode = MoveMode.All; // move whole directory "Exports" to the server sftp.Upload(@"C:\MyData\Exports", "/MyData", 0, TransferMethod.Move, 0); // set the option to only move files sftp.Settings.MultiFileMoveMode = MoveMode.FilesOnly; // move files from server to client // (create directory structure at the client; keep it at the server as well) sftp.Download("/MyData/*", @"C:\MyData", 0, TransferMethod.Move, 0);
' this is the default mode (it's not necessary to set it) sftp.Settings.MultiFileMoveMode = MoveMode.All ' move whole directory "Exports" to the server sftp.Upload("C:\MyData\Exports", "/MyData", 0, TransferMethod.Move, 0) ' set the option to only move files sftp.Settings.MultiFileMoveMode = MoveMode.FilesOnly ' move files from server to client ' (create directory structure at the client keep it at the server as well) sftp.Download("/MyData/*", "C:\MyData", 0, TransferMethod.Move, 0)
Tip: To move files at the server (not between the client and a server), use
Rename
method instead.
Symlink detection and handling
Upload
and Download
methods
automatically detect symbolic links. To specify whether to resolve them, skip them or raise an error,
set Settings.MultiFileLinkMode
to:
FollowLinks
- resolves links and processes them like other itemsSkipLinks
- skips links and continue processingThrowExceptionOnLinks
- cancels processing by throwing an exception
// don't process links sftp.Settings.MultiFileLinkMode = LinkProcessingMode.SkipLinks; // download directory without links sftp.Download("/MyData/*", @"C:\MyData");
' don't process links sftp.Settings.MultiFileLinkMode = LinkProcessingMode.SkipLinks ' download directory without links sftp.Download("/MyData/*", "C:\MyData")
Multi-file methods contain built-in infinite loop detection. When an infinite loop in the directory structure is detected,
a ProblemDetected
event is raised with ProblemDetectedEventArgs.ProblemType
set to InfiniteLoopDetected
.
Back to feature list...