Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - How to Easily Use 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

How to Easily Use FTP in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2007-09-04

    Table of Contents:
  • How to Easily Use FTP in WSH
  • Uploading and Downloading files
  • Extending the Chilkat FTP ActiveX control
  • Creating a Text File

  • 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


    How to Easily Use FTP in WSH - Uploading and Downloading files


    (Page 2 of 4 )

    It’s obviously not enough to just connect to an FTP server. You undoubtedly have some task to perform. One of the most common is a file download. Let’s take a look at how to create a simple function for performing file downloads with the Chilkat FTP component.

    In order to download a file we need to provide three pieces of information: what file to download, where to save it locally, and where to find it on the remote server. We’ll pass these values as parameters to our function.

    Our function first needs to navigate to the proper remote directory. We can do this with the ChangeRemoteDir method provided by the Chilkat component.

    Function FileDownload(rFile, lFile, rDir)

       If objFtp.IsConnected Then

          success = objFtp.ChangeRemoteDir(rDir)

           If (success <> 1) Then

              FileDownload = objFtp.LastErrorText

              Exit Function

           End If

    Finally, we can get the file that we wish to download using the GetFile method.

          success = objFtp.GetFile(rFile, lFile)

           If (success <> 1) Then

              FileDownload = objFtp.LastErrorText

              Exit Function

           End If

          FileDownload = "ok"

       End If

    End Function

    Notice that I’ve wrapped the entire function inside of an IF statement that uses the IsConnected method to make sure that the connection to the FTP server is still active. We could provide a subroutine that would reconnect if necessary.

    Also notice that I’ve continually caught the return code for each procedure I’ve performed. If you were implementing this code in a real world situation, it would be in your best interest to add some error-handling or logging.

    To perform a file upload, we just perform the opposite function. Notice how similar the two functions are.

    Function FileUpload(lFile, rFile, rdir)

       If objFtp.IsConnected Then

          success = objFtp.ChangeRemoteDir(rDir)

           If (success <> 1) Then

              FileUpload = objFtp.LastErrorText

              Exit Function

           End If

          success = objFtp.PutFile(lFile,rFile)

           If (success <> 1) Then

              FileUpload = objFtp.LastErrorText

              Exit Function

           End If

       

          FileUpload = "ok"

       End If

    End Function

    Now we can use these functions from anywhere in our script as long as we have an active FTP connection.

    result1 = FileDownload("index.old", "index.html", "/public_html/ircstats")

    result2 = FileUpload("FtpTest.vbs", "FtpTest.vbs", "/public_html/ircstats")

    More Windows Scripting Articles
    More By Nilpo


       · In a follow-up to my "Using FTP in WSH" article, we will examine a much easier FTP...
     

    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