Searching for Text in a String

Microsoft® Windows® 2000 Scripting Guide

As noted previously, you do not always have control over the text returned to you when running a script. For example, suppose your script opens log files on a computer and checks to see whether a particular action succeeded or failed. In most cases, the log file will contain far more text than simply the word Succeeded or Failed. Instead, the log might look like this:

MSI: False - MSP: True - CMW: False 
   MSISW: REINSTALL=all REINSTALLMODE=vomus /QB /L*V+ C:\WINNT\DEBUG\officexp_sp1.log 
   MSPSW: REBOOT=ReallySuppress /QB- /L*V+ C:\WINDOWS\DEBUG\officexp_sp1.log 
   InstallFile Result: [0] and Success: True 
   Client Maint Wiz: PROPLUS detected

To determine the success or failure of the activity, your script will need to read in all the text and programmatically search for the relevant piece of information (in this example, the phrase "Success: True").

The InStr function provides a way to search for the presence of a string within another string. In practical terms, this allows you to create scripts that perform tasks such as reading through a log file and reporting back only if a line contains a specific error code. For example, imagine a plain-text log file that contains hundreds of lines like this:

6/1/2002   File a.doc successfully copied 
6/1/2002   File b.doc successfully copied 
6/1/2002   Error: File c.doc could not be copied 
6/1/2002   File d.doc successfully copied

Rather than manually read through this log looking for errors, you can create a script that reads each line in the log file, echoing back any lines that contain the word Error. This provides a quick and easy way for you to extract only the operations that failed and thus need your attention.

note Note

  • The InStr function is designed to carry out simple string manipulation. If you need to carry out more complex types of operation, you need to use the VBScript Regular Expressions object. This use of this object is beyond the scope of this book; for more information, see the VBScript link on the Web Resources page at

InStr works by reporting back the character position where the substring is first found. For example, suppose you are looking for the letters ment in the word developmental. InStr returns the value 8 because the letters ment are found beginning in the eighth character position, as shown in Figure 2.16.

Figure 2.16 Character Positions in the Word Developmental


Developmental" alt="Art Image">

By contrast, if you searched for the letters mant, InStr would return 0, indicating that the substring could not be found.

InStr requires two parameters: the string being searched and the string being searched for (the substring). For example, the following line of code searches for the letters ment in the word developmental:

Wscript.Echo InStr("developmental", "ment")

For longer strings, you can set variables equal to the string values and then use those variables as InStr parameters. For example, the following script searches for the words test and strong in the sentence, "This is a test string being searched for two different words."

TestString = "This is a test string being searched for two different words." 
PresentString = "test" 
AbsentString = "strong" 
Wscript.Echo InStr(TestString, PresentString) 
Wscript.Echo InStr(TestString, AbsentString)

When the preceding script runs, the values 11 and 0 will be returned. The value 11 indicates that the word test can be found beginning with the eleventh character. The value 0 indicates that the word strong could not be found anywhere within the string being searched.