Differences
This shows you the differences between the selected revisions of the page.
2017-12-21 | 2018-01-17 | ||
generate url commands have been renamed while ago (martin) | 5.12.1 Bug 1594: Custom error handling instead of default abort can be optionally implemented for batch operations in .NET assembly. (martin) | ||
Line 1: | Line 1: | ||
====== Recursively download directory tree with custom error handling ====== | ====== Recursively download directory tree with custom error handling ====== | ||
- | The simplest way to download a directory tree is by using ''[[library_session_getfiles|Session.GetFiles]]'', providing path to a root of the tree as a source. This way a batch operation however stops on any error. If you need to use a custom error handling, for example to ignore any error and just continue, you need to implement walking the tree explicitly. | + | The simplest way to download a directory tree is by using ''[[library_session_getfiles|Session.GetFiles]]'', providing path to a root of the tree as a source. This way a batch operation however stops on any error by default. |
+ | |||
+ | The following two approaches show how to override the default behaviour. | ||
+ | |||
+ | ===== Handling Session.QueryReceived event ===== | ||
+ | |||
+ | You can choose how an error is processed by handling ''[[library_session_queryreceived|Session.QueryReceived]]'' event. &beta_feature | ||
+ | |||
+ | This example shows a basic implementation that outputs any errors encountered and continues. | ||
+ | |||
+ | ==== C# Example ==== | ||
+ | |||
+ | <code csharp> | ||
+ | session.QueryReceived += (sender, e) => | ||
+ | { | ||
+ | Console.WriteLine("Error: {0}", e); | ||
+ | e.Continue(); | ||
+ | }; | ||
+ | |||
+ | session.Open(sessionOptions); | ||
+ | |||
+ | session.GetFiles("/home/user/*", @"d:\download\").Check(); | ||
+ | </code> | ||
+ | |||
+ | ==== VB.NET Example ==== | ||
+ | |||
+ | <code vbnet> | ||
+ | AddHandler session.QueryReceived, | ||
+ | Sub(sender As Object, e As QueryReceivedEventArgs) | ||
+ | Console.WriteLine("Error: {0}", e.Message) | ||
+ | e.Continue() | ||
+ | End Sub | ||
+ | |||
+ | session.Open(sessionOptions) | ||
+ | |||
+ | session.GetFiles("/home/user/*", "d:\download\").Check() | ||
+ | </code> | ||
+ | |||
+ | ==== PowerShell Example ==== | ||
+ | |||
+ | <code powershell> | ||
+ | $session.add_QueryReceived( { | ||
+ | Write-Host "Error: $($_.Message)" | ||
+ | $_.Continue() | ||
+ | } ) | ||
+ | |||
+ | $session.Open($sessionOptions) | ||
+ | |||
+ | $session.GetFiles("/home/user/*", "d:\download\").Check() | ||
+ | </code> | ||
+ | |||
+ | ==== Upload ==== | ||
+ | |||
+ | The same mechanism can be used for uploads with ''[[library_session_putfiles|Session.PutFiles]]'' or synchronization with ''[[library_session_synchronizedirectories|Session.SynchronizeDirectories]]''. | ||
+ | |||
+ | |||
+ | ===== Explicit Implementation of a File Tree Download ===== | ||
+ | |||
+ | In case you need even more control over a download process, you can implement walking of a directory tree explicitly and handle each file individually, as you need. | ||
This example shows a basic implementation that outputs any errors encountered and continues. | This example shows a basic implementation that outputs any errors encountered and continues. | ||
- | ===== C# Example ===== | + | ==== C# Example ==== |
<code csharp> | <code csharp> | ||
Line 88: | Line 146: | ||
</code> | </code> | ||
- | ===== [[powershell]] PowerShell Example ===== | + | ==== [[powershell]] PowerShell Example ==== |
<code powershell> | <code powershell> | ||
Line 172: | Line 230: | ||
</code> | </code> | ||
- | ===== Upload ===== | + | ==== Upload ==== |
For an example of walking a local tree to upload files individually, see [[library_example_moves_files_keeping_directory_structure|Recursively move files in directory tree to/from SFTP/FTP server while preserving source directory structure]]. | For an example of walking a local tree to upload files individually, see [[library_example_moves_files_keeping_directory_structure|Recursively move files in directory tree to/from SFTP/FTP server while preserving source directory structure]]. | ||
The upload example calls ''[[library_operationresultbase#check|OperationResultBase.Check]]'', so it aborts on any error. Just replace the call with ''[[library_operationresultbase#issuccess|OperationResultBase.IsSuccess]]'' test as this example does. See [[library_session#results|Capturing Results of Operations]] | The upload example calls ''[[library_operationresultbase#check|OperationResultBase.Check]]'', so it aborts on any error. Just replace the call with ''[[library_operationresultbase#issuccess|OperationResultBase.IsSuccess]]'' test as this example does. See [[library_session#results|Capturing Results of Operations]] |