Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > Internet > FTP




VBScript Code:
Option Explicit

' Upload the file "test.txt" from the current (local) directory to
' "ftp.myServer.myTld/www/testdir/test.txt"; create the directory
' "testdir" if it doesn't exist; login as "myid" with password "secret".
WScript.Echo FTPUpload( "test.txt", "/www/testdir", _
                        "ftp.myServer.myTld", _
                        "myid", "secret", True )

Function FTPUpload( locFile, targetDir, host, user, password, blnMkDir )
' This function uses the free ChilkatFTP ActiveX component
' to upload a single file.
' The remote directory can be specified, but the remote
' file name will be the same as the local file name.
' The function is based on Chilkat's own sample for the ChilkatFTP2 component
' (which is not free):
' Arguments:
' locFile    [string]   the (path and) file name of the file to be uploaded
' targetDir  [string]   the (relative) path of the remote target directory;
'                       if empty, the current remote directory will be used
' host       [string]   the remote host name (e.g. "")
' user       [string]   the login name for the remote host
' password   [string]   the password for the login account
' blnMkDir   [boolean]  if True, the remote directory will be created if it
'                       doesn't exist, otherwise the function will fail if
'                       the remote directory doesn't exist
' The ChilkatFTP ActiveX component can be downloaded from:
' Documentation can be found at:
' Written by Rob van der Woude

    ' Standard housekeeping
    Dim objFSO, objFTP, ok, strRemFile

    ' Extract the local file name and extension only from its path
    Set objFSO = CreateObject( "Scripting.FileSystemObject" )
    With objFSO
        strRemFile = .BuildPath( targetDir, .GetFileName( locFile ) )
    End With
    Set objFSO = Nothing

    ' Create a ChilkatFTP object
    Set objFTP = CreateObject( "ChilkatFTP.ChilkatFTP.1" )

    ' pass the connection properties to the object
    objFTP.Username = user
    objFTP.Password = password
    objFTP.Hostname = host

    ' Connect, abort and return error message on failure
    ok = objFTP.Connect( )
    If ( ok <> 1 ) Then
        FTPUpload = objFTP.LastErrorText
        Set objFTP = Nothing
        Exit Function
    End If

    If targetDir <> "" Then
        ' If specified, create target directory
        If blnMkDir = True Then
            objFTP.CreateRemoteDir targetDir
        End If
        ' Change directory remotely, abort and return error message on failure
        ok = objFTP.ChangeRemoteDir( targetDir )
        If ( ok <> 1 ) Then
            FTPUpload = objFTP.LastErrorText
            Set objFTP = Nothing
            Exit Function
        End If
    End If

    ' Upload the file, abort and return error message on failure
    ok = objFTP.PutFile( locFile, strRemFile )
    If ( ok <> 1 ) Then
        FTPUpload = objFTP.LastErrorText
    End If

    ' Disconnect,and release the object
    objFTP.Disconnect( )
    Set objFTP = Nothing

    ' Return result
    FTPUpload = "Upload succeeded"
End Function
Windows version: any
Network: any
Client software: ChilkatFTP ActiveX component
Script Engine: any
Summarized: Works in any Windowsversion, requires ChilkatFTP ActiveX component.
[Back to the top of this page]

page last modified: 2016-09-19