Reader to Reader: Use a WSH Script to Upload Text Files

My company has a directory that holds thousands of short text messages. Each day, employees add new messages to that directory. I'm responsible for uploading the new or changed messages from the directory, which is on the company's local server, to the company's remote Web server, which is at the ISP's remote Network Operations Center (NOC).

I decided to automate this uploading task. I thought about using a GUI application such as Symantec's pcAnywhere or Windows NT Server 4.0, Terminal Server Edition (WTS). However, these applications require manual input to select and transfer files (i.e., they're interactive GUI applications), so I decided to use FTP to upload the files.

Most commercial versions of FTP (e.g., ws_ftp, cuteftp) are also interactive GUI applications, so I decided to use the built-in command-line version of FTP. However, I encountered a problem with the built-in Ftp command. Although you can use the Ftp command's mput subcommand to upload multiple files, this subcommand accepts wildcards and therefore prompts you for input for each filename. As a result, you must sit at the console and respond to every prompt.

I determined that the uploading task was a job for Windows Script Host (WSH). First, I created a staging directory to hold the new or changed messages that I needed to upload. Then, I used the At scheduler service to schedule a recurring task that runs a batch file that, in turn, uses the Xcopy command with the /m switch to copy to the staging directory only those files with the archive bit set (i.e., new or modified files).

For the staging directory, I created the script MsgUp.bat, which Listing 1, page 16, shows. This script performs three important steps:

MsgUp.bat starts an FTP session.

MsgUp.bat calls the WSH script GenFtpScript.vbs, which in turn, creates a script called MsgUp.ftp for use in the current FTP session. GenFtpScript.vbs writes code in MsgUp.ftp that prompts MsgUp.ftp to

log on to the current FTP session.

use the Ftp command's put subcommand to copy .asc files from the staging directory on the local server to the remote Web server.

use the Ftp command's dir subcommand to list the remote directory's files and subdirectories. I later use this list to confirm that the targeted files were copied.

MsgUp.bat executes MsgUp.ftp. Because MsgUp.ftp specifies each text file to copy by its filename, MsgUp.ftp doesn't require user interaction to run. When MsgUp.ftp executes, MsgUp.bat redirects the FTP session output to a log called ftp.log. I check this log to make sure the FTP session didn't encounter any problems.

To use these scripts in your environment, you need to make several modifications. In MsgUp.bat, you must modify the first two lines to point to the location of your staging directory. In addition, in the code at callout A in Listing 1, you need to change ftp.dest_site.com to the name of your remote server.

In GenFtpScript.vbs, you need to first modify the strings in the three Const statements at the top of the script. The strings must point to the location of your staging directory. Then, in the code at callout A in Listing 2, you need to customize the FTP username (i.e., ftpuser) and password (i.e.,12xy34z). In addition, in the code at callout B in Listing 2, you might need to change the file extension (i.e., ASC) to match the extensions of the files you want to copy. GenFtpScript.vbs performs case-sensitive comparisons, so make sure that the extension is the correct case. If you have filenames with mixed cases, you might want to adapt the script so that it performs a case conversion of the filenames in the files collection.