Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Logging Events 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

Logging Events in WSH
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-02-26

    Table of Contents:
  • Logging Events in WSH
  • Providing the Right Information
  • Constructing the Subroutines
  • Incorporating Logging into Your Script

  • 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


    Logging Events in WSH - Constructing the Subroutines


    (Page 3 of 4 )

    Now is a good time to package our code up nicely into subroutines so it can be reused easily throughout our script.  The example subroutines you’ll be seeing here have some extra functionality added.  For the sake of time and space I won’t be going into detail for all of them.  You should have no problem seeing how they work.

    Const logfile = "C:log.txt"

    Dim startTime, startDate

     

    Set objFso = CreateObject("Scripting.FileSystemObject")

    Set WshNetwork = CreateObject("WScript.Network")

     

    Call startLogging

    Call stopLogging

     

    Sub startLogging

       startTime = Time()

       startDate = Date()

       writeLog(Date() & " Executing " & Wscript.ScriptFullName _

           & " on " & WshNetwork.ComputerName)

    End Sub

     

    Sub stopLogging

       writeLog(Date() & " Execution started at " & startTime & " on " _

           & startDate & " has completed" & vbCrLf & vbCrLf)

    End Sub

     

    Sub writeLog(strEvent)

       If objFso.FileExists(logfile) Then

           Set objLog = objFso.OpenTextFile(logfile, 8)

       Else

           Set objLog = objFso.CreateTextFile(logfile, True)

       End If

       objLog.WriteLine Time() & " " & strEvent

       objLog.Close

    End Sub

    Here’s what our subroutines should look like.  Our first subroutine is the startLogging subroutine.  This subroutine writes the first line of our log file.  We can call this sub whenever we are ready to start logging data.  The stopLogging sub writes the final line of our log session and then closes the file to save changes.

    I’ve also created a writeLog subroutine to actually perform the writing operation on the log file.  It accepts a single attribute in the form of a string to write to the file.  Running the script now produces the following log.

    5:12:12 PM 2/12/2007 Executing C:logging.vbs on DEVELOPE-J06OPO

    5:12:12 PM 2/12/2007 Execution started at 5:12:12 PM on 2/12/2007 has completed

    More Windows Scripting Articles
    More By Nilpo/Developer Shed Staff Writer


       · Add a whole new dimension of power and a level of professionalism to your scripts by...
     

    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