Featured

QTP 10 not Recognizing Oracle Forms Obje

Bidun Meduza shared on our Facebook page a problem and solution that I thought might be useful to you too. Problem He had a situation where QTP 10 was unable to recognize objects of Oracle Forms. Th

Read More
QTP 10 not Recognizing Oracle Forms Objects

New Navigation Menus

In order to ease orientation through our vast knowledge archive, we have added the following navigation menus: First Steps Scripting QTP VBScript Tutorial Knowledge Articles (by levels of expertise) B

Read More
New Navigation Menus

Permalinks

We’ve changed the permalinks format, so if you have been experiencing some trouble with finding materials with the old links, then remove the YYYY/MM/ from the URL and you’d be fine. In ca

Read More
Permalinks

Advanced QTP on Android

A first version of AdvancedQTP for Android can be found here. Now you can take this perfect companion with you to keep updated on what’s new in our site.

Read More
Advanced QTP on Android

New Advanced QTP Features: Layout and FB

As you have most surely noticed, we’ve recently changed the site’s theme in order to enhance the overall user experience. As from today, you can also register and login seamlessly with you

Read More
New Advanced QTP Features: Layout and FB Integration

Pad Number String with Zeroes

0
by on May 3, 2008 at 11:00
Sometimes a numbered list is required, such as a list to populate a ListView, TreeView or one to create sequential files in a folder. One of the main problems is that such lists typically sort the items lexicographically, so that numbered items end-up in the wrong order, such as File_1, File_10, FIle_11, …, File_2, FIle_20, etc.
The following function transforms the required number to a string with padding zeroes on the left. This is accomplished by taking the difference between the string lengths of the maximum value in the list and the given number.
<span style="color: #008000;">\'*******************************************************************************</span>
<span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Function</span> PadNumber(<span style="color: #0000ff;">ByVal</span> intCurrentNum, <span style="color: #0000ff;">ByVal</span> intMaxNumInList)
<span style="color: #008000;">\'*******************************************************************************</span>
<span style="color: #008000;">\'Description : Pads a number with zeroes on the left, according to the expected</span>
<span style="color: #008000;">\' maximum length of the numbers in the list.</span>
<span style="color: #008000;">\'</span>
<span style="color: #008000;">\'Purpose : To keep a number list sorted properly, as with a file list (001,</span>
<span style="color: #008000;">\' 002,..., 010, and not 1, 10, 11,..., 2, 20).</span>
<span style="color: #008000;">\'</span>
<span style="color: #008000;">\'Arguments : intCurrentNum (the current number to be padded)</span>
<span style="color: #008000;">\' intMaxNumInList (the top number in the list)</span>
<span style="color: #008000;">\' Note: The arguments are always taken in absolute values</span>
<span style="color: #008000;">\'Returns : The padded intCurrentNum (for example, If 1 and 9999 are sent to</span>
<span style="color: #008000;">\' the function, the result will be 0001)</span>
<span style="color: #008000;">\'</span>
<span style="color: #008000;">\'Developed By : Meir Bar-Tal</span>
<span style="color: #008000;">\'</span>
<span style="color: #008000;">\'Date : 11-Jan-2007</span>
<span style="color: #008000;">\'</span>
<span style="color: #008000;">\'*******************************************************************************</span>
    <span style="color: #008000;">\'Validates the arguments - if invalid then it returns the value as is</span>
    <span style="color: #0000ff;">If</span> (<span style="color: #0000ff;">Not</span> IsNumeric(intCurrentNum) <span style="color: #0000ff;">Or</span> <span style="color: #0000ff;">Not</span> IsNumeric(intMaxNumInList)) <span style="color: #0000ff;">Then</span>
        PadNumber = intCurrentNum
        <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Function</span>
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>

    <span style="color: #0000ff;">If</span> (Abs(intCurrentNum) >= Abs(intMaxNumInList)) <span style="color: #0000ff;">Then</span>
        PadNumber = intCurrentNum
        <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Function</span>
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>

    PadNumber = <span style="color: #0000ff;">String</span>(len(<span style="color: #0000ff;">CStr</span>(Abs(intMaxNumInList)))-len(<span style="color: #0000ff;">CStr</span>(Abs(intCurrentNum))), <span style="color: #006080;">"0"</span>) _
                    & <span style="color: #0000ff;">CStr</span>(Abs(intCurrentNum))

<span style="color: #008000;">\'*******************************************************************************</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Function</span>
<span style="color: #008000;">\'*******************************************************************************</span>

 

Usage examples:

Msgbox PadNumber(4, 34567)    <span style="color: #008000;">\'Returns 00004</span>
Msgbox PadNumber(-4, 34567)   <span style="color: #008000;">\'Returns 00004 </span>
Msgbox PadNumber(4, -34567)   <span style="color: #008000;">\'Returns 00004</span>
Msgbox PadNumber(34567, 4)    <span style="color: #008000;">\'Returns 34567</span>
Msgbox PadNumber(4, 9)        <span style="color: #008000;">\'Returns 4</span>
Msgbox PadNumber(<span style="color: #006080;">"Hello"</span>, 9999) <span style="color: #008000;">\'Returns Hello</span>

 

You’re invited to customize this function according to your needs.

, , , , , , , ,

Expanding .Net DevExpress Trees

0
by on April 28, 2008 at 05:51

The following solution by Yanir Goren will allow you to immediately expand any node in a DevExpress .Net tree.It’s a wonderful implementation of .Net custom controls analysis and manipulation, and involves a cool little hack involving node editing:

<span style="color: #606060;"> 1:</span> <span style="color: #008000;">\'Initialization</span>
<span style="color: #606060;"> 2:</span> <span style="color: #0000ff;">Set</span> oTree = SwfWindow(<span style="color: #006080;">"X"</span>).SwfObject(<span style="color: #006080;">"Tree"</span>) <span style="color: #008000;">\'Change this to point to your application\'s tree object</span>
<span style="color: #606060;"> 3:</span> sPath = <span style="color: #006080;">"Root\Node\[...]\SubNode"</span>   <span style="color: #008000;">\'Change this to your required node</span>
<span style="color: #606060;"> 4:</span> oTree.<span style="color: #0000ff;">Object</span>.CollapseAll <span style="color: #008000;">\'Start from scratch</span>
<span style="color: #606060;"> 5:</span>
<span style="color: #606060;"> 6:</span> <span style="color: #008000;">\'Start Navigation</span>
<span style="color: #606060;"> 7:</span> <span style="color: #0000ff;">Call</span> oTree.<span style="color: #0000ff;">Object</span>.SetFocusOnNode(sPath) <span style="color: #008000;">\'Select and expand the required node</span>
<span style="color: #606060;"> 8:</span>
<span style="color: #606060;"> 9:</span> <span style="color: #008000;">\'Get the expanded node\'s coordinates in order to click it</span>
<span style="color: #606060;"> 10:</span> <span style="color: #008000;">\'This requires us to perform a little hack</span>
<span style="color: #606060;"> 11:</span> oTree.<span style="color: #0000ff;">Object</span>.OptionsBehavior.Editable = <span style="color: #0000ff;">true</span>
<span style="color: #606060;"> 12:</span> oTree.<span style="color: #0000ff;">object</span>.ShowEditor
<span style="color: #606060;"> 13:</span>
<span style="color: #606060;"> 14:</span> <span style="color: #008000;">\'Capture the XY location into a variable</span>
<span style="color: #606060;"> 15:</span> iX= oTree.<span style="color: #0000ff;">object</span>.ActiveEditor.Location.X
<span style="color: #606060;"> 16:</span> iY = oTree.<span style="color: #0000ff;">object</span>.ActiveEditor.Location.Y
<span style="color: #606060;"> 17:</span>
<span style="color: #606060;"> 18:</span> <span style="color: #008000;">\'Reverse the hack - restore the tree to its normal state.</span>
<span style="color: #606060;"> 19:</span> oTree.<span style="color: #0000ff;">Object</span>.OptionsBehavior.Editable = <span style="color: #0000ff;">false</span>
<span style="color: #606060;"> 20:</span> oTree.<span style="color: #0000ff;">object</span>.HideEditor
<span style="color: #606060;"> 21:</span>
<span style="color: #606060;"> 22:</span> <span style="color: #008000;">\'Actually click the node</span>
<span style="color: #606060;"> 23:</span> oTree.Click iX+2, iY+2

Thanks again to Yanir Goren for this great QTip!

, ,

Report Hierarchical Events in QTP Log

0
by on April 11, 2008 at 18:48

Motivation

The QTP native log has many drawbacks, especially if your scripts relay heavily on Functions. While Actions are reported in an hierarchical structure (i.e. nested according to their call chain), events reported by function are reported in a flat, unsorted manner. This can be illustrated by the following example:

image image

The following QTip will allow you to report custom events in an hierarchical manner. This will allow you to achieve reports like this one:

image

Notice that the report is structured as an hierarchical tree, even though all the events are executed within the same action. You can decide when to open a new node, and when to report in a flat manner.

How it’s done

The technique relays on three steps:

  1. Creating a new node in the results tree.
  2. Reporting events / Creating additional sub-nodes (they will be children / grandchildren of the node we’ve created)
  3. Climbing back “up” the hierarchy and reporting more events (they will be siblings of the node we’ve created)

 

Creating a new node in the results tree:

<span style="color: #008000;">\'dicMetaDescription will hold our new node\'s details</span>
<span style="color: #0000ff;">Set</span> dicMetaDescription = CreateObject(<span style="color: #006080;">"Scripting.Dictionary"</span>)

<span style="color: #008000;">\'Set node status</span>
dicMetaDescription(<span style="color: #006080;">"Status"</span>) = MicDone

<span style="color: #008000;">\'Set node\'s header</span>
dicMetaDescription(<span style="color: #006080;">"PlainTextNodeName"</span>) = <span style="color: #006080;">"A node with children"</span>

<span style="color: #008000;">\'Set the node\'s details. HTML is allowed</span>
dicMetaDescription(<span style="color: #006080;">"StepHtmlInfo"</span>) = <span style="color: #006080;">"<DIV align=left><H1>HTML layout testing</H1><b>This</b> can be handy to have.</DIV>"</span>

<span style="color: #008000;">\'Some backdoor settings:</span>
dicMetaDescription(<span style="color: #006080;">"DllIconIndex"</span>) = 206
dicMetaDescription(<span style="color: #006080;">"DllIconSelIndex"</span>) = 206
dicMetaDescription(<span style="color: #006080;">"DllPAth"</span>) = <span style="color: #006080;">"C:\Program Files\Mercury Interactive\QuickTest Professional\bin\ContextManager.dll"</span>
<span style="color: #008000;">\'If you\'re using QTP 9.5, replace the previous line into:</span>
<span style="color: #008000;">\'dicMetaDescription("DllPAth") = "C:\Program Files\HP\QuickTest Professional\bin\ContextManager.dll"</span>

<span style="color: #008000;">\'Actually do the report, and get the new report node ID</span>
intContext = Reporter.LogEvent(<span style="color: #006080;">"User"</span>, dicMetaDescription, Reporter.GetContext)

<span style="color: #008000;">\'Set the new report node as a parent</span>
<span style="color: #008000;">\'From now on, all reports will be added under this node</span>
Reporter.SetContext intContext

Reporting Events:

This is done the old fashion way:

Reporter.ReportEvent MicPass, <span style="color: #006080;">"Some Header"</span>, <span style="color: #006080;">"Some Details"</span>

If we’d like, we could open new nodes under our current node by repeating step 1.

Climbing up the hierarchy:

After reporting a few events under the current node, we’d like to “climb up”, and report the following events in a higher level (i.e., no longer under the node). You may need to do this several times in order to return to the report’s “top” level, depending on the number of sub-nodes you’ve created in your report.

<span style="color: #008000;">\'Now return to the parent level:</span>
Reporter.UnSetContext

*This article is based on stalis‘s post in SQAForums

, ,

Formatting Messages and Strings

0
by on April 1, 2008 at 23:08

The System.Text.StringBuilder class represents a mutable string of characters.

This class represents a string-like object whose value is a mutable sequence of characters. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters.

the AppendFormat method Appends a formatted string, which contains zero or more format specifications, to this instance. Each format specification is replaced by the string representation of a corresponding object argument.

<span style="color: #008000">\' ** Indicates the index of the supplied arguments to be inserted into the string.</span>
fName = <span style="color: #006080">"Dani"</span>
lName = <span style="color: #006080">"Vainstein"</span>
<span style="color: #0000ff">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080">"System.Text.StringBuilder"</span> )
sb.AppendFormat <span style="color: #006080">"My name is {0} {1}, I own a {2} and {3} name is {4}"</span>, fName, lName,  <span style="color: #006080">"dog"</span>, <span style="color: #006080">"his"</span>, <span style="color: #006080">"Star"</span>
Print sb.ToString
 
