Do you have a scripting-related question or problem? You can send your question or problem to firstname.lastname@example.org.
I'm just learning the ins and outs of Windows Script Host (WSH). Does WSH provide a way to search and replace text in a text file?
You can use VBScript's regular expression object, RegExp, to search and replace text in a string. If you couple the RegExp object with the File System Object (FSO) model's FileSystemObject object, you can create a relatively simple WSH script that reads a text file into a string, performs the replace operation on the string, and writes the modified string back to the file.
Let's look at an example. Suppose you want to replace all occurrences of the string "cat's" with the string "dog's" in the text file sample.txt, which Figure 1 shows. The script in Listing 1 demonstrates how you can use the FileSystemObject and RegExp objects to accomplish this task.
Listing 1 begins by declaring the variables you'll use in the script. Of particular importance are the strPattern and strReplace variables. The strPattern variable holds the text, or pattern, you're searching for (i.e., "cat's"). The strReplace variable stores the replacement string (i.e., "dog's").
The code at callout A retrieves the text in sample.txt. First, you use the FileSystemObject object's OpenTextFile method to open sample.txt in Read-only mode. Then, you call the TextStream object's ReadAll method to read the contents of the entire file into the strData variable. Finally, you use the TextStream object's Close method to close sample.txt.
The code at callout B performs the search-and-replace operation. After you apply VBScript's New keyword to create an instance of the RegExp object, you set three of the object's properties:
- Pattern—You set the Pattern property to the string or regular expression template you're searching for.
- Global—This property governs the scope of the search. Setting Global to True tells the regular expression to search the entire string (i.e., find all matches). Setting Global to False (the default) tells the regular expression to find only the first match, after which the search stops.
- IgnoreCase—You use the IgnoreCase property to specify whether you want a case-insensitive or case-sensitive search. Setting IgnoreCase to True tells the regular expression to perform a case-insensitive search. Setting IgnoreCase to False (the default) tells the regular expression to perform a case-sensitive search.
Next, you call the regular expression's Replace method. Replace takes two mandatory string arguments. The first argument is the string you're searching for and potentially modifying; the second argument is the replacement text. After the Replace method performs its magic, you have an updated string in the strData variable.
The code at callout C returns the modified string to sample.txt. You reopen sample.txt but this time in Write mode. You then use the TextStream object's Write method to write the modified string to sample.txt, after which you close the file. Figure 2 shows the modified sample.txt file.
If you're unfamiliar with VBScript's regular expressions, check out Dino Esposito, "Understanding VBScript: Using Regular Expressions," August 2000. If you're unfamiliar with the FSO model's FileSystemObject object or TextStream object, check out Dino Esposito, "Understanding VBScript: Manipulating Files with FileSystemObject," January 2000, and "Understanding VBScript: The TextStream Object," May 2000, respectively.