How to Easily Use FTP in WSH

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
Rating: 4 stars4 stars4 stars4 stars4 stars / 6
September 04, 2007
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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.

If (success <> 1) Then

   Wscript.Echo objFtp.LastErrorText

   Wscript.Quit

End If

Uploading and Downloading files

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.

result1 = FileDownload("index.old", "index.html", "/public_html/ircstats")

result2 = FileUpload("FtpTest.vbs", "FtpTest.vbs", "/public_html/ircstats")

Extending the Chilkat FTP ActiveX control

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.

Function CreateDirectory(rDir, rName)

   If objFtp.IsConnected Then

      success = objFtp.ChangeRemoteDir(rDir)

       If (success <> 1) Then

          CreateDirectory = objFtp.LastErrorText

          Exit Function

       End If

      success = objFtp.CreateRemoteDir(rName)

       If (success <> 1) Then

          CreateDirectory = objFtp.LastErrorText

          Exit Function

       End If

       

      CreateDirectory = "ok"

   End If

End Function

Creating a Text File

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?

Function CreateTextFile(rDir, rFile, strContents)

   If objFtp.IsConnected Then

      success = objFtp.ChangeRemoteDir(rDir)

       If (success <> 1) Then

          CreateTextFile = objFtp.LastErrorText

          Exit Function

       End If

      success = objFtp.PutFileFromTextData(rFile, strContents)

       If (success <> 1) Then

          CreateTextFile = objFtp.LastErrorText

          Exit Function

       End If

 

      CreateTextFile = "ok"

   End If

End Function

This is only the beginning of what you can accomplish using the Chilkat FTP control. Stop by and take a look at the documentation for more ideas.

http://www.chilkatsoft.com/refdoc/xChilkatFtpRef.html

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!

blog comments powered by Disqus
WINDOWS SCRIPTING ARTICLES

- More Windows Scripting Workarounds from Nilpo
- Overloading Methods and More in VBScript
- Improving MFC for Windows Vista
- Regular Expressions in VBScript
- Working with Dates in WMI
- Completing Calendars with VBScript Date Func...
- Building Calendars with VBScript Date Functi...
- Working With Dates and Times in VBScript
- Designing WCF DataContract Classes Using the...
- Understanding Dates and Times in VBScript
- Working With Arrays in VBScript
- Compressed Folders in WSH
- Using .NET Interops in VBScript
- Nilpo`s Scripting Secrets, Vol I
- Database operations using Silverlight 2.0 WC...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials