user3070123
user3070123

Reputation: 159

How to change an FTP upload VBScript to SFTP

I want to change this to the SFTP server. How should I update this following script?

'FTP Upload
'Upload a file/folder to an FTP server 

Set oShell = CreateObject("Shell.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFSO
Varnow = now
vardate = Day(varnow) & "-" & Month(varnow) & "-" & Year(varnow) & ".csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFile = "D:/file.csv"
strRename = "D:/file-" & vardate
If objFSO.FileExists(strFile) Then
    objFSO.MoveFile strFile, strRename
End If

'Path to file or folder to upload
path = strRename

FTPUpload(path)
Sub FTPUpload(path)

    On Error Resume Next

    'Copy Options: 16 = Yes to All
    Const copyType = 16

    'FTP Wait Time in ms
    waitTime = 8000

    FTPUser = "ftuser"
    FTPPass = "psw"
    FTPHost = "hostname"
    FTPDir = "/Dir"

    strFTP = "ftp://" & FTPUser & ":" & FTPPass & "@" & FTPHost & FTPDir
    Set objFTP = oShell.NameSpace(strFTP)

    'Upload single file      
    If objFSO.FileExists(path) Then
        Set objFile = objFSO.getFile(path)
        strParent = objFile.ParentFolder
        Set objFolder = oShell.NameSpace(strParent)

        Set objItem = objFolder.ParseName(objFile.Name)

        objFTP.CopyHere objItem,copyType
    End If

    If Err.Number <> 0 Then
        Wscript.Echo "Error: " & Err.Description
    End If

    'Wait for upload
    WScript.Sleep waitTime
    If objFSO.FileExists(strRename) Then
        objFSO.MoveFile strRename, strFile
    End If
End Sub

So as you see I want to switch the FTP upload to SFTP upload (because the destination server have been changed to the SFTP protocol. What should be modified exactly in the this function Sub FTPUpload(path) to do so? Have you any tips to do so?

Upvotes: 2

Views: 3831

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202088

There's no support for SFTP in FileSystemObject, nor any other way in Windows.

You have to use a 3rd party SFTP command-line client or COM object.

So there's no easy way to change your existing script. You have to basically start from the scratch.


There are lot of existing questions on Stack Overflow for SFTP uploads with VBScript.

You can for example use WinSCP SFTP client. It has both command-line scripting interface and COM object.

(I'm the author of WinSCP)

Upvotes: 2

Related Questions