You can write scripts to download files from an FTP site using DOS or, better yet, components and WSH. Choose your weapon.

Automate FTP Downloads

You can write scripts to download files from an FTP site using DOS or, better yet, components and WSH. Choose your weapon.

When I was a kid watching TV, nothing frustrated me more than to see those terrible words… “To Be Continued.” Good grief! You mean I have to wait a whole week to see how it ends? You could tell when it was coming, too. The clock read 7:52 and there were way too many loose ends to tie up. I hated that. So you can imagine how much joy it gave me to leave you hanging for an entire month. I’m beginning to understand those television guys. What a sense of power! Dogbert would be impressed.

Well, as the saying goes: “Good things come to those who wait.” If you’ll recall, I promised to delve deeper into the world of components by showing you how to automate downloading files from an FTP site.

We’ll look at two ways of accomplishing this task: the old way, using the DOS FTP utility, and a new way, using the Windows Script Host (WSH) and a third-party component from Mabry Software (www.mabry.com) called FTPX. Either will accomplish the objective, but I think you’ll find the latter provides more flexibility and control.

The “Barney” Method (For Dinosaurs)

The DOS FTP utility is an interactive command-line tool that allows you to connect to a site (either in ASCII or binary mode) and perform any number of functions—uploading or downloading a file, shelling to the OS, and so on. You can write a script (.CMD or .BAT) to use this tool by creating a text file with all of the appropriate commands and using the “-s” flag, as shown below.

Put this text into an ASCII file:

open my.ftpsite.com
user admin
mypassword
cd pub
lcd c:\mystuff\myfiles
binary
get myfile.txt
disconnect
bye

Place this line in your .CMD or .BAT file:

ftp –n –s:myftp.txt

Each line from the text file is fed into the FTP utility one line at a time. Once the end of the text file is reached, control is handed back to the script.

Since this uses old-style batch scripts, there are several limitations. For starters, you’re limited in the amount of error-handling you can perform. Second, if you need to change one or more of the parameters (such as the FTP site, filename, or upload/download), you must write another complete text file with all of the necessary commands. This introduces many opportunities for mistakes, even if you copy the original text file to a new file and just change the required parameters. Other limitations include the difficulties of connecting through a firewall (one that requires logging in) or via a proxy server that may or may not require you to connect via a different port.

The 21st Century Method

Yeah, yeah… I know we don’t officially enter the 21st century until January 1, 2001. Nevertheless, it’s the year 2000 now; time put away the old ways and embrace a new paradigm: Working Efficiently! (Heck of a concept, no?) By using the WSH and a third-party FTP component, we can gain unsurpassed power and efficiency over automating this process. Let’s start by using our old friend from last month’s column, XRay.exe, to look at this component.

Figure 1 shows FTPX.DLL loaded into XRay. I’ve highlighted the CONNECT method. I’ve chosen this particular method because it demonstrates a capability we haven’t discussed yet: optional parameters. Just as there are many different ways to accomplish the same task in Windows, so it is with components. Figure 1 lists LogonName, LogonPassword, and Account as optional parameters. Since these parameters correspond to properties of this object, we can set them ahead of time if we so choose. The two scripts shown below accomplish the same thing.

‘Method #1
Dim objFTP
Set objFTP=CreateObject(“Mabry.FTPXObj”)
objFTP.Host=”my.ftpsite.com”
objFTP.LogonName=”anonymous”
[email protected]
objFTP.Connect

‘Method #2
Dim objFTP
Set objFTP=(“Mabry.FTPXObj”)
objFTP.Host=”my.ftpsite.com”
objFTP.Connect anonymous, [email protected]

It’s up to you to decide which method you prefer. (Hint: Once you’ve selected a method, you should use it more or less exclusively!) My preference is to preset as many parameters as I can. It may take a few more lines of code, but it helps me to keep things organized.

Figure 1. Use the utility called XRay.exe to view FTPX.DLL, which shows some optional parameters.

Let’s Write Some Script

Now let’s write a script to connect to an ftp site, download a file, save it to our local drive, and delete it from the source. Of course, you’ll need to have the appropriate privileges on the FTP site if you want to delete files, as I do in the following example. This script connects to an FTP site, downloads a file, saves it locally, and deletes it from the source.

‘My FTP script
‘getandkill.vbs
‘Retrieves a file and deletes it from an FTP site

‘***Section 1*** Create the variables
Dim objFTP
Dim strDest, strSource, strSourceFile

‘***Section 2*** Set the filename and path properties
strDest=”C:\MyDownloadedFiles\myfile.txt”
strSource=”pub”
strSourceFile=”yourfile.txt”

‘***Section 3*** Create my object and set its properties
Set objFTP=CreateObject(“Mabry.FTPXObj”)
objFTP.Blocking=True
objFTP.Host=”my.ftpsite.com”
objFTP.LogonName=”admin”
objFTP.LogonPassword=”strongpassword”
objFTP.SrcFilename=strSourceFile
objFTP.DstFilename=strDest
objFTP.Directory=strSource

‘***Section 4*** Connect, download, delete and quit
objFTP.Connect
objFTP.ChangeDir
objFTP.GetFile
objFTP.Delete
objFTP.Disconnect

Set objFTP=Nothing

Wscript.Quit

You’ll notice that in Section 2 I’ve “hardwired” my filenames into the script. I could just as easily have passed these parameters using the Wscript.Arguments component. This would work for the LogonName and LogonPassword as well. You can also see that I’ve set the Blocking property to “True” (Section 3). This causes the script to wait for successful completion of each command before executing the next one. That’s very important in this case, since I wouldn’t want to delete the file before I was finished downloading it, now would I? If I set this property to False, however, it allows me to accomplish other tasks without waiting for previous tasks to complete (try doing that with DOS FTP!). A “Done” event will be fired as each task is completed. (We’ll talk more about events and error handling in a future column—soon, I promise.)

Now that we’ve got the file on our local drive, we can use the Scripting Runtime FileSystemObject to copy it, rename it, move it, or whatever we need to do to it. Fortunately for us, though, this component is powerful enough to take care of much of that work for us. We could even preface the destination filename with the date or some other identifier if we routinely downloaded files of the same name. The possibilities are endless!

Next month we’ll round out our study of components by looking at how we can accomplish common administrative tasks using some very powerful objects that are built-in to Windows. In other words… “To Be Continued!”

About the Author

Chris Brooke, MCSE, is a contributing editor for Redmond magazine and director of enterprise technology for ComponentSource. He specializes in development, integration services and network/Internet administration. Send questions or your favorite scripts to [email protected].

Featured

comments powered by Disqus

Subscribe on YouTube