<span style="color: #008000">\' ** Insert an integer value that is 5 digits in length, prepending leading zeros if necessary.</span>
<span style="color: #0000ff">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080">"System.Text.StringBuilder"</span> )
n1 = 25000 : n2 = 23.5 : n3 = 3345 : n4 = 928564
neg = -19.95 : pos = 19.95 : zero = 0
sb.AppendFormat <span style="color: #006080">"1. decimal with leading zeros: {0:d5}."</span>, n1
sb.Append vbCrLf
sb.AppendFormat <span style="color: #006080">"2. floating with 3 places after point: {0:f3}."</span>, n2
sb.Append vbCrLf
sb.AppendFormat <span style="color: #006080">"3. cientific rounded to 3 digits: {0:e3}."</span>, n2
sb.Append vbCrLf
sb.AppendFormat <span style="color: #006080">"4. negative formatted number: {0:N}."</span>, n1 * ( - 1 )
sb.Append vbCrLf
sb.AppendFormat <span style="color: #006080">"5. percentage: {0:P}."</span>, n2 / 100
sb.Append vbCrLf
sb.AppendFormat <span style="color: #006080">"6. hexadecimal 16-bit representation: {0:X4}."</span>, n3
sb.Append vbCrLf
sb.AppendFormat <span style="color: #006080">"7. hexadecimal 32-bit representation: {0:x8}."</span>, n4
sb.Append vbCrLf
sb.AppendFormat <span style="color: #006080">"8. hexadecimal 64-bit representation: {0:X16}."</span>, n4
sb.Append vbCrLf
sb.AppendFormat <span style="color: #006080">"9. positive currency: {0:$#,##0.00;($#,##0.00);Zero}."</span>, neg
sb.Append vbCrLf
sb.AppendFormat <span style="color: #006080">"10. negative currency: {0:$#,##0.00;($#,##0.00);Zero}."</span>, pos
sb.Append vbCrLf
sb.AppendFormat <span style="color: #006080">"11. zero currency: {0:$#,##0.00;($#,##0.00);Zero}."</span>, zero
sb.Append vbCrLf
sb.AppendFormat <span style="color: #006080">"12. my phone number: {0:(###) ##-#######}."</span>, 972507317908
sb.Append vbCrLf
sb.AppendFormat <span style="color: #006080">"13. one million?: {0:#,0}."</span>, 1000000
Print sb.ToString
 
<span style="color: #008000">\' ** Inserts the value into a column of 10 characters with alignment to the right of the column.</span>
<span style="color: #0000ff">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080">"System.Text.StringBuilder"</span> )
sb.AppendFormat <span style="color: #006080">"Right aligned 10 character column: \'{0,10}\'."</span>, <span style="color: #006080">"right"</span>
Print sb.ToString
 
<span style="color: #008000">\' ** Inserts the value into a column of 10 characters with alignment fo the left of the column.</span>
<span style="color: #0000ff">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080">"System.Text.StringBuilder"</span> )
sb.AppendFormat <span style="color: #006080">"Left aligned 10 character column: \'{0,-10}\'."</span>, <span style="color: #006080">"left"</span>
Print sb.ToString
 
<span style="color: #008000">\' ** To prevent the insertion of an argument and allow for curly braces to be inserted into the string, two</span>
<span style="color: #008000">\' ** braces must be placed together to cause an escape from the formatting sequence.</span>
<span style="color: #0000ff">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080">"System.Text.StringBuilder"</span> )
sb.AppendFormat <span style="color: #006080">"Use two braces to put a single brace in the output without formatting. {{0}}"</span>, <span style="color: #006080">"No Used"</span>
Print sb.ToString
 
<span style="color: #008000">\' ** Formating a date</span>
<span style="color: #0000ff">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080">"System.Text.StringBuilder"</span> )
<span style="color: #0000ff">Set</span> today = DotNetFactory.CreateInstance( <span style="color: #006080">"System.DateTime"</span> ).Now()
sb.AppendFormat <span style="color: #006080">"Today is {0:dd-mm-yyyy}"</span>, today 
Print sb.Tostring
 
<span style="color: #008000">\' ** The format used as variable, and the text can be also an array.</span>
<span style="color: #0000ff">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080">"System.Text.StringBuilder"</span> )
arr = Array( <span style="color: #006080">"Software"</span>, 1234.56 )
str1 = <span style="color: #006080">"This {0} costs: {1:C}."</span>
sb.AppendFormat str1, arr 
Print sb.Tostring

, ,

Dictionary of Employees

0
by on April 1, 2008 at 22:46

Demonstration script that combines the usage of classes and dictionary object. Script must be run on the local computer.

