Windows Scripting
  Home arrow Windows Scripting arrow Read Data from a Text file Using 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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

Read Data from a Text file Using WSH
By: aspfree
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 8
    2001-09-08

    Table of Contents:

    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


    A how-to code example from ASPFree.

    A how-to code example from ASPFree.In this guide, we will make use of the FTP component included in the IP*Works! .Net Edition. If you do not have this toolkit, or would like to download it for free, please visit our download site and do so. For the purposes of this document, we will assume that IP*Works! .Net Edition has already been installed on the readers computer. The .Net Edition includes sample code for both VB.Net and C#, but here we will be coding in VB.Net. If you would like code samples in a particular language, please contact support@nsoftware.com, or look through the demos available in the free fully functional trial versions of IP*Works! which is downloadable at http://www.nsoftware.com/download.asp. IP*Works! is available for many different languages and platforms. [bold]Getting Connected[/bold] Begin by placing the FTP component on the form. IP*Works! will provide you with the option of creating an IP*Works! toolbar in your VS.Net toolbox when you install. Simply select the component from there. The first thing we want to do is have the ability to logon to an FTP server, right? We?ll put a ?Connect? button, and textboxes for user input in order to get the name of the FTP server, the user name, and the password for that user. Once we have gathered this information ? we can connect to the server. Simply assign the RemoteHost, User, and Password properties and call the Logon method.

    FTP1.RemoteHost = txtServer.Text

    FTP1
    .User = txtUser.Text

    FTP1
    .Password = txtPassword.Text

    FTP1
    .Logon()

    [bold]What About Connection Errors?[/bold] If the server is not reachable ? or if the connection is refused by the server, the component will generate an exception. This error would be a standard exception, caught with traditional error catching routines (ie in VB.Net, an On Error statement). As for slow connections, or established connections that experience problems later, you can set an optional timeout value using the Timeout property if you like. The Timeout will fire an error if the method (in this case the logon method) does not return in Timeout seconds. Now that we are connected ? what are we to do? The first thing we?ll want to do obviously in an FTP client application is display a listing of files on the client and the server. In order to display the client side files ? you?d want to use some file object library. You can use the VS.Net System.IO classes for this. The remote file listing can be obtained by using the ListDirectory or ListDirectoryLong methods of the FTP component. The difference is that the ListDirectory method will only return the filenames of the remote directory. The ListDirectoryLong method returns the filename, size, modification date, etc. The results from the methods will be returned in the DirList event. You?ll need to overwrite this event in order to retrieve the results. We?ll use a listbox and combo box for both the client and server side to hold the file listings and the working directory. The RemotePath and RemoteFile properties are very important to the ListDirectory method (and any other FTP method that handles files). The directory listing will be performed on the remote path that is contained in the RemotePath property, and with the filemask contained in RemoteFile. If RemoteFile is empty, all files will be returned. After the Logon method, the RemotePath will automatically be updated with the home directory of the user, ie generally ?/home/username?. But you must be very careful to keep up with what the RemotePath is during your connection.

    FTP1.RemotePath = ?/home/username?

    FTP1.RemoteFile = ??

    FTP1.ListDirectory()

    These three simple lines will retrieve a directory listing of all files in the remote path ?/home/username?. We?ll grab this data from the DirList event and insert it into the remote file listbox called lstRemote. The DirList event will fire one time for each file on the server that matches the filemask for the directory list request.

    Private Sub Ftp1_OnDirList(ByVal sender As System.Object, ByVal e As nsoftware.IPWorks.FtpDirListEventArgs) Handles Ftp1.OnDirList

    If e.IsDir Then

        lstRemote
    .Items.Add( "<Dir> " & e.FileName)

    Else

        
    lstRemote.Items.Add(e.FileName)

    End If

    End Sub

    Here, the parameter DirEntry contains the exact directory entry response from the server, exactly as the server sent it, ie:

    -rw-r--r--   1 group   username       4557 Aug 29  2001 .Xdefaults

    FileName is a string parameter containing the name of the file itself, ie .Xdefaults. IsDir is a Boolean value. IsDir will be true if this particular DirEntry is a directory. FileSize is a long containing the size of the file on the remote server. FileTime is a string containing the last modified date of the file on the server. [bold]File Transfers: Upload and Download[/bold] Now we?ll add Upload and Download capabilities. To do this, we?ll need to set the RemotePath, RemoteFile, LocalPath, and LocalFile properties before attempting a transfer. We?ll use our two listboxes and comboboxes to determine that information. The value in our client combobox cbClient is ?C:/?. The selected item in our client listbox lstClient is ?test.txt?. So we?ll set:

    FTP1.LocalPath = cbClient.Items(cbClient.SelectedIndex)

    FTP1.LocalFile = lstClient.Items(lstClient.Selectedindex)

    This will point the FTP component to the local file we want to upload. Now we?ll name the file the same thing on the client side, and we?ll keep track of the remote path in the server combobox, cbServer, ie:

    FTP1.RemotePath = cbServer.Items(cbServer.SelectedIndex)

    FTP1.RemoteFile = lstClient.Items(lstClient.SelectedIndex)

    ?And
    now perform the upload:

    FTP1.Upload()

    [bold]Other Methods[/bold] For other miscellaneous functions of the FTP component, there are many useful methods: Append ? much like Upload, except if the remote file already exists, the file is appended to rather than overwritten. DeleteFile ? attempts to delete the remote file from the server. Logoff ? log off the server. MakeDirectory ? create the directory specified by RemoteFile on the server. RemoveDirectory ? remove the directory specified by RemoteFile on the server. This will not remove files within the directory automatically, that would have to be done using DeleteFile before removing the directory. RenameFile ? renames the file specified in RemoteFile to the value specified in the AltValue property. StoreUnique - much like Upload, except that the remote server decides the remote file name itself such that the file name is a unique one. The file name that the server writes to will be sent to the client. You can determine that filename by examining the LastReply property, which always contains the last reply from the server. [bold]Percent Transferred[/bold] OK, so now we are transferring files. How do I know the status of the file transfer? First of all, when the transfer is first begun, a StartTransfer event will fire. During the transfer, a Transfer event will fire, and after the transfer is finished, an EndTransfer event will fire. You can use the BytesTransferred property that is available in the Transfer event of the FTP component to keep track of how many bytes have been transferred so far in the current transfer. This information will allow you to create a progress bar. In order to do the math, you will of course need to know the file size. If you are uploading a file to the server, you already know the file size since its local (Use a File system object). If you are downloading a remote file from the server, you?ll need to ask the server how large the file is before initiating the download, by setting the RemoteFile property to the appropriate file and issuing a ListDirectoryLong. Then the DirList event will be fired once for the file you have specified and you can get the filesize parameter of the DirList event. [bold]Debugging[/bold] The most powerful debugging tool of the FTP component is the PITrail event. The PITrail event shows you all of the communication that is happening between the client and the server.

    Private Sub Ftp1_OnPITrail(ByVal sender As System.Object, ByVal e As nsoftware.IPWorks.FtpPITrailEventArgs) Handles Ftp1.OnPITrail

         txtPITrail
    .AppendText(e.Direction & ": " & e.Message & VbCrLf)

    End Sub

    The Direction parameter of the event tells you in what direction the message is traveling ? from the client to the server or server to the client. If the value of Direction is 0, the Message is being sent by the client. If the value of Direction is 1, the Message is being sent by the server. For the full source of this fully implemented FTP Client ? download an IP*Works! trial Edition from www.nsoftware.com/download.asp. [bold]More Information[/bold] For more information about IP*Works! or the FTP component, please visit www.nsoftware.com. There you will find access to our online Knowledge Base, access to free email technical support, and much more valuable information. Included in each edition of IP*Works! are many demo applications that show the power and flexibility of the toolkit. If you need more specific guidance, please feel free to email our technical support department via the online support form at www.nsoftware.com/support/request.asp.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Windows Scripting Articles
    More By aspfree

     

    IBM® developerWorks developerWorks - FREE Tools!


    Check out the new Jazz space on developerWorks

    <a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts.
    FREE! Go There Now!


    NEW! Achieving True Agility -- How process can change the behavior of your tools

    Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools.
    FREE! Go There Now!


    NEW! Did you say mainframe? e-kit

    Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise.
    FREE! Go There Now!


    NEW! Download IBM Rational Developer for System z

    Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects.
    FREE! Go There Now!


    NEW! Download IBM WebSphere Portal V6.1 beta code

    Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization.
    FREE! Go There Now!


    NEW! Innovate don't duplicate! Asset reuse strategies for success

    Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository.
    FREE! Go There Now!


    NEW! Rational Talks to You: Scott Ambler on being agile in a global development environment

    Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered!
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Manual Tester V7.0.1

    Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually.
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for Connectivity

    Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic.
    FREE! Go There Now!


    NEW! Webcast: WebSphere Process Server

    WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek