FTP Upload Download component for VBScript for Windows

ActiveSocket is a Network Communication component for Windows Developers. It runs on any 32 bit and 64 bit Windows Platform, incl. Windows 7, Windows 2008, Windows 2003, Windows 2000, Windows Vista and Windows XP.
It features many IP protocols, incl.: SSH (Secure Shell), RSH (Remote Shell), HTTP(s), FTP, ICMP Ping, NTP, SNMP v1/v2c (Get,GetNext,Set), SNMP MIB translation, SNMP Trap Sender, SNMP Trap Receiver, Telnet, DNS, TCP, UDP, IP-to-Country, Wake-On-LAN and more. Samples are included many popular development platforms, incl. Visual C# .NET, Visual Basic .NET, ASP .NET, Visual Basic, Visual C/C++, ASP, Java, Javascript, PHP, Borland Delphi, Borland C++ Builder, ColdFusion.

ActiveSocket can be well integrated into VBScript environments. This document describes how the ActiveSocket FtpServer object can be integrated into VBScript 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 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 objFtpServer

Create the ActiveSocket object(s) like this:

   Set objFtpServer      = CreateObject( "ActiveXperts.FtpServer" )

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

   WScript.Echo "Version: " & objFtpServer.Version
   WScript.Echo "Expiration Date: " & objFtpServer.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:

Option Explicit

Dim objConstants, objFtpServer, objFtpFile, strSaveAs

Set objFtpServer = CreateObject ( "ActiveXperts.FtpServer" )

' Write some information to console
WScript.Echo "ActiveSocket " & objFtpServer.Version & " demo."
WScript.Echo "Expiration date: " & objFtpServer.ExpirationDate & vbCrLf

' Log all FTP operations
objFtpServer.LogFile = "C:\ftpget.txt"

' Connect to the remote FTP server
objFtpServer.Connect "ftp.activexperts-labs.com", "anonymous", "me@myself.dom" 
Wscript.Echo "Connect, result: " & objFtpServer.LastError & " (" & objFtpServer.GetErrorDescription ( objFtpServer.LastError ) & ")"
If( objFtpServer.LastError <> 0 ) Then
   Wscript.Echo "Ready."
   WScript.Quit
End If

' Show last response of FTP server
WScript.Echo "Last response: " & objFtpServer.LastResponse

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

' Change directory
objFtpServer.ChangeDir "/Samples/ASocket/Visual Basic/Demo" 
Wscript.Echo "ChangeDir, result: " & objFtpServer.LastError & " (" & objFtpServer.GetErrorDescription ( objFtpServer.LastError ) & ")"
If( objFtpServer.LastError <> 0 ) Then
   objFtpServer.Disconnect
   Wscript.Echo "Disconnected."
   Wscript.Echo "Ready."
   WScript.Quit
End If

' Iterate over all files
Set objFtpFile = objFtpServer.FindFirstFile()
Wscript.Echo "Find file, result: " & objFtpServer.LastError & " (" & objFtpServer.GetErrorDescription ( objFtpServer.LastError ) & ")"
' WScript.Echo "Last response: " & objFtpServer.LastResponse
While ( objFtpServer.LastError = 0 )

   WScript.Echo "Name: " & objFtpFile.Name 
   WScript.Echo "   IsDirectory: " & objFtpFile.IsDirectory
   WScript.Echo "   Size (bytes): " & objFtpFile.Size
   WScript.Echo "   Creation date (seconds): " & objFtpFile.DateSeconds
   WScript.Echo "   Creation date: " & objFtpFile.Date
    
   ' To save the file, call the GetFile function. 
   ' strSaveAs = "C:\temp\" & objFtpFile.Name
   ' objFtpServer.GetFile objFtpFile.Name, strSaveAs 
   ' Wscript.Echo "Save file as " & strSaveAs & ", result: " & objFtpServer.LastError & " (" & objFtpServer.GetErrorDescription ( objFtpServer.LastError ) & ")"

   ' To delete a file, call the DeleteFile function. 
   ' objFtpServer.DeleteFile objFtpFile.Name
   ' Wscript.Echo "Delete file, result: " & objFtpServer.LastError & " (" & objFtpServer.GetErrorDescription ( objFtpServer.LastError ) & ")"

   Set objFtpFile = objFtpServer.FindNextFile()
   Wscript.Echo "Find file, result: " & objFtpServer.LastError & " (" & objFtpServer.GetErrorDescription ( objFtpServer.LastError ) & ")"
Wend

objFtpServer.Disconnect
Wscript.Echo "Disconnected."
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/asocket.