Jump to content

Recommended Posts

Posted (edited)

Hi all.
Today I would like to introduce the beginning of the UDF.

How to get started:

http://winscp.net/eng/docs/library

http://winscp.net/eng/docs/library_install

Original readme_automation.txt:

  Reveal hidden contents

This is the README file for automation package of WinSCP.



For use of WinSCP automation package see
http://winscp.net/eng/docs/library
http://winscp.net/eng/docs/library_install

To use this package you must unpack it to the WinSCP installation folder
or to a folder, where you have already unpacked the WinSCP standalone package.
http://winscp.net/eng/docs/installation or
http://winscp.net/eng/docs/portable

To use the WinSCP assembly via COM interop, register it using:
%WINDIR%Microsoft.NETFrameworkRegAsm.exe WinSCPnet.dll /codebase /tlb
where is typically either v4.0.30319 or v2.0.50727.
http://winscp.net/eng/docs/library_install#registering

WinSCP homepage is http://winscp.net/

See the file 'license-dotnet.txt' for the license conditions.

 

now I have only one function (standard FTP on standard port) to show the future possibilities:

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
Const Enum _
        $__eWSCP_SO_Protocol_Sftp, _
        $__eWSCP_SO_Protocol_Scp, _
        $__eWSCP_SO_Protocol_Ftp
Const Enum _
        $__eWSCP_TO_TransferMode_Binary, _
        $__eWSCP_TO_TransferMode_Ascii, _
        $__eWSCP_TO_TransferMode_Automatic

Example_PutFile('YOUR FTP HOST NAME', 'YOUR USER NAME', 'YOUR PASSWORD')

