Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Nilpo`s Scripting Secrets, Vol I
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

Nilpo`s Scripting Secrets, Vol I
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-12-15

    Table of Contents:
  • Nilpo`s Scripting Secrets, Vol I
  • Determine if a user is an Administrator
  • Wait for a program to end
  • More scripting secrets

  • 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


    Nilpo`s Scripting Secrets, Vol I - More scripting secrets


    (Page 4 of 4 )

    Reading the last line of a text file

    I recently saw a very long discussion in which a half a dozen or more people were suggesting different ways of reading only the last line of a text file. Some suggested splitting the file into an array at lone breaks, others suggested parsing by other means. My point is that this was a classic example of over thinking a problem.

    Const ForReading = 1

     

    Set objFso = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFso.OpenTextFile("C:file.txt", ForReading)

     

    Do Until objFile.AtEndOfStream

       strLine = objFile.ReadLine

    Loop

    First off, parsing a text file in VBScript requires reading the entire file into memory. That is far too inefficient right from the get-go. All you need to do is open the text file with the FileSystemObject and create a loop that reads the file line by line. Because the value of strLine is reassigned each time the loop iterates, it only ever contains one line. When the loop exits, strLine contains the last line that was read, which incidentally happens to be the last line of the file.

    Playing system sounds

    I was recently asked on my web site if there was any way to play system sounds in a script. As it turns out, there sure is.

    PlaySound

     

    Sub PlaySound

       strCommand = "sndrec32 /play /close " & "%WINDIR%Mediatada.wav"

       CreateObject("WScript.Shell").Run strCommand, 0, False

    End Sub

    Windows system sounds are .wav files that can be found in the Media folder in the Windows directory. They can be played through sndrec32.exe from the command line without any extra windows.

    Well, that wraps up volume one of Nilpo’s Scripting Secrets. Stick around for the next installment, when I will reveal even more useful scripting techniques. 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.

       · Thank you very much for those valuable tips.Reading your article gave me an...
       · Hi Patrick,You make a very good point, but there's one small mistake in your...
       · Sorry, you misread my comment. Reading every line but the last line was precisely...
       · My apologies. In that case your example is spot on. Thanks for sharing with our...
     

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek