WinSCP function modifications

bfuchs
bfuchs used Ask the Experts™
on
Hi Experts,

I have the function below that downloads files from a FTP server.

Option Explicit
 
Sub Example()
 
    Dim mySession As New Session
    
    ' Enable custom error handling
    On Error Resume Next
    
    Upload mySession
    
    ' Query for errors
    If Err.Number <> 0 Then
        MsgBox "Error: " & Err.Description
 
        ' Clear the error
        Err.Clear
    End If
     
    ' Disconnect, clean up
    mySession.Dispose
     
    ' Restore default error handling
    On Error GoTo 0
    
End Sub

Private Sub Download(ByRef mySession As Session)
 
    ' Setup session options
    Dim mySessionOptions As New SessionOptions
    With mySessionOptions
        .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
    
    ' Connect
    mySession.Open mySessionOptions
    
    ' Upload files
    Dim myTransferOptions As New TransferOptions
    myTransferOptions.TransferMode = TransferMode_Binary
     
    Dim transferResult As TransferOperationResult
    Set transferResult = _
        mySession.GetFiles("/home/user/*", "H:\FTP\", False, myTransferOptions)
     
    ' Throw on any error
    transferResult.Check
     
    ' Display results
    Dim transfer As TransferEventArgs
    For Each transfer In transferResult.Transfers
        MsgBox "Download of " & transfer.Filename & " succeeded"
    Next
    
End Sub

Open in new window


Now would need to do following modifications to it.

1- Currently it downloads all files into folder "H:\FTP\" regardless if file exists, would like to only download new files which are not there yet.
2- Should only download files that name contains wording PatChanges or SchChanges, except if they contain word "Full", which should be excluded regardless.

I tried the following but only partially working...
mySession.GetFiles("/Outbox/*PatChanges*", "H:\FTP\", False, myTransferOptions)

Open in new window


Thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Eddie ShipmanAll-around developer

Commented:
only partially working
Clarify...
Hi,
I did get it to download only files with wording PatChanges, however that's not enough, we need to exclude files with wording "Full", and also include files with with wording "SchChanges".
Thanks,
Ben
All-around developer
Commented:
Try using the TransferOperations FileMask like this:
Dim myTransferOptions As New TransferOptions
myTransferOptions.TransferMode = TransferMode_Binary
' include *PatChanges* and *SchChanges* but exclude *Full*
myTransferOptions.FileMask = "*PatChanges*;*SchChanges*|*Full*"  
Dim transferResult As TransferOperationResult
Set transferResult = mySession.GetFiles("/home/user/*", "H:\FTP\", False, myTransferOptions)

Open in new window

The documentation on Filemasks is a little confusing.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Hi,
Okay this solved issue #2.
Let me know if solving issue #1 is complicated, will post another question on that.
BTW, I first encountered an error, see attached.
When ran again was fine.
Thanks,
Ben
Untitled.png
Eddie ShipmanAll-around developer

Commented:
Did you get the Error code 3 fixed?
For synching directories, give this a try:

' Upload files
Dim myTransferOptions As New TransferOptions
myTransferOptions.TransferMode = TransferMode_Binary
 
Dim myTransferOptions As New TransferOptions
Dim transferResult As TransferOperationResult

myTransferOptions.TransferMode = TransferMode_Binary
' include *PatChanges* and *SchChanges* but exclude *Full*
myTransferOptions.FileMask = "*PatChanges*;*SchChanges*|*Full*"  

' When the SynchronizationMode is Local, changes from remote directory are applied to local directory. 
' When the SynchronizationMode is Remote, changes from the local directory are applied to the remote directory. 
' When the SynchronizationMode is Both, both local and remote directories can be modified.
' Parameters of the SynchronizeDirectories call:
' Synch Mode, Local Dir, Remote dir, Remove files, mirror, Synch Criteria, TransferOptions

Set transferResult = _
    mySession.SynchronizeDirectories(SynchronizationMode.Local, "H:\FTP\", "/home/user/", False, False, null, myTransferOptions)

Open in new window

Did you get the Error code 3 fixed?
No, actually i had this once already, not sure whats causing it.
However will leave that for another question.
Will try your code now.
Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
For the #3 error, try this:

myTransferOptions.ResumeSupport = TransferResumeSupportState.Off

Open in new window


Man, you've GOT to start reading the documentation ;-)
I get Method or data member not found on SynchronizationMode.Local
the same I get on TransferResumeSupportState.Off
looks like I'm missing some reference?

Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
Not sure...
SynchronizationMode mode  Synchronization mode. Possible values are 
                              SynchronizationMode.Local
                              SynchronizationMode.Remote
                              SynchronizationMode.Both

Open in new window

You do have this at the top of your script, right?
Imports WinSCP

Open in new window


What does Intellisence show as properties of SynchronizationMode?
You do have this at the top of your script, right?
Lets start here, I'm using in my MS Access VBA module.

I'm getting error on this as well
Imports WinSCP
where do I need this placed?

Thanks,
Ben
Should I upload the module as Access file?

Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
Geez, Intellisense tells me it's:
SynchronizationMode.SynchronizationMode_Local

Open in new window

and this:
myTransferOptions.ResumeSupport = TransferResumeSupportState.TransferResumeSupportState_Off

Open in new window

Did you meant like this?
Set transferResult = _
    mySession.SynchronizeDirectories(SynchronizationMode_Local, "H:\FTP\", "/Outbox/*", False, False, Null, myTransferOptions)

Open in new window

I get the attached.

Thanks,
Ben
Untitled.png
Eddie ShipmanAll-around developer

Commented:
That is not what I posted. I believe it only works for SFTP, too.
This doesn't work either
    mySession.SynchronizeDirectories(SynchronizationMode.SynchronizationMode_Local, "H:\FTP\", "/Outbox/*", False, False, Null, myTransferOptions)

Open in new window

Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
Looks like you have some reading to do. I don't use Access so I'm not sure if I can help further because I don't know how to set things up for this capability. I'm just going off the docs that I've read.
well this one is meant for Access, no?
https://winscp.net/forum/viewtopic.php?t=14489
And looks like its done right, not sure why are those errors.
Perhaps I need add some reference, besides of WinSCP?

Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
I would say to take their code, change the directories and test it.
Also, read this: https://winscp.net/eng/docs/library_vb
The only difference is they only have 4 parameters, while mine had 6.
 Set mySynchronizationResult = mySession.SynchronizeDirectories(SynchronizationMode.SynchronizationMode_Remote, sLocalPath, sRemotePath, False)

Open in new window

Where can I see what those parameters are?
Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
THE DOCUMENTATION!!
I see only 3 parameters listed...
synchronize local|remote|both [ <local directory> [ <remote directory> ] ]

Open in new window

So if I go with this, the other settings of specify file names are not avail?

Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
This is the scripting documentation for Session.SynchronizeDirectories: https://winscp.net/eng/docs/library_session_synchronizedirectories#parameters
So it takes 7 params,
looks like I need to figure out how to set the 6th param. (SynchronizationCriteria)
    mySession.SynchronizeDirectories(SynchronizationMode.SynchronizationMode_Local, "H:\FTP\", "/Outbox/*", False, False, SynchronizationCriteria_Time, myTransferOptions)

Open in new window

However now I get error type mismatch.
Also tried SynchronizationCriteria.Time and didnt work.
null and "" doesn't work either..

Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
From https://winscp.net/eng/docs/library_vb

Accessing Enumeration Values
Members of enumerations are represented as constants with name like <type>_<member>, e.g. Protocol.Sftp becomes  Protocol_Sftp.

So, it should be
SynchronizationCriteria.SynchronizationCriteria_Time

Open in new window

Also, don't you ever use Intellisense? Type out SynchronizationCriteria then hit the period and it should show you the options.
So it takes 7 params,
I pointed that out in my code:
' Parameters of the SynchronizeDirectories call:
' Synch Mode, Local Dir, Remote dir, Remove files, mirror, Synch Criteria, TransferOptions

Open in new window

Still getting type mismatch error on this
    mySession.SynchronizeDirectories(SynchronizationMode.SynchronizationMode_Local, "H:\FTP\", "/Outbox/*", False, False, SynchronizationCriteria.SynchronizationCriteria_Time, myTransferOptions)

Open in new window


don't you ever use Intellisense?
I do, however for most of those object here it wasn't avail, for example TransferOptions.

Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
I do, however for most of those object here it wasn't avail, for example TransferOptions
Because it is not an Enum.
I see, but in Access I get this for all objects used in code, as long as I have a reference to that library.

So regarding this issue, I think will close this one and open a new one for the finalization as its taking to much of your time here...-:)

Perhaps we can get the error code3 fixed there as well..

Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
Did the Error Code 3 fix I posted above work?
No, its giving me an error as posted above.

Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
I believe that that is also only for SFTP.
Thank you very much Eddie.
Really appreciate your time and efforts!!
Eddie ShipmanAll-around developer

Commented:
Hey, you really should try reading the scripting documentation, you'll answer a lot of your questions by yourself ;-)
you'll answer a lot of your questions by yourself ;-)
But I get no points for that...I should rather read documentations on problems others are posting, no-:)

Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
No but you learn and don't have to pay to ask the question. I've been a member since 2000 and get free asking privileges because I answer about 3,000-5,000 points per month.
I would rather pay those $20 a month and have others do the reading/solving then spend my work hours on this...
Believe me, if I have someone solving this question alone for $20 I would go for that...
Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
Try rent-a-coder or elance. May be a little more expensive but will get your solution without people posting snarky replies ;-)
Dont see the endorsement icon, so I marked your comment as helpful-:)

Thanks,
Ben
btw, I used to use the GIG projects option for these, but they're gone-:(
looking for replacement...
Thanks,
Ben
Eddie ShipmanAll-around developer

Commented:
maybe Craigslist? I know they have gigs there or how about r/MSAccess on Reddit?
Will try..
Thank you again Eddie.
See you at my next post-:)

Thanks,
Ben
BTW,

the following almost did that already, just got stock with something else...
https://www.experts-exchange.com/questions/29112971/Script-causing-an-error-after-running-for-long-time.html

Thanks,
Ben
Hi Eddie,

I got pass those two traps already...
However still facing permission denied error.
See following
https://www.experts-exchange.com/questions/29113836/Permission-denied-error-while-synchronizing-folders.html#questionAdd

Thanks,
Ben

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial