Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Using FTP in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Using FTP in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-08-13

    Table of Contents:
  • Using FTP in WSH
  • Creating the Unattended FTP script
  • Putting the WSH script together
  • Security considerations when using unattended ftp scripts

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using FTP in WSH - Putting the WSH script together


    (Page 3 of 4 )

    At this point you can probably see where I’m going with this. We’ll be creating a WSH script that creates an unattended ftp script on the fly and then executes the ftp command. While the example in this article takes a very direct approach, try to keep in mind that this can be a very dynamic process if need be.

    Don’t feel like you’re stuck creating simple line-by-line script solutions. You can create an upload script to synchronize a dynamic directory structure, for example. All you need to do is analyze the structure, gather the folder and file names, and use those to create your unattended script file. You’ll see that this is much easier than it sounds.

    Let’s take a look at the code. To begin, we’re going to establish our server details and instantiate both the WshShell object and the FileSystemObject. We’ll also create a temporary text file that will be used as an unattended ftp script.

    Const HOSTNAME = "ftp.myserver.com"

    Const USERNAME = "Login"

    Const PASSWORD = "password"

     

    Set WshShell = CreateObject("WScript.Shell")

    Set objFso = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFso.CreateTextFile("session.txt")

    Next, we’ll add the necessary ftp commands by writing them to the ftp script file.

    With objFile

    .WriteLine "USER username"

    .WriteLine "password"

    .WriteLine "cd /public_html/pub/examples"

    .WriteLine "get word.vbs"

    .Close

    End With

    Finally, we’ll execute the ftp command using the WshShell object’s Exec method.

    strFTP = "%systemroot%System32ftp.exe -s:session.txt"

    strFTP = WshShell.ExpandEnvironmentStrings(strFTP)

    WshShell.Run strFTP

    For the sake of compatibility, I’ve used an environmental variable in my path. Be aware that you cannot do this if you intend to run this script as a Scheduled Task. Scheduled Tasks do not support the use of environmental variables in WSH scripts and will require you to hard code the path.

    objFso.DeleteFile "session.txt", vbTrue

    All that’s left now is a little cleanup to remove the temporary text file that we’ve created. Now let’s take a look at some potential security vulnerabilities that we should take into consideration.

    More Windows Scripting Articles
    More By Nilpo


       · Hey, guys. I'm making my return to Windows Scripting with this workaround for a...
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT