Differences
This shows you the differences between the selected revisions of the page.
library_example_recursive_download_custom_error_handling 2018-01-17 | library_example_recursive_download_custom_error_handling 2022-06-16 (current) | ||
Line 4: | Line 4: | ||
The following two approaches show how to override the default behaviour. | The following two approaches show how to override the default behaviour. | ||
- | ===== Handling Session.QueryReceived event ===== | + | Particularly the first approach with ''[[library_session_queryreceived|Session.QueryReceived]]'' event is relevant for other batch operations as well, including ''[[library_session_putfiles|Session.PutFiles]]'' and ''[[library_session_synchronizedirectories|Session.SynchronizeDirectories]]''. |
- | You can choose how an error is processed by handling ''[[library_session_queryreceived|Session.QueryReceived]]'' event. &beta_feature | + | ===== [[queryreceived]] Handling Session.QueryReceived event ===== |
+ | |||
+ | You can choose how an error is processed by handling ''[[library_session_queryreceived|Session.QueryReceived]]'' event. | ||
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. | ||
Line 18: | Line 20: | ||
e.Continue(); | e.Continue(); | ||
}; | }; | ||
- | |||
- | session.Open(sessionOptions); | ||
session.GetFiles("/home/user/*", @"d:\download\").Check(); | session.GetFiles("/home/user/*", @"d:\download\").Check(); | ||
Line 32: | Line 32: | ||
e.Continue() | e.Continue() | ||
End Sub | End Sub | ||
- | |||
- | session.Open(sessionOptions) | ||
session.GetFiles("/home/user/*", "d:\download\").Check() | session.GetFiles("/home/user/*", "d:\download\").Check() | ||
</code> | </code> | ||
- | ==== PowerShell Example ==== | + | ==== [[event_powershell]] PowerShell Example ==== |
<code powershell> | <code powershell> | ||
Line 45: | Line 43: | ||
$_.Continue() | $_.Continue() | ||
} ) | } ) | ||
- | |||
- | $session.Open($sessionOptions) | ||
$session.GetFiles("/home/user/*", "d:\download\").Check() | $session.GetFiles("/home/user/*", "d:\download\").Check() | ||
Line 56: | Line 52: | ||
- | ===== Explicit Implementation of a File Tree Download ===== | + | ===== [[tree_download]] 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. | 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. | ||
Line 83: | Line 79: | ||
UserName = "user", | UserName = "user", | ||
Password = "mypassword", | Password = "mypassword", | ||
- | SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx..." | + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
}; | }; | ||
Line 95: | Line 91: | ||
// Enumerate files and directories to download | // Enumerate files and directories to download | ||
+ | var opts = WinSCP.EnumerationOptions.EnumerateDirectories | | ||
+ | WinSCP.EnumerationOptions.AllDirectories; | ||
IEnumerable<RemoteFileInfo> fileInfos = | IEnumerable<RemoteFileInfo> fileInfos = | ||
- | session.EnumerateRemoteFiles( | + | session.EnumerateRemoteFiles(remotePath, null, opts); |
- | ························remotePath, null, | + | |
- | EnumerationOptions.EnumerateDirectories | | + | |
- | EnumerationOptions.AllDirectories); | + | |
foreach (RemoteFileInfo fileInfo in fileInfos) | foreach (RemoteFileInfo fileInfo in fileInfos) | ||
{ | { | ||
string localFilePath = | string localFilePath = | ||
- | session.TranslateRemotePathToLocal( | + | RemotePath.TranslateRemotePathToLocal( |
fileInfo.FullName, remotePath, localPath); | fileInfo.FullName, remotePath, localPath); | ||
Line 119: | Line 114: | ||
Console.WriteLine("Downloading file {0}...", fileInfo.FullName); | Console.WriteLine("Downloading file {0}...", fileInfo.FullName); | ||
// Download file | // Download file | ||
+ | string remoteFilePath = RemotePath.EscapeFileMask(fileInfo.FullName); | ||
TransferOperationResult transferResult = | TransferOperationResult transferResult = | ||
- | session.GetFiles( | + | session.GetFiles(remoteFilePath, localFilePath); |
- | session.EscapeFileMask(fileInfo.FullName), localFilePath); | + | |
// Did the download succeeded? | // Did the download succeeded? | ||
Line 152: | Line 147: | ||
# Use Generate Session URL function to obtain a value for -sessionUrl parameter. | # Use Generate Session URL function to obtain a value for -sessionUrl parameter. | ||
[Parameter(Mandatory = $True)] | [Parameter(Mandatory = $True)] | ||
- | $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/", | + | $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/", |
[Parameter(Mandatory = $True)] | [Parameter(Mandatory = $True)] | ||
$remotePath, | $remotePath, | ||
Line 186: | Line 181: | ||
{ | { | ||
$localFilePath = | $localFilePath = | ||
- | $session.TranslateRemotePathToLocal($fileInfo.FullName, $remotePath, $localPath) | + | [WinSCP.RemotePath]::TranslateRemotePathToLocal( |
+ | ····················$fileInfo.FullName, $remotePath, $localPath) | ||
if ($fileInfo.IsDirectory) | if ($fileInfo.IsDirectory) | ||
Line 200: | Line 196: | ||
Write-Host "Downloading file $($fileInfo.FullName)..." | Write-Host "Downloading file $($fileInfo.FullName)..." | ||
# Download file | # Download file | ||
- | $transferResult = | + | $remoteFilePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName) |
- | ···················$session.GetFiles( | + | ···············$transferResult = $session.GetFiles($remoteFilePath, $localFilePath) |
- | ·······················$session.EscapeFileMask($fileInfo.FullName), $localFilePath) | + | |
# Did the download succeeded? | # Did the download succeeded? | ||
Line 232: | Line 227: | ||
==== 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|*]]. |
- | 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 the download example above does. See [[library_session#results|Capturing results of operations]]. |