Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Writing Text Files 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

Writing Text Files in WSH
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-01-24

    Table of Contents:
  • Writing Text Files in WSH
  • Opening an existing text file
  • Advanced Writing Methods
  • Advanced writing concepts

  • 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


    Writing Text Files in WSH - Advanced writing concepts


    (Page 4 of 4 )

    I’m going to quickly present two concepts that you may find very useful when working with text files in your scripts.  I’m going to touch on them very briefly so please understand that these are just building blocks to help get you started.

    Quite frequently you’ll be using scripts to parse data before writing to a text file.  You’ll find that often this means your data will be contained in an array.  The following reusable code sample shows how to write text from an array into a comma-delimited text file.  You can easily change the delimiter to anything you like.  Use the vbTab constant if you want to create a tab-delimited file.

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFSO.OpenTextFile("C:\ScriptLog.txt", 2, 1)

    delimiter = ","

    arrDataPairs = array("Name1", "Value1", "Name2", "Value2")

    For i = 0 to UBound(arrDataPairs) Step 2

       Wscript.echo i

       strData = arrDataPairs(i) & delimiter & arrDataPairs(i + 1)

       objFile.WriteLine(strData)

    Next

    objFile.Close

    Here we’ve just moved through the array grabbing each name-value pair and joined them with a comma before writing them together as a single line.  We could easily have multiple values as well.  Just remember to increase your Step value accordingly to avoid processing the same value twice.  You could also use this same concept with multi-dimensional arrays.

    The final concept I’d like to show you concerns the VBScript Time and Date functions.  This time and date stamping is very common in file logging and can be accomplished much more quickly with these functions.

    Time()

    Date()

    The time and Date Functions do not require any parameters and simply return a string containing the local time and date respectively.  VBScript provides many different time and date functions if you’re interested in formatting them differently.  Here’s a quick example.

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFSO.OpenTextFile("C:\ScriptLog.txt", 2, 1)

    objFile.Write(Time() & " " & Date() & " This is a time stamped line")

    objFile.Close

    This code sample will result in an output similar to the following.

    8:07:39 PM 1/21/2007 This is a time stamped line

    I hope I’ve provided you with some useful tools that will allow you to add greater functionality to your scripts.  You’ll find that being able to read and write text files can be a very useful tool for both logging and debugging.  Good luck in your adventures.  Until next time, keep coding!


    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.

       · In part 2 of this series, we learn how to write information to text files. I'd love...
       · the link for the file does not work. you get a page not found error.
     

    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 2 hosted by Hostway
    Stay green...Green IT