VanDyke Software

Automation Tip

Index

In this tip you'll learn how to use SFXCL (a command-line file transfer utility included with SecureFX® for Windows and Linux), a VBScript, and a desktop shortcut to create a convenient way to securely upload files to a remote sever by simply dragging and dropping right from Windows Explorer.

Using SFXCL and a VBScript for Drag and Drop SFTP Transfers to a Folder on a Remote Host

Note: This tip is for use with SecureFX for Windows.

If you're a SecureFX for Windows user taking advantage of the SFXCL command-line SFTP utility for secure file transfers, it's often inconvenient to bring up a command prompt and type in the command and the full path to the folder or files you want to transfer each time you need to upload.

Ever wish you had the ability to securely upload a file to a remote sever by simply dragging and dropping right from Windows Explorer?

In this tip, we show you how you can combine SFXCL, a VBScript, and a desktop shortcut to allow drag and drop SFTP transfers to a folder on a remote machine.

These are the steps to take:

  1. Create a VBScript that wraps Sfxcl.exe so that you can drag from Windows Explorer onto a desktop shortcut to perform secure uploads. The code for this script is provided below. Be sure to change the indicated variables to match the machine or session to which you are connecting, your username, public key, and the destination folder.

  2. Create a shortcut on the Desktop to the VBScript created in step 1 so that you can drag from Explorer onto this shortcut. A shortcut to this script can also be created in the "Send To" by right-clicking on the Start menu, choosing "Explore", and creating the shortcut in the SendTo folder.

Once you have successfully completed steps 1 and 2, you should be able to drag and drop both files and folders from Explorer onto the Desktop shortcut and the selected files and folders will be uploaded to the specified folder on the remote machine indicated within the script.

Here is the code to create a VBScript, DragAndDropSFXCL.vbs:


Save the following VBScript code to a .vbs file on your machine. Edit the file and modify the following variables:

g_szRemoteUserMachine: your username
g_szRemoteDestination: destination folder on remote machine
g_szIdentityFile: identity file to use when connecting

~~~~~~~~~~~~~~~~~~~~~~~~ Begin DragAndDropSFXCL.vbs ~~~~~~~~~~~~~~~~~~~~~~
' DragAndDropSFXCL.vbs
'
'
'
' This script demonstrates how to use drag and drop to transfer
' files to a pre-determined directory on a pre-determined remote
' machine using a session that has been set up using the SecureFX
' UI.
'
' This example uses a session that has a saved password, and does
' not delve into the specifics of how to use a session that
' requires a passphrase (for public key authentication), or a
' password (for a session that does not have a password already
' saved). For more information about how you can use a private/
' public key for authentication with sfxcl.exe, contact
' support@vandyke.com
'
' Running this script without any arguments will present
' a dialog explaining setup and usage information.


Option Explicit

Dim g_objArgs, g_shell, g_fso
Set g_objArgs = WScript.Arguments
Set g_shell = CreateObject("WScript.Shell")
Set g_fso = CreateObject("Scripting.FileSystemObject")

Dim g_szSFXCLExePath, g_szTargetSessionPath, g_szRemoteDestination

' Modify this variable to reflect the name of the session to be
' used for connecting to the remote machine
g_szTargetSessionPath = "redhat"

' Modify this to reflect the path to the destination folder on the
' remote machine
g_szRemoteDestination = "sfxcl-testing"

Dim g_szLogFile
g_szLogFile = "C:\Temp\sfxcl_log.txt"

' So that our log file doesn't grow too big...
if g_fso.FileExists(g_szLogFile) Then g_fso.DeleteFile(g_szLogFile)

