Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

ftp.vbs - vbscript script by ActiveXperts Software

ftp.vbs checks whether a file existens on an FTP server.

Use ftp.vbs directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select ftp.vbs. Configure the required parameter, or press 'Load a working sample'.

In ActiveXperts Network Monitor, Administrators can use three different scripting languages: Powershell, VBScript and SSH.


ftp.vbs script code

' ///////////////////////////////////////////////////////////////////////////////
' // ActiveXperts Network Monitor  - VBScript based checks
' // © ActiveXperts Software B.V.
' //
' // For more information about ActiveXperts Network Monitor and VBScript, please
' // visit the online ActiveXperts Network Monitor VBScript Guidelines at:
' //    http://www.activexperts.com/support/network-monitor/online/vbscript/
' // 
' ///////////////////////////////////////////////////////////////////////////////

Option Explicit
Const  retvalUnknown = 1
Dim    SYSDATA, SYSEXPLANATION  ' Used by Network Monitor, don't change the names

' ///////////////////////////////////////////////////////////////////////////////
' // To test a function outside Network Monitor (e.g. using CSCRIPT from the
' // command line), remove the comment character (') in the following 5 lines:
' Dim bResult
' bResult = CheckFtp( "ftp.activexperts-labs.com", "anonymous", "me@myself.me", "samples/network-monitor", "readme.txt" )
' WScript.Echo "Return value: [" & bResult & "]"
' WScript.Echo "SYSDATA: [" & SYSDATA & "]"
' WScript.Echo "SYSEXPLANATION: [" & SYSEXPLANATION & "]"
' ////////////////////////////////////////////////////////////////////////////////////////

Function CheckFtp( strHost, strAccount, strPassword, strDir, strFile )
' Description:
'     Login to an FTP server, change the directory and check for file existence.
'     This function uses ActiveXperts Network Component.
'     ActiveXperts Network Component is automatically licensed when ActiveXperts Network Monitor is purchased
'     For more information about ActiveXperts Network Component, see: www.activexperts.com/network-component
' Parameters:
'     1) strHost - Host name or IP address of the FTP host
'     2) strAccount - FTP account; use 'anonymous' for anonymous FTP access
'     3) strPassword - FTP password; use an e-mail address for anonymous FTP access
'     4) strDir - The directory where the file should be located
'     4) strFile - The file to check 
' Usage:
'     CheckFtp( "<Host>", "<Account>" , "<Password>", "<Directory>", "<File>"   )
' Sample:
'     CheckFtp( "ftp.activexperts-labs.com", "anonymous", "me@myself.me", "samples/network-monitor", "readme.txt" )

    Dim objFtpServer, objFtpFile

    CheckFtp            = retvalUnknown  ' Default return value
    SYSDATA             = ""             ' Not used by this function
    SYSEXPLANATION      = ""             ' Set initial value

    Set objFtpServer = CreateObject( "AxNetwork.FtpServer" )

    objFtpServer.Connect strHost, strAccount, strPassword
    If( objFtpServer.LastError <> 0 ) Then
        CheckFtp        = False
        SYSEXPLANATION  = "Login failed, error #" & objFtpServer.LastError & ": " & objFtpServer.GetErrorDescription( objFtpServer.LastError )
        Exit Function
    End If

    ' Use binary transfer for GetFile calls
    objFtpServer.BinaryTransfer = True

    ' Change directory
    objFtpServer.ChangeDir strDir
    If( objFtpServer.LastError <> 0 ) Then
        CheckFtp        = False
        SYSEXPLANATION  = "Failed to change directory, error #" & objFtpServer.LastError & ": " & objFtpServer.GetErrorDescription( objFtpServer.LastError )
        Exit Function
    End If

    ' Iterate over all files
    Set objFtpFile = objFtpServer.FindFirstFile()
    While ( objFtpServer.LastError = 0 )

       If( UCase( objFtpFile.Name ) = UCase( strFile ) ) Then
          CheckFtp        = True
          SYSEXPLANATION  = "File [" & strFile & "] found on FTP server [" & strHost & "]"
          objFtpServer.Disconnect
          Exit Function
       End If

       Set objFtpFile = objFtpServer.FindNextFile()
    WEnd

    CheckFtp        = False
    SYSEXPLANATION  = "File [" & strFile & "] not found on FTP server [" & strHost & "]"

    objFtpServer.Disconnect
	
End Function