Func Example_PutFile($sHostName, $sUserName, $sPassword)
    Local $sFileFullPath = StringReplace(@ScriptFullPath, '\', '\\')
    Local $sFilesToPut = StringReplace(@ScriptDir & '\*.au3', '\', '\\')


    ; based on:
    ; http://winscp.net/eng/docs/library_com_wsh#vbscript

    Local $oSessionOptions = ObjCreate("WinSCP.SessionOptions");
    With $oSessionOptions
        .Protocol = $__eWSCP_SO_Protocol_Ftp
        .HostName = $sHostName;
        .UserName = $sUserName;
        .Password = $sPassword;
        ; below not jet tested
        ; .SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    EndWith

    Local $oSession = ObjCreate("WinSCP.Session");
    With $oSession

        ; Connect: Open sesion with defined options
        .Open($oSessionOptions);

        ; Set TransferOptions
        Local $oTransferOptions = ObjCreate("WinSCP.TransferOptions")
        $oTransferOptions.TransferMode = $__eWSCP_TO_TransferMode_Binary
        ; Upload files: put @ScriptFullPath to the ROOT directory
        Local $oTransferResult = .PutFiles($sFilesToPut, '/');

        ; Throw on any error
        $oTransferResult.Check

        ; Print results
        For $oTransfer In $oTransferResult.Transfers
            ConsoleWrite("Upload of " & $oTransfer.FileName & " succeeded" & @CRLF)
        Next

        ;' Disconnect, clean up
        .Dispose()
    EndWith

    ; CleanUp

    $oSession = ''
    $oSessionOptions = ''

EndFunc   ;==>Example_PutFile

Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

EDIT: 2014-06-20 04:47 - script changed

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Nice start. I use WinScp a lot at work to connect to different AIX-Servers. Go ahead!

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Posted

Nice to hear that .

What is the connection method you use?
What kind of encryption?

I need someone to test - would you be so kind?

mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

I would be more than happy to test, as I will most likely this weekend play around with the .NET assembly.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

I just install Filezilla Server

https://wiki.filezilla-project.org/FTPS_using_Explicit_SSL/TLS_howto_(Server)

and starting with testing.

But any feedback would be welcome.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

I use SFTP with a SSH tunnel. First I have to connect to a jump-server and then go from there to all other servers.

Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

  • 4 years later...
Posted (edited)
Local $Winscpun = 'Me.xprt.sftp'
Local $Winscpconn = 'transfer.server2.com'
Local $Winscpssh = 'ssh-rsa 4096 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00'
Local $Winscpmask = '/Exports/Data*.*'
Local $Winscplocal = 'S:\Server\User\'
Local $Commongrpname = 'Phrase'

        
        Global $filesdownloaded = False
        Local $sDecrypted = StringEncrypt(False, RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Key", "SFTP"), $Commongrpname)
        Const Enum $__eWSCP_SO_Protocol_Sftp, $__eWSCP_SO_Protocol_Scp, $__eWSCP_SO_Protocol_Ftp
        Const Enum $__eWSCP_TO_TransferMode_Binary, $__eWSCP_TO_TransferMode_Ascii, $__eWSCP_TO_TransferMode_Automatic
        Loged("Get Files")
        Local $oSessionOptions = ObjCreate("WinSCP.SessionOptions");
        $oSessionOptions.Protocol = $__eWSCP_SO_Protocol_Sftp
        $oSessionOptions.HostName = $Winscpconn;
        $oSessionOptions.UserName = $Winscpun;
        $oSessionOptions.Password = $sDecrypted;
        $oSessionOptions.SshHostKeyFingerprint = $Winscpssh
        Local $oSession = ObjCreate("WinSCP.Session");
        $oSession.Open($oSessionOptions);
        Local $oTransferOptions = ObjCreate("WinSCP.TransferOptions")
        $oTransferOptions.TransferMode = $__eWSCP_TO_TransferMode_Binary
        $oTransferOptions.PreserveTimestamp = True
        $oTransferOptions.Filemask = $Winscpmask
        Local $oTransferResult = $oSession.GetFiles('*', $Winscplocal, False, $oTransferOptions)
        $oTransferResult.Check
        Sleep(1000)
        Local $tmpFilename
        For $oTransfer In $oTransferResult.Transfers
            ;Rename remote files
            $filesdownloaded = True
            Loged("Rename remote files")
            $dt = StringReplace(_NowDate(),"/",".")
            $dt = StringReplace($dt," ","")
            $orgfn = $oTransfer.FileName
            $tmpFilename = $oTransfer.FileName
            ;msg(StringReplace($tmpFilename, "/Exports/", "/Exports/Downloaded_" & $dt & "_" ))
            Local $oTransferResult2 =  $oSession.MoveFile($orgfn,StringReplace($tmpFilename, "/Exports/", "/Exports/Downloaded_" & $dt & "_" ))
        Next
        $oSession.Dispose()
        $oSession = ''
        $oSessionOptions = ''

I used alot of what you posted to figure out how to download alot of similar files and then rename them after download. Take the following with a grain of salt, hope it helps someone...

I encrypted my password in the registry so it's not in plain site, I also have it renaming the remote files so I don't download them again

 


 

Edited by dwalker0229
made into code block(first time)
  • 5 weeks later...
Posted

First, let me say thanks to all who have put time into creating this UDF solution.

Okay, now for my problem... 

I keep getting:
Local $oTransferResult2 =  $oSession.MoveFile($origFN,StringReplace($newFN, $remoteDir, $remoteDir & '/~Loaded'))
Local $oTransferResult2 =  $oSession^ ERROR

I have tried everything I can think of, and still get this error every time.  I include WinSCP.au3 in my script.   my original WinSCP.au3 has this:
Func _sftpMove($oSession, $source, $target)
    $oSession.MoveFile($source, $target)
    Return $oSession.FileExists($target)
EndFunc

When I call it from my other script with _sftpMove($sftpSession,$source,$target), I get the same error.

Can someone please stop me from pulling anymore hair out??

 

Thanks

 

 

Posted

Post your script, and copy/paste the output pane of SciTE so we can see the entire error message.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

  Reveal hidden contents

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted (edited)

Section of script calling WinSCP.au3:

Func _syncImportFiles()
;~     $sftpAccts: customer_id, hostkey,sftp_host, sftp_port, sftp_username, sftp_password,sftp_download_path,contact_email

    _alert('Beginning sync of import files for customer_id: ' & $customer_id)

    ; Create local directory
    $localDir         = @ScriptDir & '\member_lists\' & $sftpAccts[$row][0]
    DirCreate($localDir)


    ; Sync patient import files sftp with local directory
    $sftpHost        = $sftpAccts[$row][2]
    $sftpUser        = $sftpAccts[$row][4]
    $sftpPassword    = $sftpAccts[$row][5]
    $hostKey        = $sftpAccts[$row][1]
    $sftpPort        = $sftpAccts[$row][3]
    Global $remoteDir        = $sftpAccts[$row][6]
    Global $sftpSession = _sftpOpen($sftpHost, $sftpUser, $sftpPassword, $hostKey, $sftpPort)

    If $sftpSession = False Then
        If $sftpSession Then $sftpSession.Dispose
        _alert('Failed to sync import files', $LOG, $EMAIL)
        Return SetError(-1, 0, False)
    EndIf
    
    If @error Then
        _alert('Failed to sync files.  Error Code: ' & @error, $LOG, $EMAIL)
        Return SetError(-2, 0, False)
    ElseIf UBound($syncedFiles)>0 Then
        _alert('Synced files:' & @CRLF & _ArrayToString($syncedFiles))
        Return True
    Else
        _alert('Synced files: NONE')
        Return True
    EndIf


;~ _ArrayDisplay($syncedFiles)

EndFunc ; _syncImportFiles()
 

Portion of WinSCP.au3 that is running:
Func _sftpSyncMove($session, $localDir, $remoteDir, $direction='Both', $fileMask=Null)

    Switch $direction
        Case 'Local'
            $syncMode = 0
        Case 'Remote'
            $syncMode = 1
        Case 'Both'
            $syncMode = 2
    EndSwitch

    If $fileMask <> Null Then
        $oTransferOptions = ObjCreate("WinSCP.TransferOptions")
        If Not IsObj($oTransferOptions) Then
            SetError(-1)
            Return False
        EndIf
;~         $oTransferOptions.FileMask = $session.EscapeFileMask($fileMask)
        $oTransferOptions.FileMask = $fileMask
    Else
        $oTransferOptions = Null
    EndIf

    $syncResult = $session.SynchronizeDirectories($syncMode, $localDir, $session.EscapeFileMask($remoteDir), False, False, 1, $oTransferOptions)
    If Not IsObj($syncResult) Then
        SetError(-11)
        Return False
    EndIf

    If Not $syncResult.IsSuccess Then
        SetError(-5)
        Return False
    EndIf

    Dim $itemArray[0][3]
    For $transferItem in $syncResult.Downloads
        _ArrayAdd($itemArray, 'Download' & '|' & $transferItem.Destination  & '|' & $transferItem.FileName)
        ;MsgBox(0,"Files",$transferItem.FileName)
        $dt = StringReplace(_NowDate(),"/",".")
        $dt = StringReplace($dt," ","")
        $origFN = $transferItem.FileName
        $newFN = $transferItem.FileName & '_' & $dt

        ;msgBox(0,"",(StringReplace($newFN, $remoteDir, $remoteDir & '/~Loaded')))
        Local $oTransferResult2 =  $sftpSession.MoveFile($origFN,StringReplace($newFN, $remoteDir, $remoteDir & '/~Loaded'))
    Next
    For $transferItem in $syncResult.Uploads
        _ArrayAdd($itemArray, 'Upload' & '|' & $transferItem.Destination  & '|' & $transferItem.FileName)
    Next
    $oSession = ''
    Return $itemArray

EndFunc

 

Output:
--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
2019/04/05 20:00:53 : Script initializing.
2019/04/05 20:00:53 : SFTP Accounts loaded
2019/04/05 20:00:53 : Beginning sync of import files for customer_id: 864
"C:\SOME\PATH\WinSCP2.au3" (217) : ==> The requested action with this object has failed.:
Local $oTransferResult2 =  $sftpSession.MoveFile($origFN,StringReplace($newFN, $remoteDir, $remoteDir & '/~Loaded'))
Local $oTransferResult2 =  $sftpSession^ ERROR
2019/04/05 20:01:31 : Script exiting
->20:01:31 AutoIt3.exe ended.rc:1
+>20:01:31 AutoIt3Wrapper Finished.
>Exit code: 1    Time: 39.95

Edited by jcobb
removed true path
  • 4 years later...
Posted (edited)

Hi to all!

I'm not an expert in COM programming, but I'm always trying to implement it more an more into my coding.

I can't organize file copying files with progress. I will be grateful for any examples how to use FileTransferProgress and FileTransferProgressEventArgs to show the progress. 

 

Edited by RAMzor

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...