Main

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub Main()
    Dim szReg
    szReg = "HKLM\Software\VanDyke\SecureFX\Install\Main Directory"
    g_szSFXCLExePath = g_shell.RegRead(szReg) & "\sfxcl.exe"

    If g_objArgs.Count < 1 Then
    ShowUsage
    Exit Sub
    End If

    Dim nIndex, szArg, szSourceArgs

    For Each szArg in g_objArgs
        szSourceArgs = szSourceArgs & " " & chr(34) & szArg & chr(34)
    Next

    ' For visual confirmation that drag and drop is working
    ' correctly, uncomment the following lines to display a
    ' message box with the arguments to this script and then
    ' exit...
    ' Note that a MsgBox in VBScript can only hold 256 characters
    'MsgBox "Argument Count: " & g_objArgs.Count & vbcrlf & _
    '       "------------------" & vbcrlf & szSourceArgs
    'Exit Sub

    Dim szCommandLine, szArgs, nResult

    szArgs = szSourceArgs & " " & _
            "/S " & chr(34) & g_szTargetSessionPath & chr(34) & " " & _
            g_szRemoteDestination

    szCommandLine = chr(34) & g_szSFXCLExePath & chr(34) & " " & _
            "/Log " & chr(34) & g_szLogFile & chr(34) & " " & szArgs

    ' For debugging, or if you want to be really verbose, uncomment the
    ' following line
    'If Not Continue(szCommandLine) Then Exit Sub

    ' Now run the actual command...
    ' "7" below is used to launch sfxcl in a minimized cmd window.
    ' You could actually change this to a 0 to hide the window entirely
    nResult = g_shell.Run(szCommandLine, 7, True)

    If nResult <> 0 Then
        ' Capture some of the information from the Log file to display
        ' in the Error message. This information might give some good
        ' hints as to what might have gone wrong
        Dim objLogFile, szLogText, szLastLines

        Set objLogFile = g_fso.OpenTextFile(g_szLogFile)
        szLogText = objLogFile.ReadAll

        ' Get the Last 6 lines of the log file and display them
        Dim nPos, nLine, szTimeTag
        szTimeTag = Year(Now) & "-" & NN(Month(Now)) & "-" & NN(Day(Now))
        For nLine = 0 to 6
            nPos = InstrRev(szLogText, szTimeTag)
            If nPos = 0 Then Exit For
            If nLine > 0 Then szLastLines = Mid(szLogText, nPos) & szLastLines
            szLogText = Left(szLogText, nPos - 1)
        Next

        szLastLines = "[...]" & vbcrlf & szLastLines
        MsgBox "Failed to upload using the following command: " & vbCrlf & _
               chr(9) & szCommandLine & vbCrlf & vbCrlf & _
                "Error code: " & nResult & vbcrlf & vbcrlf & _
                "Log file details: " & vbcrlf & szLastLines

    End If

End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub ShowUsage()
    MsgBox "DragAndDropSFXCL Usage:" & chr(13) & chr(13) & _
    "1. Create a session in SecureFX that contains the" & vbcrlf & _
    "connection information that will be used for the" & vbcrlf & _
    "target machine. You will probably want to use this" & vbcrlf & _
    "session to connect from within SecureFX and save your" & vbcrlf & _
    "password so that you will not have to put any password" & vbcrlf & _
    "information in this plain-text script file." & _
    vbcrlf & vbcrlf & _
    "2. Create a Desktop shortcut to this .vbs file." & vbCrlf & _
    "If the .vbs extension is not set to run wscript.exe" & vbcrlf & _
    "automatically on your system, create a shortcut with" & vbcrlf & _
    "the path set to: " & vbCrlf & _
    chr(9) & "<path to wscript.exe> <path to this .vbs file>" & _
    vbCrlf & vbCrlf & _
    "3. Drag And drop files and/or folders from Windows" & vbcrlf & _
    "Explorer onto the Desktop shortcut for this .vbs file"
End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function Continue(szMsg)
    Continue = True
    If msgBox(szMsg, vbYesno) <> vbYes Then Continue = False
End Function

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function NN(nNumber)
' Normalizes a single digit number into a double-digit number with a leading 0
    If nNumber < 10 Then
        NN = "0" & nNumber
    Else
        NN = nNumber
    End If
End Function

'~~~~~~~~~~~~~~~~~~~~~~~~~~~ End DragAndDropSFXCL.vbs ~~~~~~~~~~~~~~~~~~~~~~~

Additional information on using the SFXCL command-line utility can be found in the SecureFX Help files, the SFXCL Command-Line Tool for SecureFX FAQs, and the SFXCL Automation Guide.

 

VanDyke Software uses cookies to give you the best online experience. Before continuing to use this site, please confirm that you agree to our use of cookies. Please see our Cookie Usage for details.