VanDyke Software

Automation Tip


This tip from one of our software developers shows you how to set up a way to drag and drop SFTP transfers to a folder on a remote machine using VCP (a command-line file transfer utility included with VShell Server® and in the ClientPack suite of command-line utilities), VBScript, and a desktop shortcut.

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

If you're like me, you need to upload files to the same folder on the remote machine many times throughout the day. If you're taking advantage of the VCP 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 could securely upload a file to a remote server by simply dragging and dropping right from Windows Explorer? You can by combining VCP, 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 Vcp.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, your 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" folder by right-clicking on the Windows Start menu, clicking on "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.

For SecureFX® users, this can also be done with Sfxcl.exe, the command-line SFTP utility. Here is a link to VBScript code that uses SFXCL:

SFXCL VBSCript code

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 DragAndDropVCP.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 the Vcp.exe command-line utility that is
' included with VShell Server and VanDyke ClientPack.
' Running this script without any arguments will present
' a dialog explaining setup and usage information.

Option Explicit

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

Dim g_szVcpExePath, g_szRemoteUserMachine, g_szRemoteDestination, g_szIdentityFile

' Modify this username@machine to reflect your username and the remote machine
g_szRemoteUserMachine = "jdev@blacktail"

' Modify this to reflect the path to the destination folder on the remote
' machine
g_szRemoteDestination = "Temp:\VCP Testing"

' Modify this private key path to match your own. For the sake of
' this script, we have created a special private key that is not
' protected by a passphrase. This setup would not be recommended
' if you are not the only one with exclusive access to your machine.
g_szIdentityFile = "C:\Temp\identity"


Sub Main()

    ' Get the path to VCP.exe from the registry.
    Dim szReg
    szReg = "HKLM\Software\VanDyke\SecureCRT\Install\Main Directory"
    g_szVcpExePath = g_shell.RegRead(szReg) & "\vcp.exe"

    If g_vArgs.Count < 1 Then
        Exit Sub
    End If

    Redim vFilenames(g_vArgs.Count)

    ' used for parameters that are not valid files/folders
    Redim vUnknown(g_vArgs.Count)

    Dim nIndex, szFileList, nValidFilesAndFoldersCount
    Dim szCurrentFileOrFolder, nUnknownCount

    nUnknownCount = 0
    nValidFilesAndFoldersCount = 0
    For nIndex = 0 To g_vArgs.Count - 1
        ' We could also probably check to make sure that we have access
        ' to the files, but we're just going to live with access denied
        ' errors.
        szCurrentFileOrFolder = g_vArgs(nIndex)
        If g_fso.FileExists(szCurrentFileOrFolder) Or _
            g_fso.FolderExists(szCurrentFileOrFolder) Then
            vFilenames(nIndex) = szCurrentFileOrFolder
            szFileList = szFileList & vFilenames(nIndex) & vbCrlf
            nValidFilesAndFoldersCount = nValidFilesAndFoldersCount + 1
            vUnknown(nUnknownCount) = szCurrentFileOrFolder
            nUnknownCount = nUnknownCount + 1
        End If

    ' 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...
    'MsgBox szFileList
    'Exit Sub

    If nUnknownCount > 0 Then
        ' We don't ever expect this to happen, but just in case...
        Dim szUnknownList
        For nIndex = 0 To nUnknownCount - 1
            szUnknownList = szUnknownList & vUnknown(nIndex) & vbCrlf
        MsgBox "These parameters were files or folders" & vbcrlf & _
               "that did not exist:" & vbCrlf & vbCrlf & szUnknownList
    End If

    ' Now upload each valid file or folder to the remote machine
    Dim szVCPCommand, szVCPArgs, nResult

    szVCPArgs = " -v -i " & chr(34) & g_szIdentityFile & chr(34)
    For nIndex = 0 To nValidFilesAndFoldersCount - 1
        If g_fso.FolderExists(vFilenames(nIndex)) Then
            ' If the item is a folder, we'll need to supply the " -r "
            ' cmd line arg to VCP
            szVCPArgs = szVCPArgs & " -r "
            ' ... it must be a file that we are uploading
            szVCPArgs = szVCPArgs & " "
        End If

        ' Now let's build up our command line. We use chr(34) (") to work
        ' around possible spaces in our VCP.exe and remote paths.
        szVCPCommand = chr(34) & g_szVcpExePath & chr(34) & _
                       szVCPArgs & " " & _
                       chr(34) & vFilenames(nIndex) & chr(34) & " " & _
                       g_szRemoteUserMachine & ":" & chr(34) & _
                       g_szRemoteDestination & chr(34)

    ' To take a sneak peek at the command line, uncomment the 2 lines
    ' below:
    'If Not Continue("Upload using the following command?" & _
    '                vbCrlf & chr(9) & szVCPCommand) Then Exit Sub

        ' Now run the actual VCP command...
        ' The number 7 below is used to launch VCP in a minimized cmd window.
        ' If you want the window to be hidden, use 0 (Zero) as the argument
        ' in place of 7.
        ' "True" instructs the scripting engine to wait until VCP is complete
        ' before continuing. If you would like to transfer all files in
        ' parallel, replace the True with False below.
        nResult = g_shell.Run(szVCPCommand, 7, False)
        If nResult <> 0 Then
            MsgBox "Failed to upload using the following command: " & _
                vbCrlf & chr(9) & szVCPCommand & vbCrlf & vbCrlf & _
                "Error code: " & nResult
        End If

        WScript.Sleep 50

        ' Reset the command and args
        szVCPArgs = ""
        szVCPCommand = ""
End Sub

Private Sub ShowUsage()
    MsgBox "UploadToRemote Usage:" & chr(13) & chr(13) & _
        "1. 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 & _
        "You may also want to create a shortcut in your" & vbcrlf & _
        """Send To"" folder allowing you to right-click" & vbcrlf & _
        "on selected files from within Explorer and" & vbcrlf & _
        "Send To -> (Machine Name)" & vbcrlf & vbcrlf & _
        "2. Use SecureCRT's Key Generation Wizard to create a" & vbcrlf & _
        "private key file that has an empty passphrase and" & vbcrlf & _
        "upload the corresponding public key to the SSH" & vbcrlf & _
        "server. This will allow you to connect and" & vbcrlf & _
        "authenticate with the remote SSH server without" & vbcrlf & _
        "having to input a passphrase or a password. Note" & vbcrlf & _
        "that you could also supply these using the " & vbcrlf & _
        "'-p <passphrase>' and '-pw <password>' command" & vbcrlf & _
        "line options and you will need to modify the szVCPArgs" & vbcrlf & _
        "variable in this script to add these options and" & vbcrlf & _
        "values." & _
        vbcrlf & vbcrlf & _
        "3. Drag and drop files or folders from Explorer onto" & vbcrlf & _
        "the Desktop shortcut for this .vbs file"
End Sub

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

'~~~~~~~~~~~~~~~~~~~~~~~~ End DragAndDropVCP.vbs ~~~~~~~~~~~~~~~~~~~~~~~~~~