<span style="color: #0000ff;">Class</span> Employee
    <span style="color: #0000ff;">Private</span> fname,lname,m_id,m_city,m_country

    <span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Property</span> <span style="color: #0000ff;">Get</span> FirstName()
        FirstName = fname
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Property</span>
    <span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Property</span> <span style="color: #0000ff;">Let</span> FirstName( value )
        fname = value
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Property</span>
    <span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Property</span> <span style="color: #0000ff;">Get</span> LastName()
        LastName = lname
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Property</span>
    <span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Property</span> <span style="color: #0000ff;">Let</span> LastName( value )
        lname = value
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Property</span>
    <span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Property</span> <span style="color: #0000ff;">Get</span> Id()
        Id = m_id
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Property</span>
    <span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Property</span> <span style="color: #0000ff;">Let</span> Id( value )
        m_id = value
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Property</span>
     <span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Property</span> <span style="color: #0000ff;">Get</span> City()
        City = m_city
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Property</span>
    <span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Property</span> <span style="color: #0000ff;">Let</span> City( value )
        m_city = value
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Property</span>
     <span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Property</span> <span style="color: #0000ff;">Get</span> Country()
        Country = m_country
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Property</span>
    <span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Property</span> <span style="color: #0000ff;">Let</span> Country( value )
        m_country = value
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Property</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Class</span>

<span style="color: #0000ff;">Set</span> listEmployee = CreateObject( <span style="color: #006080;">"Scripting.Dictionary"</span> )
listEmployee.Add <span style="color: #006080;">"1"</span>, <span style="color: #0000ff;">New</span> Employee
listEmployee( <span style="color: #006080;">"1"</span> ).Id = <span style="color: #006080;">"3425789734"</span>
listEmployee( <span style="color: #006080;">"1"</span> ).FirstName = <span style="color: #006080;">"Dani"</span>
listEmployee( <span style="color: #006080;">"1"</span> ).LastName = <span style="color: #006080;">"Vainstein"</span>
listEmployee( <span style="color: #006080;">"1"</span> ).City = <span style="color: #006080;">"Petaj Tikva"</span>
listEmployee( <span style="color: #006080;">"1"</span> ).Country = <span style="color: #006080;">"Israel"</span>
listEmployee.Add <span style="color: #006080;">"2"</span>, <span style="color: #0000ff;">New</span> Employee
listEmployee( <span style="color: #006080;">"2"</span> ).Id = <span style="color: #006080;">"456875677"</span>
listEmployee( <span style="color: #006080;">"2"</span> ).FirstName = <span style="color: #006080;">"Sara"</span>
listEmployee( <span style="color: #006080;">"2"</span> ).LastName = <span style="color: #006080;">"Vainstein"</span>
listEmployee( <span style="color: #006080;">"2"</span> ).City = <span style="color: #006080;">"BatYam"</span>
listEmployee( <span style="color: #006080;">"2"</span> ).Country = <span style="color: #006080;">"Israel"</span>
<span style="color: #008000;">\' ** Printing</span>
<span style="color: #0000ff;">For</span> nEmp = 1 <span style="color: #0000ff;">To</span> listEmployee.Count
    Print <span style="color: #006080;">"Employee No: "</span> & nEmp
    Print listEmployee( <span style="color: #0000ff;">Cstr</span>( nEmp ) ).FirstName
    Print listEmployee( <span style="color: #0000ff;">Cstr</span>( nEmp ) ).LastName
    Print listEmployee( <span style="color: #0000ff;">Cstr</span>( nEmp ) ).Country
Next

, ,

Problem – Retrieve all Sections from ini File

0
by on April 1, 2008 at 21:16

API Syntax :

DWORD GetPrivateProfileSectionNames(

LPCTSTR lpszReturnBuffer,

DWORD nSize,

LPCTSTR lpFileName

);

Parameters :

clip_image001 lpszReturnBuffer – [out] buffer that receives the section names associated with the named file. The buffer is filled with one or more null-terminated strings; the last string is followed by a second null character

clip_image001[1] nSize – [in] The size of the buffer pointed to by the lpszReturnBuffer parameter, in characters.

clip_image001[2] lpFileName – [in] The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.

Return Value : The return value specifies the number of characters copied to the specified buffer, not including the terminating null character. If the buffer is not large enough to contain all the section names associated with the specified initialization file, the return value is equal to the size specified by nSize minus two.

This function does not with QTP. this is because the returned string value is delimited with Null. so, QTP string are null terminated, so only the first string is returned, even that the buffer size is bigger.

, , ,

Create a CSV File

0
by on April 1, 2008 at 20:47

CSV is short for comma-separated-values file, a text file in which individual elements separated by commas. For example, say you have a CSV file consisting of user first names, last names, and job titles; that file might look something like this:

Ken,Myer,Accountant

Pilar,Ackerman,Vice-President

Carol,Philips,Research Specialist

How can you create a CSV file all your own? That’s easy: just use the FileSystemObject.

Let’s take a look at a very simple demonstration script, then we’ll take a look at a more practical example. Here’s a sample script that writes the string A,B,C to a text file named Test.csv:

<span style="color: #0000ff;">Const</span> ForReading = 1
<span style="color: #0000ff;">Const</span> ForWriting = 2

<span style="color: #0000ff;">Set</span> fso = CreateObject( <span style="color: #006080;">"Scripting.FileSystemObject"</span> )
<span style="color: #0000ff;">Set</span> logFile = fso.CreateTextFile( <span style="color: #006080;">"test.csv"</span>, ForWriting, <span style="color: #0000ff;">True</span> )

logFile.Write <span style="color: #006080;">"A,"</span>
logFile.Write <span style="color: #006080;">"B,"</span>
logFile.Write <span style="color: #006080;">"C"</span>
logFile.Writeline

logFile.Close

We start out by defining a constant named ForWriting and setting the value to 2; any time you use the FileSystemObject, you need to use the appropriate constant, depending on whether you want to read from, write to, or append a file. We create an instance of the FileSystemObject, and then use the CreateTextFile method to create a new text file named Test.cvs. (Notice that we did not specify a path; that means that the text file will be created in the same folder as the script. Had we wanted to, we could have specified a complete path, like C:\Scripts\Logfiles\Test.csv.)

When we call the CreateTextFile method, we simultaneously create an object reference to the new file; in our script, we named that object reference logFile. With the object reference in hand, we can then use the Write method to write data to the file. Notice that we begin by writing the string A,. The Write method writes the specified data (in this case A,), and then leaves the cursor in place. Because of that, when we next call this method the string B, is tacked on right next to the A,. As a result, our text file looks like this:

A,B,

We call the Write method a third time, and write C. Because this marks the end of the line, we don’t append a comma after it; instead, we call the WriteLine method, which is equivalent to hitting ENTER on the keyboard. That gives us a text file that looks like this:

A,B,C

Note that there are no spaces between commas and the start of the next item. When one item ends, the next begins right away.Now let’s take a look at a more practical example. This script uses WMI to retrieve service information, then uses the FileSystemObject to write that information to a text file named Process_List.csv:

<span style="color: #0000ff;">Const</span> ForAppending = 2

<span style="color: #0000ff;">Set</span> fso = CreateObject( <span style="color: #006080;">"Scripting.FileSystemObject"</span> )
<span style="color: #0000ff;">Set</span> logFile = fso.CreateTextFile( <span style="color: #006080;">"process_list.csv"</span>, ForWriting, <span style="color: #0000ff;">True</span> )

<span style="color: #0000ff;">Set</span> wmiService = GetObject( <span style="color: #006080;">"winmgmts:root\cimv2"</span> )
<span style="color: #0000ff;">Set</span> processList = wmiService.ExecQuery( <span style="color: #006080;">"Select * from Win32_Process"</span> )
<span style="color: #0000ff;">For</span> <span style="color: #0000ff;">Each</span> process <span style="color: #0000ff;">in</span> processList
    logFile.Write process.Name & <span style="color: #006080;">","</span>
    logFile.Write process.ProcessId & <span style="color: #006080;">","</span>
    logFile.Write process.ThreadCount
    logFile.Writeline
<span style="color: #0000ff;">Next</span>

logFile.Close

As you can see, we use the same exact technique used in our demonstration script. The biggest difference is that we aren’t using hard-coded values like A, B, and C. Instead, we’re using variables like process.Name. That’s not a problem, we just use the variable as the parameter to the Write method. However, because these are variables, we cannot enclose them and the following comma in double quotes. A line of code like this will not work

logFile.Write <span style="color: #006080;">"process.Name,"</span>

Instead, we specify the variable (process.Name), and then use the ampersand to tack a comma (“,”) to the end. We do the same thing for the ProcessId but not for ThreadCount; that’s because ThreadCount is the last item on each line. Consequently, we use WriteLine to press ENTER and start a new line in the file. Because we do this within a For Each loop, we’ll end up writing this information for each service installed on the computer.

The net result is a text file that looks like this

System Idle Process,0,2

System,4,69

smss.exe,636,3

csrss.exe,704,13

winlogon.exe,728,20

services.exe,780,15

lsass.exe,792,20

Although we could have cut down the number of lines of code slightly (by concatenating all our variables and what-not) writing the code in this fashion makes it pretty easy for you to see what’s going on. It also makes it easy for you to add new items to each line. Want to include the process ExecutablePath? Then just add a line of code like this:

logFile.Write process.ExecutablePath & <span style="color: #006080;">","</span>

One more thing: When you’re working with WMI, the preceding script will likely handle all your needs. When working with other scripting technologies, however, you might encounter a problem with data that includes commas. For example, suppose you have a text file of users, their office addresses, and their job titles. One user has an address of 2049, but another has an address of 2050, Suite A. Your subsequent text file looks like this:

