ActiveSocket Toolkit Add network capabilities to any Windows or .NET application

Quicklinks


VBScript SFTP Client Sample Source Code

ActiveSocket provides an easy-to-use development interface to a variety of IP protocols. By using ActiveSocket, you can very easily create or enhance applications with network features.

ActiveSocket 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.

ActiveSocket can be well integrated into ASP environments. This document describes how the ActiveSocket SFtp object can be integrated into VBScript projects.

The most important functions of the SFtp object are:

  • Connect - connect to the (remote) FTP server on port 22 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;
  • PutFile - put (upload) a file;

Step 1: Download and install the ActiveSocket Toolkit

Download ActiveSocket 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 ActiveSocket Toolkit.

Step 3: Create the ActiveSocket 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 ActiveSocket object(s):

Dim objSFtp

Create the ActiveSocket object(s) like this:

Set objSFtp      = CreateObject( "ActiveXperts.SFtp" )

Now, add the following lines to the file to have your fist ActiveSocket VBScript program:

WScript.Echo "Version: " & objSFtp.Version
WScript.Echo "Expiration Date: " & objSFtp.Expiration Date

Step 4: Connect to a remote FTP server, change directory and list all files

You can now connect to a remote FTP server, logon, change directory and perform file operations.

The following VBScript code shows how to list files in a specific directory on a remote FTP server:


' Helper function to expand a string variable to a specified length
Function Pad(ByRef Text, ByVal Length)
  Pad = Left(Text & Space(Length), Length)
End Function

' Helper function to test and find the error description and exit
Function test_lasterror()
  If objSFtp.LastError <> 0 Then
    WScript.Echo "Error "  & objSFtp.LastError
    If objSFtp.ProtocolError <> "" Then 
      WScript.Echo "Protocol error: " & objSFtp.ProtocolError
      WScript.Quit 1
    End If     
  End If
End Function

' Connect to the host
Set objSFtp           = CreateObject("ActiveXperts.SFtp")   ' Create an SFtp instance
objSFtp.Host          = "192.168.1.10"                      ' Hostname or IP address of remote UNIX/LINUX machine
objSFtp.UserName      = "demo"                              ' Login as 'demo'
objSFtp.Password      = "topsecret"                         ' Use a password to login

WScript.Echo "Connect"
objSFtp.Connect                                             ' Connect to the SFTP server
test_lasterror

' Change directory
objSFtp.ChangeDir "test"
test_lasterror

' List directory contents
Set f = objSFtp.FindFirstFile(".")
test_lasterror
While objSFtp.LastError = 0
  str = pad(f.name, 30) & pad(f.SizeKb & "KB", 10) & f.Date
  WScript.Echo str
  Set f = objSFtp.FindNextFile    
WEnd

' Get smalltoken file
objSFtp.GetFile "Smalltoken.file", "."
test_lasterror

objSFtp.Disconnect

' YES, command has completed
WScript.Echo "Ready."

There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/network-component.