Check if file exist on SFTP remote server

The following code will down the file named file.txt from the SFTP remote server to the local machine.

 sftp.Get("/usr/mine/file.txt" , "C:/Folder/");

What i want to do is to Check if the file file.txt exist in the remote server or not. How can i do this check. Help

I am using SharpSSH

Answers


You could consider just taking the small hit and attempting to download the file. If it doesn't exist, an exception should be thrown and you can just catch it and move on. Checking for a file's existence is a volatile situation so in the majority of cases it's best to try and perform your action.


This should do the trick.

 using (var sftp = new SftpClient(host, username, password))
        {
            try
            {

                sftp.Connect();
                MessageBox.Show(sftp.Exists(remoteDirectory).ToString());
            }
            catch (Exception Sftpex)
            {
                MessageBox.Show(Sftpex.ToString());
            }
        }

I'm doing this by using .GetFileList and reading the values into an ArrayList and then looping thru each value, adding the filename to a list box. I then check my input file against the list box to see if it exists. Sample code below to add the .GetFileList values into an ArrayList and then into a list box.

BTW - this is VB.NET :)

Dim InputFileList As ArrayList = oSftp.GetFileList(frmOptions.tbFTPInboundFolder.Text)
 For Each f In InputFileList
                If f.ToString() <> "." AndAlso f.ToString <> ".." Then
                    frmMain.lbFTPInputDirectory.Items.Add(f)
                End If
            Next

Need Your Help

Node.js List All Modules Required in a File

javascript node.js mocha

I must be missing something here. I have a mocha test file that is set up like so: