Improve Your Home-Brewed Hardware Inventory

Troubleshooting a previous script leads our authors to improve on the original.

4 Min Read
ITPro Today logo in a gray background | ITPro Today


I enjoyed your article "Home-Brewed Hardware Inventory" (July 2003, Instant-Doc ID 39111). The collection process is fabulous! However, whenever I try to run the report agent (BuildReport.vbs), I get the following error message: (36, 5) Microsoft VBScript runtime error. File not found. I looked at the script, and the line that the error message refers to is

Set objTextFile = objFSO.OpenTextFile   (strInvFilePath & strFileName,ForReading) 

I don't see what's wrong with this line. Can you help me?

"Home-Brewed Hardware Inventory" provides a solution for creating a Windows computer hardware inventory by using VBScript and Windows Management Instrumentation (WMI). The answer to this reader's question is useful for adding fault tolerance to any script.

The reader explained that he was using a valid directory path to initialize the script's strInvFilePath variable, which points the report agent to inventory files for processing. However, the script was failing as if the path was invalid. We determined that the problem was a missing backslash () at the end of the value that he was specifying. After the reader added the trailing backslash, BuildReport.vbs functioned properly.

Assume you initialize the strInv-FilePath variable with a value of

"c:invFiles" (strInvFilePath = _   "c:invFiles") 

BuildReport.vbs automatically sets the strFileName variable, which represents-each filename found in the inventory-file directory. Every time the computer inventory agent (Hrd-WrInv.vbs) runs, it generates a unique filename that starts with HrdWrInv_ and ends with the computer name of the computer on which the script ran. For this example, then, strFileName equals HrdWrInv_Computer01. As a result, the file path value that passes to the FileSystemObject's Open-TextFile method is c:invFilesHrd-WrInv_Computer01, which isn't a valid path. The value should be c:invFiles HrdWrInv_Computer01. The missing backslash causes the script to fail.

To make the script more resilient, we created the LastCharCheck function, which callout C in Listing 1 shows. LastCharCheck verifies that the last character in the string is equal to the SearchChar input parameter that the calling script passes to the function. If it is, LastCharCheck remains empty; if it isn't, LastCharCheck is initialized to the value in the SearchChar parameter.

To demonstrate the purpose of LastCharCheck, callout A in Listing 1 shows that the strInvFilePath variable doesn't contain a trailing backslash. Callout B shows how to call the LastCharCheck function. In the first call to LastCharCheck, the function appends a backslash to strInv-FilePath. In the second call, the function doesn't append a backslash because the function determines that strReportPath already contains a trailing backslash.

We pass in the backslash as a parameter to the LastCharCheck function to make it more generally useful. If you have a variable that must end with a colon (:), you can set the second parameter of the function (SearchChar) to ":" when you call the function. Then, if the script operator forgets to add a colon at the end of a variable that requires one, LastCharCheck will silently add one and thus avoid a possible script error.

In addition, you can make other modifications to simplify this script solution. First, consider replacing the VBScript InStr function with the VBScript Right function. Because the script searches for the last character in a string, you simply pass to the Right function the FilePath and the number of characters to evaluate from the right side of the string. In this case, you pass a value of 1 so that the Right function evaluates the first character from the right side of the string. If the character at the end of the string isn't equal to the SearchChar value that you pass into the LastCharCheck function, the LastCharCheck function appends the value of the SearchChar to the end of the string, as Listing 2 shows. Another possible improvement involves replacing the LastCharCheck function with the FileSystemObject's BuildPath method, as Listing 3 shows. BuildPath appends a backslash to a file path if the backslash is missing. This change simplifies the script updates for the BuildReport.vbs script. However, unlike LastCharCheck, BuildPath is limited to appending a missing backslash to a file path; LastCharCheck lets you append any value to the end of a string.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like