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

Downloads
21626.zip

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:

  1. MsgUp.bat starts an FTP session.
  2. 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.
  • use the Ftp command's bye subcommand to log off of the FTP session.
Listing 2, page 16, shows the section of GenFtpScript.vbs that generates this MsgUp.ftp code.
  • 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.

  • Discuss this Article 13

    Anonymous User (not verified)
    on Jan 11, 2005
    or you can use the ftp -i switch to turn off interactive prompting.
    Anonymous User (not verified)
    on Oct 18, 2004
    Great! Is it possible to upload binary files via changing the script? If yes then how to change it?
    Anonymous User (not verified)
    on Oct 26, 2004
    hmmm, in ftp why don't you set "prompt" = " Interactive mode off".
    Bengt (not verified)
    on Nov 24, 2003

    Wow this works great! Didn't even think it was possible. Thanks!

    Now, being a non-techie, I just have to figure out how to change upload dir so the uploaded files aren't placed in the ROOT dir.

    Jonas (not verified)
    on Oct 10, 2002

    This solved a lot of my problems! Thanks for publishing it!

    Allen J Wilkes (not verified)
    on Nov 23, 2001
    I thought you'd might be interested to know that when using 'mput' with 'ftp', you can type 'prompt' command before the 'mput' command to turn the interactive questions off.
    Prasanna Karmarkar (not verified)
    on Mar 31, 2004
    Hey - great script. Love the way the upload script is created on the fly. Great work

    Please or Register to post comments.

    Upcoming Training

    Mastering System Center 2012

    During over 6 hours of training you can join John Savill from your computer as he will walk you through the key components and capabilities of System Center 2012, what’s involved in using the components, and the benefit they can bring to your environment.

    Register Now

    Current Issue

    May 2013 - The NameTranslate object is useful when you need to translate Active Directory object names between different formats, but it's awkward to use from PowerShell. Here's a PowerShell script that eliminates the awkwardness.

    CURRENT ISSUE / ARCHIVE / SUBSCRIBE

    Windows Forums

    Get answers to questions, share tips, and engage with the Windows Community in our Forums.