Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - 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 - Wait for a program to end


    (Page 3 of 4 )

    There are three basic ways to wait for a program to terminate in WSH. If you are launching the program from your script, the first is by far the easiest.

    Set WshShell = CreateObject("WScript.Shell")

    WshShell.Run "cleanmgr.exe",, True

    The first example launches the Disk Cleanup Wizard using the WshShell object’s Run method. The Run method provides an optional third parameter that accepts a Boolean value indicating whether the script should wait until the program terminates before continuing. Setting this value to true will cause the script to wait until the Disk Cleanup Wizard finishes before moving on.

    Do While WshShell.AppActivate("Disk Cleanup")

       WScript.Sleep 1000 ' Wait while window is open

    Loop

    The second method is a little less reliable, but can be effective when monitoring programs that run in a window. The WScript Shell object provides an AppActivate method that is used to bring a specific window into focus. The method returns true if the window can be activated and false if not. Since this can only return false if the window doesn’t exist, it provides a handy workaround for monitoring programs that run in windows.

    Do While IsRunning

       WScript.Sleep 1000

    Loop

     

    Function IsRunning

       Set objWMI = GetObject("winmgmts:.rootcimv2")

       Set colProcesses = objWMI.ExecQuery("Select * From Win32_Process " & _

           "Where Name = 'cleanmgr.exe'")

     

       If colProcesses.Count > 0 Then

           IsRunning = True

       Else

           IsRunning = False

       End If

    End Function

    The third and final method relies on WMI’s ability to monitor the actual executable process. It queries WMI for the existence of a specific process and pauses execution based upon that finding. This method is extremely reliable and works effectively, although it’s not as efficient as the first method I showed you.

    More Windows Scripting Articles
    More By Nilpo


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