Session.FileExists Method

Checks for existence of remote file or directory.

Advertisement

Syntax

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

Parameters

Name Description
string path Full path to remote file.1

Return Value

true if file exists, false otherwise.

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.

Advertisement

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 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;
        }
    }
}

Advertisement

VB.NET Example

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

PowerShell Example

Learn more about using WinSCP .NET assembly from PowerShell.

Advertisement

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
}

JScript (WSH) Example

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

Advertisement

<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>

Advertisement

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 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>

Real-Life Examples

Advertisement

  1. Note that you cannot use wildcards here.Back

Last modified: by martin