In my last article I showed you a work around that allowed you to use FTP in WSH. This time I’m going to show you a much easier, more flexible approach using a third-party ActiveX control designed to expand WSH.
Contributed by Nilpo Rating: / 6 September 04, 2007
ActiveX controls can be used to add functionality to WSH that it does not have natively. The ActiveX must be written for this purpose and must provide a COM interface. Once the ActiveX control is instantiated in your script, you will be able to access all of the methods and properties that it provides.
ActiveX controls must be installed on the system on which they are being used. Simply referencing a file is not enough. To install an ActiveX control, it must be registered, meaning that a set of registry keys must be created that reference it as a COM object. Many third parties will include installers for this.
If your ActiveX control does not have an installer, you can register it yourself from the command line. Navigate to the directory where your control is located and use the command regsvr32 activex.ocx with the name of your control.
We’ll be using Chilkat’s Free FTP ActiveX control. This ActiveX control acts as a sort of scriptable FTP client that provides access to the most common FTP functions. There are two versions—one free and one commercial. While we’ll be using the free control for this demonstration, the paid version offers some very cool functionality.
Once you’ve installed the control, you’ll be able to access it through its COM interface by connecting to the ChilkatFTP.ChilkatFTP namespace.
Set objFtp = CreateObject("ChilkatFTP.ChilkatFTP")
Next you’ll want to make a connection. To do that you’ll need to provide the hostname, username, and password. The FTP control provides properties for this.
objFtp.Hostname = "ftp.mysite.com"
objFtp.Username = USERNAME
objFtp.Password = PASSWORD
success = objFtp.Connect()
Finally, the Connect method makes the connection. Be sure to catch the return code for error handling purposes.
It’s obviously not enough to just connect to an FTP server. You undoubtedly have some task to perform. One of the most common is a file download. Let’s take a look at how to create a simple function for performing file downloads with the Chilkat FTP component.
In order to download a file we need to provide three pieces of information: what file to download, where to save it locally, and where to find it on the remote server. We’ll pass these values as parameters to our function.
Our function first needs to navigate to the proper remote directory. We can do this with the ChangeRemoteDir method provided by the Chilkat component.
Function FileDownload(rFile, lFile, rDir)
If objFtp.IsConnected Then
success = objFtp.ChangeRemoteDir(rDir)
If (success <> 1) Then
FileDownload = objFtp.LastErrorText
Exit Function
End If
Finally, we can get the file that we wish to download using the GetFile method.
success = objFtp.GetFile(rFile, lFile)
If (success <> 1) Then
FileDownload = objFtp.LastErrorText
Exit Function
End If
FileDownload = "ok"
End If
End Function
Notice that I’ve wrapped the entire function inside of an IF statement that uses the IsConnected method to make sure that the connection to the FTP server is still active. We could provide a subroutine that would reconnect if necessary.
Also notice that I’ve continually caught the return code for each procedure I’ve performed. If you were implementing this code in a real world situation, it would be in your best interest to add some error-handling or logging.
To perform a file upload, we just perform the opposite function. Notice how similar the two functions are.
Function FileUpload(lFile, rFile, rdir)
If objFtp.IsConnected Then
success = objFtp.ChangeRemoteDir(rDir)
If (success <> 1) Then
FileUpload = objFtp.LastErrorText
Exit Function
End If
success = objFtp.PutFile(lFile,rFile)
If (success <> 1) Then
FileUpload = objFtp.LastErrorText
Exit Function
End If
FileUpload = "ok"
End If
End Function
Now we can use these functions from anywhere in our script as long as we have an active FTP connection.
As you begin using the Chilkat FTP control, you may find that you wish to take your scripts much farther than simple uploads and downloads. I want to take a few minutes and examine some other coding practices that you may find useful.
One of these is checking for the existence of a file on the remote server. Perhaps you want to add some error-handling, or maybe you want to download an updated file only if it exists. In either case, checking for the existence of a file could prove useful.
Function FileExists(rDir, rFile)
objFtp.ChangeRemoteDir(rDir)
strTemp = objFtp.Listpattern
objFtp.ListPattern = rFile
If objFtp.GetSize(0) = -1 Then
FileExists = False
Else
FileExists = True
End If
objFtp.ListPattern = strTemp
End Function
This simple function makes use of the GetSize method to check whether a file exists. It returns the file’s size in bytes if found and returns –1 if not. So a simple If statement allows us to create a function that will return a Boolean value based on whether or not a file exists.
Notice that I’ve changed the ListPattern value so that it only lists the file for which we are looking. Since this is a global setting, I’ve made sure to capture its entering value and reset it afterward.
Now we’ll take a look at creating files and directories. Again, you will see a similar construct for these functions. Notice that the only real changes are the methods I’ve invoked to perform the action.
Finally, we’ll take a look at how to create a text file on the remote server using a provided text string. You could also manipulate this code to read from a TextStreamObject. What uses can you find for that?
That’s all for now. I hope you find this ActiveX control useful. I’m confident that once you begin using it, you will definitely want to add it to your arsenal. Thanks for reading. Until next time, keep coding!