Search code examples
vb.netsftpjschsharpssh

SharpSSH Progress and Rename


Using the library to connect to a remote server and copy a file. I have the process working fairly well but have some smaller things which I cant seem to resolve as documentation for the library is fairly thin.

I have two routines working. One using the Tamir.SharpSsh class and the other using the Tamir.SharpSsh.jsch class.

  1. Using the Tamir.SharpSsh class I am able to copy the file from the local server to the remote server and tap into the pogress event. What I can't do is determine if a particular file on the remote server say /Report/data.txt exists on the server. I need to take different actions if it exists or if doesn't exist. Also how would I rename a file on the remote server. Ive tried using SshExec with a 'rename', 'rn', and 'mv' command and it doesn't seem to work.

  2. Using the Tamir.SharpSsh.jsch I can copy the file from the local server to the remote server. I can also rename the file on the remote server. What I cant do with this class is to tap into the progress event to keep track of the copy progress. Also I cant seem to find a good way to test to see if a particular file exists on the server. What I have come up with is crude and the only way that I could come up with to test and that is to use

        Dim c As ChannelSftp
        Dim vct As Tamir.SharpSsh.java.util.Vector = c.ls(sRemoteFile)
        Dim cnt As Integer = vct.Count
    

When one or more file exists I get a count no problem. When there is no file then an exception is thrown.

Anyway, I have the routines working its just some minor things I need help with.

tia AGP


Solution

  • You can call the Tamir.SharpSsh.Sftp.GetFile method using the path of the file you want to check exists (example in C#, sorry):

    private bool FileExists(string filePath)
    {
        try
        {
            SftpConnection connection = new SftpConnection(_host, _username, _password);
            connection.Connect(_port);
            connection.Get(filePath, _toDir);
        }
        catch(JSchException)
        {
            return false;
        }
        return true;
    }
    

    I have also noticed a few other issues through my use of this library - like a lack of a GetFileInfo method or recursive Gets and Puts. But overall it get the job done.

    The simple fact is, Tamir.SharpSsh can't rename a file remotely - it just does not implement that functionality. You can purchase a better library that has far more features, such as:

    • Kellerman Software .NET SFTP Library
    • wodSFTP.NET
    • Rebex SFTP for .NET
    • edtFTPnet/PRO

    or you could extend SharpSsh, since it is open source.