Ken,Myer,2049,Accountant

Pilar,Ackerman,2050,Suite A,Vice-President

How do you deal with embedded commas like this? The secret is to surround each field with double quotes; if your text file looks like this, the embedded commas are ignored:

“Ken”,”Myer”,”2049″,”Accountant”

“Pilar”,”Ackerman”,”2050,Suite A”,”Vice-President”

Moreover, how do you surround your fields with double quotes? Here’s another sample script that uses the function Chr(34) to write a double quote mark, writes a service property (such as process.Name), uses Chr(34) to write another double quote mark, and only then tacks a comma on the end.

<span style="color: #0000ff;">Const</span> ForWriting = 2

<span style="color: #0000ff;">Set</span> fso = CreateObject( <span style="color: #006080;">"Scripting.FileSystemObject"</span> )
<span style="color: #0000ff;">Set</span> logFile = fso.CreateTextFile( <span style="color: #006080;">"process_list.csv"</span>, ForWriting, <span style="color: #0000ff;">True</span> )

<span style="color: #0000ff;">Set</span> wmiService = GetObject( <span style="color: #006080;">"winmgmts:root\cimv2"</span> )
<span style="color: #0000ff;">Set</span> processList = wmiService.ExecQuery( <span style="color: #006080;">"Select * from Win32_Process"</span> )
<span style="color: #0000ff;">For</span> <span style="color: #0000ff;">Each</span> process <span style="color: #0000ff;">in</span> processList
    logFile.Write Chr(34) & process.Name & Chr(34) & <span style="color: #006080;">","</span>
    logFile.Write & Chr(34) & process.ProcessId & Chr(34) & <span style="color: #006080;">","</span>
    logFile.Write & Chr(34) & process.ThreadCount & Chr(34)
    logFile.Writeline
<span style="color: #0000ff;">Next</span>

logFile.Close

Based on “How Can I Create a CSV File?” from the Scripting Guys.

,

Tally Items in a Text File

0
by on April 1, 2008 at 20:40

Lets say we have a text file with customer names:

Customer, City

Michael Doe, Seattle

Lionel Adams, Seattle

Wendy Russel, Los Angeles

Jonathan Maine, Chicago

Willy Kalkstein, Boston

Jimmy Andreas, Seattle

Jose Luis Alvear, Los Angeles

Jim Lung, Pittsburgh

What you’re looking for is a script that can read through the file and report the number of occurrences for each item; in other words, a script that can return a report similar to this:

Boston –> 1
Chicago –> 1
Los Angeles –> 2
Pittsburgh –> 1
Seattle –> 3

There are several different ways you can do this, but we preferred to show you a very powerful and flexible method for dealing with text files as databases. It’s not well-known, but you can actually use ADO (ActiveX Data Objects) to run database queries against a text file. And that’s exactly what we do with this script:

<span style="color: #0000ff;">Const</span> adOpenStatic = 3
<span style="color: #0000ff;">Const</span> adLockOptimistic = 3
<span style="color: #0000ff;">Const</span> adCmdText = &H0001

<span style="color: #0000ff;">Set</span> con = CreateObject( <span style="color: #006080;">"ADODB.Connection"</span> )
<span style="color: #0000ff;">Set</span> rst = CreateObject( <span style="color: #006080;">"ADODB.Recordset"</span> )

path2TextFile = <span style="color: #006080;">"C:\Data"</span>
dataProvider = <span style="color: #006080;">"Provider=Microsoft.Jet.OLEDB.4.0;"</span>
dataSource = <span style="color: #006080;">"Data Source="</span> & path2TextFile & <span style="color: #006080;">";"</span>
extProperties = <span style="color: #006080;">"Extended Properties="</span><span style="color: #006080;">"text;HDR=Yes;FMT=Delimited"</span><span style="color: #006080;">""</span>

con.Open dataProvider & dataSource & extProperties
<span style="color: #0000ff;">If</span> con.State = 1 <span style="color: #0000ff;">Then</span>
   fileName = <span style="color: #006080;">"Customers.txt"</span>
   sql = <span style="color: #006080;">"SELECT City, COUNT(City) AS CountCity "</span> & _
         <span style="color: #006080;">"FROM "</span> & fileName & <span style="color: #006080;">" GROUP BY City"</span>
   rst.Open sql, con, adOpenStatic, adLockOptimistic, adCmdText
   <span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">Until</span> rst.EOF
      Print rst.Fields.Item( <span style="color: #006080;">"City"</span> ) & <span style="color: #006080;">" -> "</span> & rst.Fields.Item( <span style="color: #006080;">"CountCity"</span> )
      rst.MoveNext
   <span style="color: #0000ff;">Loop</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>

