Session.FileExists Method

Checks for existence of remote file.

Syntax

C#
public bool FileExists(string path)
VB.NET
Public Function FileExists(ByVal path As String) As Boolean

Parameters

Name Description
string path Full path to remote file.1)

Return Value

true if file exists, false otherwise.

Advertisements:

Exceptions

Exception Condition
InvalidOperationException Session is not opened.
SessionLocalException Error communicating with winscp.com.
See the exception documentation for details.
TimeoutException Timeout waiting for winscp.com to respond.

Examples

C# Example

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 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
            };
 
            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;
        }
    }
}

VB.NET Example

Imports System
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 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
            End With
 
            Using session As Session = 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

PowerShell Example

Learn more about using WinSCP .NET assembly from PowerShell.

try
{
    # Load WinSCP .NET assembly
    [Reflection.Assembly]::LoadFrom("WinSCPnet.dll") | Out-Null
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.HostName = "example.com"
    $sessionOptions.UserName = "user"
    $sessionOptions.Password = "mypassword"
    $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        $remotePath = "/home/user/test.txt"
 
        if ($session.FileExists($remotePath))
        {
            Write-Host ("File {0} exists" -f $remotePath)
            # Now you can e.g. download file using session.GetFiles
 
            exit 0
        }
        else
        {
            Write-Host ("File {0} does not exist" -f $remotePath)
            exit 1
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 2
}

JScript (WSH) Example

In this example the JScript script is embedded into WSF file, to allow access to enumeration values.

<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 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx";
     
    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>

VBScript (WSH) Example

In this example the VBScript script is embedded into WSF file, to allow access to enumeration values.

<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 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
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>
1) Note that you cannot use wildcards here.
 
  library_session_fileexists.txt · Last modified: by prikryl
 

Search Documentation

This page

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

Associations

Site design by Black Gate