Differences

This shows you the differences between the selected revisions of the page.

library_session_fileexists 2012-01-10 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.
- +
-&future_feature+
===== Syntax ===== ===== Syntax =====
-<code csharp>+<code csharp *>
public bool FileExists(string path) public bool FileExists(string path)
 +</code>
 +
 +<code vbnet *>
 +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 21: Line 23:
| [[library_sessionlocalexception|SessionLocalException]] | Error communicating with ''[[executables#winscp.com|winscp.com]]''. \\ See the exception documentation for details. | | [[library_sessionlocalexception|SessionLocalException]] | Error communicating with ''[[executables#winscp.com|winscp.com]]''. \\ See the exception documentation for details. |
| TimeoutException | Timeout waiting for ''winscp.com'' to respond. | | TimeoutException | Timeout waiting for ''winscp.com'' to respond. |
 +
 +===== [[example]] Examples =====
 +
 +==== [[csharp]] C# Example ====
 +<code csharp>
 +using System;
 +using WinSCP;
 +
 +class Example
 +{
 +    public static int Main()
 +    {
 +        try
 +        {
 +            // Setup session options
 +            SessionOptions sessionOptions = new SessionOptions
 +            {
 +                Protocol = Protocol.Sftp,
 +                HostName = "example.com",
 +                UserName = "user",
 +                Password = "mypassword",
 +                SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
 +            };
 +
 +            using (Session session = new Session())
 +            {
 +                // Connect
 +                session.Open(sessionOptions);
 +
 +                const string remotePath = "/home/user/test.txt";
 +                if (session.FileExists(remotePath))
 +                {
 +                    Console.WriteLine("File {0} exists", remotePath);
 +                    // Now you can e.g. download file using session.GetFiles
 +
 +                    return 0;
 +                }
 +                else
 +                {
 +                    Console.WriteLine("File {0} does not exists", remotePath);
 +                    return 1;
 +                }
 +            }
 +        }
 +        catch (Exception e)
 +        {
 +            Console.WriteLine("Error: {0}", e);
 +            return 2;
 +        }
 +    }
 +}
 +</code>
 +
 +==== [[vbnet]] VB.NET Example ====
 +<code vbnet>
 +Imports WinSCP
 +
 +Friend Class Example
 +
 +    Public Shared Function Main() As Integer
 +
 +        Try
 +            ' Setup session options
 +            Dim sessionOptions As New SessionOptions
 +            With sessionOptions
 +                .Protocol = Protocol.Sftp
 +                .HostName = "example.com"
 +                .UserName = "user"
 +                .Password = "mypassword"
 +                .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
 +            End With
 +
 +            Using session As New Session
 +                ' Connect
 +                session.Open(sessionOptions)
 +
 +                Const remotePath As String = "/home/user/test.txt"
 +
 +                If session.FileExists(remotePath) Then
 +                    Console.WriteLine("File {0} exists", remotePath)
 +                    ' Now you can e.g. download file using session.GetFiles
 +                   
 +                    Return 0
 +                Else
 +                    Console.WriteLine("File {0} does not exist", remotePath)
 +                    Return 1
 +                End If
 +            End Using
 +
 +        Catch e As Exception
 +            Console.WriteLine("Error: {0}", e)
 +            Return 2
 +        End Try
 +
 +    End Function
 +
 +End Class
 +</code>
 +
 +==== [[powershell]] PowerShell Example ====
 +Learn more about [[library_powershell|using WinSCP .NET assembly from PowerShell]].
 +
 +<code powershell>
 +try
 +{
 +    # Load WinSCP .NET assembly
 +    Add-Type -Path "WinSCPnet.dll"
 +
 +    # Setup session options
 +    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
 +        Protocol = [WinSCP.Protocol]::Sftp
 +        HostName = "example.com"
 +        UserName = "user"
 +        Password = "mypassword"
 +        SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
 +    }
 +
 +    $session = New-Object WinSCP.Session
 +
 +    try
 +    {
 +        # Connect
 +        $session.Open($sessionOptions)
 +
 +        $remotePath = "/home/user/test.txt"
 +
 +        if ($session.FileExists($remotePath))
 +        {
 +            Write-Host "File $remotePath exists"
 +            # Now you can e.g. download file using session.GetFiles
 +
 +            exit 0
 +        }
 +        else
 +        {
 +            Write-Host "File $remotePath does not exist"
 +            exit 1
 +        }
 +    }
 +    finally
 +    {
 +        # Disconnect, clean up
 +        $session.Dispose()
 +    }
 +}
 +catch
 +{
 +    Write-Host "Error: $($_.Exception.Message)"
 +    exit 2
 +}
 +</code>
 +
 +==== [[jscript]] JScript (WSH) Example ====
 +In this example the JScript script is embedded into WSF file, to allow [[library_com_wsh#enums|access to enumeration values]].
 +
 +<code javascript>
 +<job>                                                             
 +<reference object="WinSCP.Session"/>
 +<script language="JScript">
 +
 +try
 +{
 +    // Setup session options
 +    var sessionOptions = WScript.CreateObject("WinSCP.SessionOptions");
 +    sessionOptions.Protocol = Protocol_Sftp;
 +    sessionOptions.HostName = "example.com";
 +    sessionOptions.UserName = "user";
 +    sessionOptions.Password = "mypassword";
 +    sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...";
 +   
 +    var session = WScript.CreateObject("WinSCP.Session");
 +
 +    var result;
 +
 +    try
 +    {
 +        // Connect
 +        session.Open(sessionOptions);
 +
 +        var remotePath = "/home/user/test.txt";
 +
 +        if (session.FileExists(remotePath))
 +        {
 +            WScript.Echo("File " + remotePath + " exists");
 +            // Now you can e.g. download file using session.GetFiles
 +
 +            result = 0;
 +        }
 +        else
 +        {
 +            WScript.Echo("File " + remotePath + " does not exist");
 +            result = 1;
 +        }
 +    }
 +    finally
 +    {
 +        // Disconnect, clean up
 +        session.Dispose();
 +    }
 +
 +    WScript.Quit(result);
 +}
 +catch (e)
 +{
 +    WScript.Echo("Error: " + e.message);
 +    WScript.Quit(2);
 +}
 +
 +</script>
 +</job>
 +</code>
 +
 +
 +==== [[vbscript]] VBScript (WSH) Example ====
 +In this example the VBScript script is embedded into WSF file, to allow [[library_com_wsh#enums|access to enumeration values]].
 +
 +<code vb>
 +<job>                                                             
 +<reference object="WinSCP.Session"/>
 +<script language="VBScript">
 +
 +Option Explicit
 +
 +' Setup session options
 +Dim sessionOptions
 +Set sessionOptions = WScript.CreateObject("WinSCP.SessionOptions")
 +With sessionOptions
 +    .Protocol = Protocol_Sftp
 +    .HostName = "example.com"
 +    .UserName = "user"
 +    .Password = "mypassword"
 +    .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
 +End With
 +
 +Dim session
 +Set session = WScript.CreateObject("WinSCP.Session")
 +
 +' Connect
 +session.Open sessionOptions
 +
 +remotePath = "/home/user/test.txt"
 +
 +Dim fs
 +Set fs = WScript.CreateObject("Scripting.FileSystemObject")
 +       
 +If session.FileExists(remotePath) Then
 +    WScript.Echo "File " & remotePath & " exists"
 +    ' Now you can e.g. download file using session.GetFiles
 +Else
 +    WScript.Echo "File " & remotePath & " does not exist"
 +End If
 +
 +' Disconnect, clean up
 +session.Dispose
 +
 +</script>
 +</job>
 +</code>
 +
 +==== Real-Life Examples ====
 +
 +  * [[library_example_check_existence_timestamp|*]];
 +  * [[library_example_moves_files_keeping_directory_structure|*]].

Last modified: by martin