Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Some Problems

I've tested your code and it works correctly.

Please upgrade to the latest version of the assembly and attach a full session log file showing the problem.

To generate log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.
POL

Re: Some Problems

martin wrote:

Sorry, I do not understand. How do you skip a file?

Your problem seems like a generic programming one, rather than a problem with WinSCP .NET assembly.


Hi

i have made an .NET Sample and the problem is the same:

Imports System

Imports WinSCP


Public Class sFTP
    Dim _mySession As Session

    Public Sub New()
        Dim RPath As String = "**"
        Dim LPath As String = "C:\tmp"
        Dim OK As Boolean = Connect("**", "**", "**", "**")

        Dim directory As RemoteDirectoryInfo = _mySession.ListDirectory(RPath)

        Dim fileinfo As RemoteFileInfo


' There a 7 files on the sftp server
' test1.csv, test2.csv, test3.csv, test4.csv, test1.txt, test2,txt, test3.txt
' At first i have to download only *.txt files
' For every txt files there a csv file
' For Each downloaded txt file i have to download the existing csv file (This is important!)


'First Step download only the text files
        For Each fileinfo In directory.Files
            If (Right(fileinfo.Name, 3) <> "csv" And fileinfo.Name <> "." And fileinfo.Name <> "..") Then
                OK = _mySession.GetFiles(RPath & "/" & fileinfo.Name.ToString, LPath & "\" & fileinfo.Name.ToString, True).IsSuccess
            End If

            'This works!
            'If (fileinfo.Name <> "." And fileinfo.Name <> "..") Then 'Right(fileinfo.Name, 3) <> "csv" And
            '    OK = _mySession.GetFiles(RPath & "/" & fileinfo.Name.ToString, LPath & "\" & fileinfo.Name.ToString, True).IsSuccess
            'End If
        Next



    End Sub

    Public Function Connect(HostName As String, User As String, Pw As String, HostKey As String) As Boolean

        Try
            Dim mySessionOptions As New SessionOptions

            With mySessionOptions
                .Protocol = Protocol.Sftp
                .HostName = HostName
                .UserName = User
                .Password = Pw
                .SshHostKey = HostKey
            End With

            _mySession = New Session
            _mySession.Open(mySessionOptions)

            Return True

        Catch e As Exception

            Return False

        End Try

    End Function

End Class


Hope u can help me
POL

Re: Some Problems

martin wrote:

Sorry, I do not understand. How do you skip a file?

Your problem seems like a generic programming one, rather than a problem with WinSCP .NET assembly.


HI, thanks for your answer.
In the Code sample i didnt skip a file. But if i do that next in the Collection will not downloading.
For i = 1 To oFls.nCount 

        'Skipping like that
        If right(ofls.ofile(i).sname,3) <> "csv" then
            bOK = o.bGetFile(Me.txtDownRDir & "/" & oFls.oFile(i).sName, Me.txtDownLDir & oFls.oFile(i).sName, True)
         End if
    next i
[/code]
martin

Re: Some Problems

Sorry, I do not understand. How do you skip a file?

Your problem seems like a generic programming one, rather than a problem with WinSCP .NET assembly.
POL

Some Problems

HI @all,

i have a problem and did not know how to resolve.
I use VB6 and have registered the WINSCP (Creating the tlb file ...)

i have a class named cSFTP:
a typical function to connect (bConnect)
a Property to get all all files (Property Get)

Connecting to the Host works fine (bConnect)
Getting all Files from SFTP works fine (Property Get oDirectoryFiles)

If i still download ALL FILES (o.bGetFile) in the loop it works fine
But if i wont download some files and skip in the loop a file it doenst work. i dont know why

i hope someone can help me

'Code From frmMain
    Dim o           As New cSFTP

    bOK = o.bConnect(Protocol_Sftp, Me.txtHost, Me.txtUser, Me.txtPW, Me.txtHKey, False, sErr)
   
'oFls is still class with a collection and reference to an new class for the file
Set oFls.oFiles = o.oDirectoryColFileList(Me.txtDownRDir, isNew, "")


'/// HERE IS THE PROBLEM
'/// Skipping an file, the next file will not download
    For i = 1 To oFls.nCount
'If i skip a file bgetfile (mySession.getfiles(sRemoteFileWithPath, sTargetPath, bRemoveFile).IsSuccess) returns false
        bOK = o.bGetFile(Me.txtDownRDir & "/" & oFls.oFile(i).sName, Me.txtDownLDir & oFls.oFile(i).sName, True)
    next i


'Code from Class


Dim mySessionOptions        As SessionOptions
Dim mySession               As Session
Dim myDirectoryInfo         As RemoteDirectoryInfo
Dim myFileInfo              As RemoteFileInfo
Dim myFiles                 As Collection

Const FileExtensionOK    As String = "OK"

Public Enum enmProtocol
    Protocol_Sftp = 0
    Protocol_Scp = 1
    Protocol_Ftp = 2
End Enum

Public Enum enmFileStatus
        isNew = 1
        isDownloaded
        All
End Enum


Private Sub Class_Terminate()
   
    If Not mySession Is Nothing Then
        mySession.dispose
    End If
   
    Set mySessionOptions = Nothing
    Set mySession = Nothing
    Set myDirectoryInfo = Nothing
    Set myFileInfo = Nothing
    Set myFiles = Nothing
   
End Sub

Property Get oSession() As Session

    Set oSession = mySession
   
End Property

Property Get oDirectoryFiles(Optional sPath As String = "/") As RemoteDirectoryInfo

    If myDirectoryInfo Is Nothing Then
        Set oDirectoryFiles = mySession.ListDirectory(sPath)
    End If
   
End Property


Property Get oDirectoryColFileList(Optional sPath As String = "/", Optional FileStatus As enmFileStatus = 1, Optional sFileExt As String = "") As Collection
On Error GoTo errHandle

    Dim oUBSFile        As cUBSFile
    Dim bDownload       As Boolean
   
    Set oDirectoryColFileList = New Collection
    goLog.Log = "Ermittle Remote Dateien .."
    If myDirectoryInfo Is Nothing Then
        Set myDirectoryInfo = mySession.ListDirectory(sPath)
        goLog.Log = "Ermittle Remote Dateien ..OK"
    End If
   
    goLog.Log = "Anzahl Remote Dateien .. " & oDirectoryColFileList.Count
    For Each myFileInfo In myDirectoryInfo.Files
        bDownload = False
        If myFileInfo.Name = "." Or _
           myFileInfo.Name = ".." Then
        Else
            If FileStatus = All Then
                bDownload = True
            ElseIf FileStatus = isDownloaded Then
                If UCase(Right(myFileInfo.Name, 2)) = UCase(FileExtensionOK) Then
                    bDownload = True
                End If
            ElseIf FileStatus = isNew Then
                If UCase(Right(myFileInfo.Name, 3)) <> UCase(FileExtensionOK) Then
                    bDownload = True
                End If
            End If
           
            If sFileExt <> "" Then
                If UCase(Right(myFileInfo.Name, Len(sFileExt))) <> UCase(sFileExt) Then
                    bDownload = False
                End If
            End If
           
            If bDownload Then
                Set oUBSFile = New cUBSFile
                With oUBSFile
                    .bIsDirectory = myFileInfo.isdirectory
                    .dtLastWriteTime = myFileInfo.lastwritetime
                    .nLen = myFileInfo.length
                    .sName = myFileInfo.Name
                End With
                oDirectoryColFileList.Add oUBSFile, "K" & oUBSFile.sName
                Set oUBSFile = Nothing
            End If
        End If
    Next
   
   
    goLog.Log = "Ermittle Remote Dateien .. abgeschlossen"
    Exit Property
   
errHandle:
    goLog.Log = "Fehler bei Ermittlung Remote Dateien .." & Err.Number & "." & Err.Description
    Err.Clear
   
End Property

Public Function bConnect(m_enmProtocol As enmProtocol, sHostName As String, _
                         sUserName As String, sPWD As String, _
                         Optional sHostKey As String = "", _
                         Optional bReConnectIfConnected As Boolean = False, _
                         Optional sErr As String = "") As Boolean
                     
On Error GoTo err_Init
    goLog.Log = "Verbindung .."
    If mySession Is Nothing Then
        Set mySession = New Session
    Else
        If bReConnectIfConnected Then
            mySession.dispose
            Set mySession = Nothing
            Set mySessionOptions = Nothing
            Set mySession = New Session
        End If
    End If
   
    Set mySessionOptions = New SessionOptions
    With mySessionOptions
        .Protocol = m_enmProtocol
        .HostName = sHostName
        .UserName = sUserName
        .Password = sPWD
        .SshHostKey = sHostKey
        '.GiveUpSecurityAndAcceptAnySshHostKey = True
    End With
    mySession.Open mySessionOptions
    bConnect = True
    goLog.Log = "Verbindung ..OK"
Exit Function

err_Init:
    goLog.Log = "Fehler beim Verbinden " & Err.Number & "." & Err.Description
    sErr = Err.Number & "-" & Err.Description
    Err.Clear
    mySession.dispose
    Set mySession = Nothing
    Set mySessionOptions = Nothing
    bConnect = False
End Function

Public Function bGetFile(sRemoteFileWithPath As String, sTargetPath As String, _
                         Optional bRemoveFile As Boolean = False, _
                         Optional bSetStatus As Boolean = True) As Boolean
                         
    On Error GoTo err_GetFile
    goLog.Log = "Ermittlung der Remote Daten"
    If mySession Is Nothing Then
        bGetFile = False
    Else
        bGetFile = mySession.getfiles(sRemoteFileWithPath, sTargetPath, bRemoveFile).IsSuccess
        goLog.Log = "Lade Datei .."

        If (bGetFile And bSetStatus) And Not bRemoveFile Then
            Call bRenameFile(sRemoteFileWithPath, sRemoteFileWithPath & "." & FileExtensionOK)
        End If
    End If
    goLog.Log = "Ermittlung abgeschlossen"
    Exit Function
   
err_GetFile:
    goLog.Log = "Fehler bei der Ermittlung der Remote Daten " & Err.Number & "." & Err.Description
    Err.Clear
    bGetFile = False
End Function

Public Function bPutFile(sRemotePath As String, sLocalPath As String, _
                         Optional sFile As String = "*.*", _
                         Optional bRemoveFile As Boolean = False, _
                         Optional bSetStatus As Boolean = True, _
                         Optional sErr As String) As Boolean
                         
    On Error GoTo err_PutFile
   
    Dim myTransferOptions   As New TransferOptions
    Dim transferResult      As TransferOperationResult
    Dim transfer            As TransferEventArgs
    Dim l                   As Long
   
    Dim bOK                 As Boolean
   
    goLog.Log = "Upload ... "
    If Right(sLocalPath, 1) <> "\" Then
        sLocalPath = sLocalPath & "\"
    End If
   
    If Right(sRemotePath, 1) <> "/" Then
        sRemotePath = sRemotePath & "/"
    End If
   
    bOK = gbDirExist(sLocalPath & DIRBACKUP)
    If Not bOK Then
        bOK = gbMkDir(sLocalPath & DIRBACKUP)
    End If
   
    myTransferOptions.TransferMode = TransferMode_Binary
   
    If mySession Is Nothing Then
        bPutFile = False
    Else
        goLog.Log = "Upload .. "
        Set transferResult = mySession.PutFiles(sLocalPath & sFile, sRemotePath, False, myTransferOptions)
        transferResult.Check
        For Each transfer In transferResult.Transfers
            bPutFile = (sLocalPath & sFile = transfer.FileName)
           
            If (bPutFile And bSetStatus) And Not bRemoveFile Then
                If bOK Then
                    goLog.Log = "Upload .. OK"
                    Call bReNameLocalFile(sLocalPath & sFile, sLocalPath & DIRBACKUP & sFile & "." & FileExtensionOK)
                Else
                    goLog.Log = "Upload .. Nicht OK"
                End If
            End If
        Next
    End If
   
    Exit Function
   
err_PutFile:
    goLog.Log = "Fehler bei Upload .. " & Err.Number & "." & Err.Description
    Err.Clear
    bPutFile = False
End Function

Public Function bRenameFile(sRemotePathFile As String, sRemotePathFileNew As String) As Boolean
    On Error GoTo err_Rename
       
    goLog.Log = "Umbenennen Remote Datei .. "
    If mySession Is Nothing Then
        bRenameFile = False
        goLog.Log = "Umbenennen Remote Datei .. Nicht OK (Session getrennt)"
    Else
        Call mySession.MoveFile(sRemotePathFile, sRemotePathFileNew)
        goLog.Log = "Umbenennen Remote Datei .. OK"
        bRenameFile = True
    End If
    Exit Function
   
err_Rename:
    goLog.Log = "Fehler beim Umbenennen .. " & Err.Number & "." & Err.Description
    Err.Clear
    bRenameFile = False
End Function

Public Function bReNameLocalFile(sPathFile As String, sPathFileNew As String) As Boolean
On Error GoTo errHandle
   
    goLog.Log = "Umbenennen Lokale Datei .."
    FileCopy sPathFile, sPathFileNew
    Kill sPathFile
    goLog.Log = "Umbenennen Lokale Datei .. OK"
errHandle:
    goLog.Log = "Fehler beim Umbenennen Lokale Datei .." & Err.Number & "." & Err.Description
    bReNameLocalFile = (Err.Number = 0)
    Err.Clear
End Function