Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


July 23, 2001

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

RSS
Subscribe to Windows IT Pro | See More Remote Computing Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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.
  3. 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.

End of Article



Reader Comments
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.

Allen J Wilkes November 23, 2001


<p>This solved a lot of my problems! Thanks for publishing it!</p>

Jonas October 10, 2002


<P>Wow this works great! Didn't even think it was possible. Thanks!</P>

<P>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.</P>

Bengt November 24, 2003


Hey - great script. Love the way the upload script is created on the fly. Great work

Prasanna Karmarkar March 31, 2004


Great! Is it possible to upload binary files via changing the script? If yes then how to change it?

Anonymous User October 18, 2004


hmmm, in ftp why don't you set "prompt" = " Interactive mode off".

Anonymous User October 26, 2004


or you can use the ftp -i switch to turn off interactive prompting.

Anonymous User January 11, 2005 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Windows 7 Sets Sales Record

Microsoft CEO Steve Ballmer described Windows 7's first ten days of sales as "fantastic" while in Japan yesterday. ...


Task Automation Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Will Your Next Generation Server System Meet Your Infrastructure Optimization Needs?

Continuous Data Protection and Recovery for Microsoft Exchange

Related Events Managing IT Across Multiple Locations

Check out our list of Free Email Newsletters!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement