Fileaze banner

 

VBScript Scripting Techniques > Internet > FTP

FTP

 

 

ChilkatFTP
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): http://www.example-code.com/vbscript/ftpPutFile.asp
'
' 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. "ftp.mydomain.org")
' 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:
' http://www.chilkatsoft.com/download/FtpActiveX.msi
' Documentation can be found at:
' http://www.chilkatsoft.com/refdoc/xChilkatFtpRef.html
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

    ' 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 targeDir <> "" 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
            objFTP.Disconnect()
            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
Requirements:
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]
 
 
Primalscript.FTPTransfer (Sapien FTP component)
VBScript Code:
Function FTPGet( myServer, myUserID, myPassword, myPort, blnPassive, myRemotePath, myLocalPath )
' Download a file using SAPIEN Technologies' FTP component (http://www.primalscript.com/Free_Tools/)
' Based on a sample script by Alexander Riedel, SAPIEN Technologies, Inc.
'
' Input:
' myServer      [string]   remote FTP server
' myUserID      [string]   user ID for access to FTP server
' myPassword    [string]   password for access to FTP server
' myPort        [integer]  remote FTP port number
' blnPassive    [boolean]  True if passive, otherwise False
' myRemotePath  [string]   full path of remote file, starting from root
' myLocalPath   [string]   fully qualified local path
'
' Returns:
' Status message [string]  if transfer didn't succeed, message starts with "ERROR:"
    Dim objFTP

    On Error Resume Next
    Set objFTP = CreateObject( "Primalscript.FTPTransfer" )
    If Err Then
        FTPGet = "ERROR: Sapien FTP Object not available on this computer"
        Exit Function
    End If

    If blnPassive = True Then
        objFTP.Passive = 1
    Else
        objFTP.Passive = 0
    End If
    If myPort <> "" Then
        objFTP.Port = myPort
    End If

    If objFTP.Connect( "ftp.microsoft.com", "", "" ) = 0 Then
        FTPGet = "ERROR: " & objFTP.Status
    Else
        WScript.Echo "Starting download"
        If objFTP.Get( myRelPath, myLocalPath ) = 0 Then
            FTPGet = "ERROR: " & objFTP.Status
        Else
            FTPGet = "Download complete"
        End If
        objFTP.Disconnect
    End If
    Set objFTP = Nothing End Function
Requirements:
Windows version: any
Network: any
Client software: Primalscript.FTPTransfer (Sapien FTP component)
Script Engine: any
Summarized: Works in any Windowsversion, requires Primalscript.FTPTransfer (Sapien FTP component).
 
[Back to the top of this page]

 

 


page last uploaded: 4 March 2011, 12:56
Fileaze