Scripting Central--Contest Put the Fun Back Into Functions--January 6, 2006

1. Perspectives

  • Contest Put the Fun Back Into Functions
  • 2. Scripter's Toolkit

  • Function Determines a Script's Path On the Fly
  • 3. Scripting Scuttlebutt

  • A More Elegant Approach for Determining Leap Years
  • Perl Script Is a Real Gem
  • 4. Script Watch

  • Customize Email the Easy Ways
  • 5. Events and Resources

  • SQL Server 2005: Up & Running Roadshows Coming to Europe!
  • WEB SEMINAR: SMTP Troubleshooting In-Depth
  • WEB SEMINAR: Validate Your Disaster Recovery Data
  • WHITE PAPER: Protect Your Data--How to Ensure Data Integrity
  • EBOOK: Understanding and Leveraging SSL-TLS for Secure Communications
  • 6. Featured White Paper

  • IT Consolidation: Maximizing the Potential of Your Windows Environment
  • 7. Announcements

  • Get Full Online Access to Windows IT Pro
  • Celebrate the New Year with Windows IT Pro Magazine

  • 1. Perspectives
    by Karen Bemowski, [email protected]

    Contest Put the Fun Back Into Functions
    Over the past three months, we've been conducting the "My Favorite Function" contest. Ze'ev Ionis was the first winner with his VBScript function named WriteIt (http://www.windowsitpro.com/Windows/Article/ArticleID/48371/48371.html). You can use this function to write to a file whatever text you send it. Jim Turner was the second winner. He created the LeapYear function, which you can use within a script or HTML Application (HTA) to determine whether a given year is a leap year (http://www.windowsitpro.com/Windows/Article/ArticleID/48649/48649.html). The third and final winner is Bill Stewart. As you'll read in this issue's "Scripter's Toolkit" section, the ScriptPath function lets a script determine, on the fly, the path to a specified file.

    Ze'ev, Jim, and Bill each received $100 for their winning functions. In addition, these three winners were put into a drawing for another $100. Jim was the lucky winner of that drawing.

    The "My Favorite Function" contest not only provided some useful functions but also brought to light an important reason for holding such contests: the sharing of ideas. After reading about Jim's LeapYear function, Bo Eschricht sent me an email about a possible way to improve that function. (You can read that letter in the "Scripting Scuttlebutt" section.) Scripting is more of an art than a science because there are many ways to script a task. Through forums such as contests and letters, people can share information and possibly get ideas on how they might begin to tackle scripting a complex task or hone a script they've already written.

    Because contests facilitate the sharing of ideas (and because they're just plain fun), we'll be starting a new contest soon. Look for details in the February 3 issue of Scripting Central.

    2. Scripter's Toolkit

    Editor's note: Bill Stewart is the third winner of the "My Favorite Function" contest. His VBScript function named ScriptPath determines a script's path while the script is running.

    Function Determines a Script's Path On the Fly
    It's sometimes useful to determine a script's path while the script is running. To accomplish this, I wrote the ScriptPath function in both VBScript and JScript.

    There isn't a Windows Script Host (WSH) property that returns the currently running script's path, so the ScriptPath function includes some creative coding with the WScript object's ScriptFullName and ScriptName properties. ScriptFullName returns the full path and filename of the current script. ScriptName returns just the script's filename. To obtain only the script's path, the ScriptPath function returns the ScriptFullName property less the number of characters in the ScriptName property. The VBScript version uses the Left function and the JScript version uses the substr method for this task. The ScriptPath function leaves the trailing backslash (\) character at the end of the path. Here's the VBScript version of ScriptPath:

       Function ScriptPath()
          ScriptPath = Left(WScript.ScriptFullName, _
             Len(WScript.ScriptFullName) - Len(WScript.ScriptName))
       End Function
    

    Here's the JScript version:

       function ScriptPath()
       \{
          return WScript.ScriptFullName.substr(0,
             WScript.ScriptFullName.length - WScript.ScriptName.length);
       \}

    One use for the ScriptPath function is if you have a script that creates a log file and you want to write the log file to the same directory as the script. Another example is if you have a script component (e.g., a .wsc file) that you want to use without registering it on the computer. In VBScript and JScript, you can create a reference to a script component's object using the GetObject function and use "script:path\component.wsc" as the parameter, where "path" is the directory containing the component, and "component.wsc" is the component's filename. The drawback to this syntax is that you must specify the full path to the component file. You can work around this limitation by using the ScriptPath function if the component file is in the same directory as the script. For example, if the file MyObject.wsc is in the same directory as your script, you can use the VBScript code

       Set MyObject = GetObject("script:" & ScriptPath() & "MyObject.wsc") 

    to retrieve the object reference. The equivalent JScript code is

       myobject = GetObject("script:" + ScriptPath() + "MyObject.wsc"); 

    3. Scripting Scuttlebutt

    A More Elegant Approach for Determining Leap Years
    The LeapYear function highlighted in the "Scripter's Toolkit" section in the December 2, 2005, issue of Scripting Central (http://www.windowsitpro.com/Windows/Article/ArticleID/48649/48649.html) startled me. Although the function works, using "day numbers" is an ugly way to go about it. Perhaps a more elegant scripting solution would take into account that you can determine a leap year by dividing the year by 400 if it's a centennial year (e.g., 2000, 3000) or by 4 if it's a noncentennial year (e.g., 2006, 2007). If the year divides evenly (i.e., no remainder), it's a leap year. Otherwise, it's a nonleap year. Ned Rozell points out this approach for determining leap years in the article "Leap Year: Invented by Caesar; Perfected by a Pope" (http://www.gi.alaska.edu/ScienceForum/ASF12/1272.html).

    Perl Script Is a Real Gem
    I just downloaded the PerlDriveSpace.pl script from Dick Lewis' article "Real-World Scripting: Perl Script Creates a Disk Utilization Report" (http://www.windowsitpro.com/Article/ArticleID/5880/5880.html). I found the script very useful and easy to customize. It saved me the time to have to write it myself. Thank you--and keep up the good work.

    4. Script Watch

    Customize Email the Easy Way
    Using SQL Mail and the xp_sendmail extended stored procedure, you can send email. Companies often need to send email to a group of people, but each mail message needs to contain unique information for each person. Learn how to script that task in the Web-exclusive article "Use CURSOR to Send Customized Email" (http://www.windowsitpro.com/SQLServer/Article/ArticleID/48597/48597.html).

    5. Events and Resources

    SQL Server 2005: Up & Running Roadshows Coming to Europe!
    SQL Server experts will present real-world information about administration, development, and business intelligence to help you put SQL Server 2005 into practice and learn how to use its new capabilities. Includes one-year PASS membership and subscription to SQL Server Magazine.
    http://www.windowsitpro.com/roadshows/sqlservereurope/index.cfm?code=0104emailannc

    WEB SEMINAR: SMTP Troubleshooting In-Depth
    Identify and troubleshoot common SMTP problems and learn about each component of Exchange that touches inbound and outbound messages. Live seminar: February 14, 2006.
    http://www.windowsitpro.com/go/seminars/smtp/?partnerref=0104emailannc

    WEB SEMINAR: Validate Your Disaster Recovery Data
    Validate your disaster recovery data and learn if your backup and restore data is worth staking your career on.
    http://www.windowsitpro.com/go/seminars/disasterrecovery/?partnerref=0104emailannc

    WHITE PAPER: Protect Your Data--How to Ensure Data Integrity
    Learn ways to ensure the integrity of your application's data with minimal impact on network and system performance.
    http://www.windowsitpro.com/go/whitepapers/NSI/dataintegrity?code=0104emailannc

    EBOOK: Understanding and Leveraging SSL-TLS for Secure Communication
    Learn all you need to know about today's most popular security protocols for secure Web-based communications.
    http://www.windowsitlibrary.com/ebooks/leveragingssl/index.cfm?code=0104emailannc

    6. Featured White Paper

    IT Consolidation: Maximizing the Potential of Your Windows Environment
    Optimize your existing Windows Server infrastructure with the addition of server and storage consolidation software and techniques.
    http://www.windowsitpro.com/go/whitepapers/polyserve/serverconsolidation/?code=0104emailannc

    7. Announcements

    Get Full Online Access to Windows IT Pro
    Order a Monthly Online Pass now and get INSTANT access to all articles, tools, and helpful resources published on WindowsITPro.com, including exclusive Web content. You'll have 24/7 access to the Windows IT Pro article database (includes more than 9000 articles) and get the latest digital issue of Windows IT Pro delivered to your inbox. Order now for just $5.95 per month:
    https://store.pentontech.com/index.cfm?s=1&promocode=eu2061um

    Celebrate the New Year with Windows IT Pro Magazine
    You won't want to miss any of Windows IT Pro's upcoming 2006 issues! Subscribe now and discover the best ways to plan for Longhorn, the need-to-knows of VBScript, ways to make sense of SQL Server 2005, the 10 Security Tools You Can't Live Without, Vista launch essentials, and much more. You'll save $40 off the full cover price and gain exclusive access to the entire Windows IT Pro online article database FREE. Subscribe today:
    https://store.pentontech.com/index.cfm?s=1&promocode=eu2061uw

    Contact Us

  • About Scripting Central -- [email protected]
  • About product news -- [email protected]
  • About your subscription -- [email protected]
  • This email newsletter is brought to you by Windows IT Pro, the leading publication for IT professionals deploying Windows and related technologies. Subscribe today!
    https://store.pentontech.com/index.cfm?s=1&promocode=eu205xfb

    View the Windows IT Pro Privacy Policy.
    http://www.winnetmag.com/aboutus/index.cfm?action=privacy

    Windows IT Pro is a division of Penton Media, Inc. 221 East 29th Street, Loveland, CO 80538, Attention: Customer Service Department

    Copyright 2006, Penton Media, Inc. All Rights Reserved.

    Hide comments

    Comments

    • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

    Plain text

    • No HTML tags allowed.
    • Web page addresses and e-mail addresses turn into links automatically.
    • Lines and paragraphs break automatically.
    Publish