Let’s start with the SQL query we use to retrieve data:

path2TextFile = <span style="color: #006080;">"C:\Data"</span>
dataProvider = <span style="color: #006080;">"Provider=Microsoft.Jet.OLEDB.4.0;"</span>
dataSource = <span style="color: #006080;">"Data Source="</span> & path2TextFile & <span style="color: #006080;">";"</span>
extProperties = <span style="color: #006080;">"Extended Properties="</span><span style="color: #006080;">"text;HDR=Yes;FMT=Delimited"</span><span style="color: #006080;">""</span>

con.Open dataProvider & dataSource & extProperties

At first glance, you might be confused by this query. Each line in the file contains a city name and the customer name. Yet, even though we have only one field in our “database,” our SQL query specifies two separate items: City and Count (City) AS CountCity. What gives?

Actually, we are not selecting two different fields; instead, we are selecting one field (City) and one “calculated field” we can use to tally up the number of times each city appears in the database. (A calculated field is a field that we calculate based on data found in other fields; the field itself does not actually exist in the database.) The item Count(City) AS CountCity (in conjunction with the clause GROUP BY City) can be read like this: Count up the number of times each city appears in the file and then store that number in a calculated field named CountCity.

The nice thing is that we get back this recordset with the city totals calculated for us. Alternatively, we could have opened the text file, read each line, and used an array or a Dictionary object to keep track of the number of times each city name appeared in the file. However, ADO makes this process far easier.

Once we get a recordset back and connection state open ( 1 ) we simply use this code to loop through all the records, echoing back the name of the city and the number of times each city appears in the file:

<span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">Until</span> rst.EOF
   Print rst.Fields.Item( <span style="color: #006080;">"City"</span> ) & <span style="color: #006080;">" -> "</span> & rst.Fields.Item( <span style="color: #006080;">"CountCity"</span> )
   rst.MoveNext
<span style="color: #0000ff;">Loop</span>

Based on “How Can I Tally Items in a Text File?” from the Scripting Guys.

,

Registering .NET Objects

0
by on April 1, 2008 at 19:43

You can access some .NET classes from script directly, with one preparatory step on a system where you want to do it. Here’s why and how.

Basically a .NET class which is scriptable is one which is COM-accessible. You can write one yourself; you need to make sure that it is public, that it returns a classic COM data type, that the assembly containing it is signed, and that it is not MustInherit/Notinheritable (or is not sealed/abstract if looking at as a C# class) is roughly the criteria it must meet – I have never been clear on the class attributes I specified last, though, so take those with a chunk of salt.

It turns out that several of the core .NET runtime classes meet these criteria, particularly some in mscorlib.dll which are of interest, but they are not COM-accessible because they are not registered when the .NET runtime is installed. So how do we get access to them? Simple, we register them! The following command line will export a TLB from mscorlib.dll and register it:

regasm /nologo /tlb:mscorlib.tlb mscorlib.dll

you can also just export the data to a REG file to see what it would register:

regasm /nologo /regfile:mscorlib.reg mscorlib.dll

 

Unfortunately the things you can access this way are pretty raw. The generally useful ones I’ve explored include some System.Collections classes which can do special things that take a lot of work in script (the Stack, Queue, SortedList, ArrayList, and Hashtable classes) and the System.Text.StringBuilder class which can be used for super-fast string building. This also means you have to deal with some method overloading problems

,

Retrieving Visual Studio Last Version

0
by on April 1, 2008 at 17:31

Describes how to automate the Visual Studio .NET IDE from outside the IDE.

The following VBScript sample shows how to create and instance of VS.NET 2005 and show its name and version:

<span style="color: #0000ff;">Dim</span> dte

<span style="color: #008000;">\' ** Creates an instance of the VS.NET 2003 IDE</span>
<span style="color: #0000ff;">Set</span> dte = CreateObject( <span style="color: #006080;">"VisualStudio.DTE.8.0"</span> )

<span style="color: #008000;">\' ** While the instance is still invisible, show its name and version</span>
Print dte.Name & <span style="color: #006080;">" "</span> & dte.Version

<span style="color: #008000;">\' ** Make it visible and keep it open after we finish this script</span>
dte.MainWindow.Visible = <span style="color: #0000ff;">True</span>
dte.UserControl = <span style="color: #0000ff;">True</span>

based on http://support.microsoft.com/kb/555391

, ,