Differences
This shows you the differences between the selected revisions of the page.
library_session_fileexists 2013-05-09 | library_session_fileexists 2022-06-16 (current) | ||
Line 1: | Line 1: | ||
====== Session.FileExists Method ====== | ====== Session.FileExists Method ====== | ||
- | Checks for existence of remote file.· | + | Checks for existence of remote file or directory. |
===== Syntax ===== | ===== Syntax ===== | ||
Line 8: | Line 8: | ||
<code vbnet *> | <code vbnet *> | ||
- | Public Function FileExists(ByVal path As String) As Boolean | + | Public Function FileExists(path As String) As Boolean |
</code> | </code> | ||
==== Parameters ==== | ==== Parameters ==== | ||
^ Name ^ Description ^ | ^ Name ^ Description ^ | ||
- | | string path | Full path to remote file. | | + | | string path | Full path to remote file.((Note that you cannot use [[library_wildcard|wildcards]] here.)) | |
==== Return Value ==== | ==== Return Value ==== | ||
Line 25: | Line 25: | ||
===== [[example]] Examples ===== | ===== [[example]] Examples ===== | ||
+ | |||
==== [[csharp]] C# Example ==== | ==== [[csharp]] C# Example ==== | ||
<code csharp> | <code csharp> | ||
Line 37: | Line 38: | ||
{ | { | ||
// Setup session options | // Setup session options | ||
- | SessionOptions sessionOptions = new SessionOptions { | + | SessionOptions sessionOptions = new SessionOptions |
+ | ···········{ | ||
Protocol = Protocol.Sftp, | Protocol = Protocol.Sftp, | ||
HostName = "example.com", | HostName = "example.com", | ||
UserName = "user", | UserName = "user", | ||
Password = "mypassword", | Password = "mypassword", | ||
- | SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
}; | }; | ||
Line 76: | Line 78: | ||
==== [[vbnet]] VB.NET Example ==== | ==== [[vbnet]] VB.NET Example ==== | ||
<code vbnet> | <code vbnet> | ||
- | Imports System | ||
Imports WinSCP | Imports WinSCP | ||
Line 91: | Line 92: | ||
.UserName = "user" | .UserName = "user" | ||
.Password = "mypassword" | .Password = "mypassword" | ||
- | .SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
End With | End With | ||
- | Using session As Session = New Session | + | Using session As New Session |
' Connect | ' Connect | ||
session.Open(sessionOptions) | session.Open(sessionOptions) | ||
Line 122: | Line 123: | ||
==== [[powershell]] PowerShell Example ==== | ==== [[powershell]] PowerShell Example ==== | ||
+ | Learn more about [[library_powershell|using WinSCP .NET assembly from PowerShell]]. | ||
+ | |||
<code powershell> | <code powershell> | ||
try | try | ||
{ | { | ||
# Load WinSCP .NET assembly | # Load WinSCP .NET assembly | ||
- | [Reflection.Assembly]::LoadFrom("WinSCP.dll") | Out-Null | + | Add-Type -Path "WinSCPnet.dll" |
# Setup session options | # Setup session options | ||
- | $sessionOptions = New-Object WinSCP.SessionOptions | + | $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ |
- | ···$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp | + | ········Protocol = [WinSCP.Protocol]::Sftp |
- | $sessionOptions.HostName = "example.com" | + | ·······HostName = "example.com" |
- | $sessionOptions.UserName = "user" | + | ·······UserName = "user" |
- | $sessionOptions.Password = "mypassword" | + | ·······Password = "mypassword" |
- | $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | ·······SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
+ | } | ||
$session = New-Object WinSCP.Session | $session = New-Object WinSCP.Session | ||
Line 147: | Line 151: | ||
if ($session.FileExists($remotePath)) | if ($session.FileExists($remotePath)) | ||
{ | { | ||
- | Write-Host("File {0} exists" -f $remotePath) | + | Write-Host·"File $remotePath exists" |
# Now you can e.g. download file using session.GetFiles | # Now you can e.g. download file using session.GetFiles | ||
Line 154: | Line 158: | ||
else | else | ||
{ | { | ||
- | Write-Host ("File {0} does not exist" -f $remotePath) | + | Write-Host "File $remotePath does not exist" |
exit 1 | exit 1 | ||
} | } | ||
Line 164: | Line 168: | ||
} | } | ||
} | } | ||
- | catch [Exception] | + | catch |
{ | { | ||
- | Write-Host $_.Exception.Message | + | Write-Host "Error: $($_.Exception.Message)" |
exit 2 | exit 2 | ||
} | } | ||
Line 187: | Line 191: | ||
sessionOptions.UserName = "user"; | sessionOptions.UserName = "user"; | ||
sessionOptions.Password = "mypassword"; | sessionOptions.Password = "mypassword"; | ||
- | sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"; | + | sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."; |
var session = WScript.CreateObject("WinSCP.Session"); | var session = WScript.CreateObject("WinSCP.Session"); | ||
Line 250: | Line 254: | ||
.UserName = "user" | .UserName = "user" | ||
.Password = "mypassword" | .Password = "mypassword" | ||
- | .SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
End With | End With | ||
Line 277: | Line 281: | ||
</job> | </job> | ||
</code> | </code> | ||
+ | |||
+ | ==== Real-Life Examples ==== | ||
+ | |||
+ | * [[library_example_check_existence_timestamp|*]]; | ||
+ | * [[library_example_moves_files_keeping_directory_structure|*]]. | ||