VBScript TFTP Client Sample Source Code
Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.
Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.
Network Component can be well integrated into any development platform that supports ActiveX objects.
This document describes how the Network Component FtpServer object can be integrated into your projects.
The most important functions of the FtpServer object are:
- Connect - connect to the (remote) FTP server on port 21 or any alternate port;
- Disconnect - to diconnect after a connect call;
- GetCurrentDir - retrieve the current directory;
- ChangeDir - change the current directory;
- CreateDir - create a new directory;
- RenameDir - rename a directory;
- DeleteDir - delete a directory;
- FindFile - find a specific file in the current directory;
- FindFirstFile - iterate over all files in the current directory; find the first file;
- FindNextFile - iterate over all files in the current directory; find the next file;
- RenameFile - rename a file in the current directory;
- DeleteFile - delete a file in the current directory;
- GetFile - get (download) a file, either using binary transfer or ASCII transfer;
- PutFile - put (upload) a file, either using binary transfer or ASCII transfer;
Step 1: Download and install the Network Component
Download Network Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Step 2: Create a new script
Create a new script using your favorite editor. You can simply use notepad. However, a VBScript editor is recommended, so you can browse through objects, objects properties and object functions.
You're now able to write a more advanced script to communicate using the Network Component.
Step 3: Create the Network Component object in VBScript
Create a new VBScript file called DEMO.VBS. It is recommended to insert the following line on top of your code:
Option Explicit
This statement requires that all variable names be defined (with the Dim statement), to avoid simple typos that can cause incredible headaches and long debugging sessions for something that should have never happened.
Now, declare the Network Component object(s):
Dim objTftpServer
Create the Network Component object(s) like this:
Set objTftpServer = CreateObject( "AxNetwork.TftpServer" )
Now, add the following lines to the file to have your first Network Component VBScript program:
WScript.Echo "Version: " & objTftpServer.Version WScript.Echo "License Status: " & objTftpServer.License Status
Appendix: Full source code
You can now connect to a remote TFTP server, logon, change directory and perform file operations.
The following VBScript code shows how to list files in a specific directory on a remote TFTP server:
' ******************************************************************** ' ActiveXperts Network Component sample - TFTP ' TFTP Sample: simple sample to show how retrieve a file from a ' remote TFTP server ' Written by ActiveXperts Software ' https://www.activexperts.com ' ******************************************************************** Option Explicit Dim objTftp, fso, strHost, strRemoteFile, strFileName, strLocalFile, strWindowsTempFolder ' Create a TftpServer instance Set objTftp = CreateObject( "AxNetwork.TftpServer" ) ' A license key is required to unlock this component after the trial period has expired. ' Call 'Activate' with a valid license key as its first parameter. Second parameter determines whether to save the license key permanently ' to the registry (True, so you need to call Activate only once), or not to store the key permanently (False, so you need to call Activate ' every time the component is created). For details, see manual, chapter "Product Activation". ' ' objTftp.Activate "XXXXX-XXXXX-XXXXX", False ' Component info Wscript.Echo "ActiveXpert Network Component " & objTftp.Version Wscript.Echo "Build: " & objTftp.Build & vbCrLf & "Module: " & objTftp.Module Wscript.Echo "License Status: " & objTftp.LicenseStatus & vbCrLf & "License Key: " & objTftp.LicenseKey & vbCrLf ' Set Logfile Set fso = CreateObject( "Scripting.FileSystemObject" ) objTftp.LogFile = fso.GetSpecialFolder(2) & "\Tftp.log" WScript.Echo "Log file: " & objTftp.LogFile & vbCrLf strHost = ReadInput( "Enter a host name or IP address", "srv202.activexperts-labs.com", False ) strRemoteFile = ReadInput( "Enter the filename on the remote TFTP host", "/network-component/readme.txt", False ) ' Retrieve filename from strRemoteFile to make a suggestion on how the file should be called on local machine. If (InStr(strRemoteFile, "/")) Then strFileName = Mid(strRemoteFile, InStrRev(strRemoteFile,"/") +1) Else strFileName = strRemoteFile End If strWindowsTempFolder = WScript.CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2) strLocalFile = ReadInput( "Enter the filename on the local machine", strWindowsTempFolder & "\" & strFileName, False ) ' objTftp.LogFile = "c:\tftp.txt" objTftp.HostPort = 69 ' Default port: 69. If you need another port, set this property objTftp.BinaryTransfer = True ' Default:binary. To use ASCII, set this property to False. objTftp.Timeout = 5000 ' Timeout in milliseconds. objTftp.Get strHost, strRemoteFile, strLocalFile Wscript.Echo "Get, result: " & objTftp.LastError & " (" & objTftp.GetErrorDescription ( objTftp.LastError ) & ")" If( objTftp.LastError = 0 ) Then WScript.Echo "Packets received: " & objTftp.PacketsReceived WScript.Echo "Bytes received: " & objTftp.BytesReceived End If WScript.Echo "Ready." ' *************************************************************************** ' Function ReadInput ' *************************************************************************** Function ReadInput( ByVal strTitle, ByVal strDefault, ByVal bAllowEmpty ) Dim strInput, strReturn Do strInput = inputbox( strTitle, "Enter value", strDefault ) If ( strInput <> "" ) Then strReturn = strInput End If Loop until strReturn <> "" Or bAllowEmpty ReadInput = strReturn End Function
You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.