Rem: Editing the Boot.ini File
Bob Wells shows you how to use VBScript to access the boot.ini file, remove text, and write the file back to disk.
August 27, 2001
I need to write a script that accesses the C:boot.ini file, removes the text /noguiboot, and writes the file back to the disk. Do you have any suggestions about how I can use VBScript to accomplish this task?
You can approach this task two ways. You can use the Scripting Runtime Library's FileSystemObject object with VBScript's RegExp object (the object in VBScript 5.0 or later that supports regular expressions), or you can use the FileSystemObject object with VBScript's Replace function.
Because both solutions let you replace multiple occurrences of the target string, the solution you choose to implement is largely a matter of preference. However, the RegExp object supports case-insensitive searches. Listing 1, page 6, contains a script that uses the RegExp object but includes the line of code to use if you want to apply the Replace function instead. If you use the Replace function, remember that case matters in the search string you specify.
As Listing 1 shows, the script creates an instance of the FileSystemObject object and sets a file reference named fsoFile to C:boot.ini. Then, the code at callout A in Listing 1 checks whether the ReadOnly attribute is set on the boot.ini file. If the attribute is set, the If...Then...Else statement removes the ReadOnly setting. In addition, the statement sets the bToggledReadOnlyAttribute flag to True so that the script knows to return the attribute to its original setting after the file modification is complete.
Next, the script begins the real work of modifying the boot.ini file. The script creates a TextStream object that points to C:boot.ini, reads the entire boot.ini file into the string variable named strOldBootIni, then closes the file. The strOldBootIni variable contains all the file's contents, including formatting characters such as line returns. You don't need to break the file into individual lines because the formatting characters don't affect the operation of the RegExp object or the Replace function.
With the contents of boot.ini in the strOldBootIni variable, the code at callout B removes the unwanted text. The code begins by creating an instance of the RegExp object, then sets the object's Pattern, Global, and IgnoreCase properties. The Pattern property identifies the string for which the RegExp object is to search. In this case, the target string is /noguiboot. Setting the Global property to True tells the RegExp object to find all occurrences of the string that the Pattern property identifies. Setting the IgnoreCase property to True tells the RegExp object to perform a case-insensitive search.
Having set the RegExp object's properties, the code at callout B invokes the RegExp object's Replace method and passes to the method the target string and the new string that will replace the target pattern. In this case, the new string is an empty string (""). Replacing an existing string with an empty string has the same effect as deleting the existing string. The RegExp object's Replace method returns the modified boot.ini contents, which the code writes to a new string variable named strNewBootIni.
The commented code at callout C contains the line to use if you want to apply the Replace function instead of the RegExp object. To use the Replace function, comment or remove the five lines of regular-expression code at callout B, then uncomment the line of code at callout C.
Next, the script reopens the original C:boot.ini file, writes the modified content in strNewBootIni to the file, then closes the file. Finally, the script sets the ReadOnly attribute if it was set initially.
Before you use this script, you should back up your existing original boot.ini file. You can use the FileSystemObject object's CopyFile method to incorporate the backup operation into the script. For information about the CopyFile method, go to the Microsoft Windows Script Technologies Web site (http://msdn.microsoft.com/scripting). On the left-side menu, click VBScript, Documentation, FileSystemObject User's Guide. This guide includes links to several resources that discuss how to use the FileSystemObject object and its method and properties.
About the Author
You May Also Like