Hey, Scripting Guy! Question

Hey, Scripting Guy! I’m trying to use a LIKE query in my WMI scripts. The query works fine on Windows XP computers, but I can’t get it work on Windows 2000 computers. Do you know what I’m doing wrong?

-- GS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, GS. Actually you’re not doing anything wrong. You’re using a script similar to this, which uses the LIKE operator to search for all the files on a computer whose file name includes a ~ (these are almost always temporary files):

strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile where FileName Like '%~%'")
For Each objFile in colFiles
    Wscript.Echo objFile.Name
Next

And you’re right: this works great on Windows XP and Windows Server 2003, and we encourage people to take advantage of this new capability; it can be incredibly useful. However, those are the only two platforms which support the LIKE operator; this script won’t work on Windows 2000 (or Windows NT 4.0 or Windows 98) because those platforms don’t support the LIKE operator. And, before you ask, there are no plans to issue an update to older versions of Windows so that they will support this capability. Sorry; we wish there were such plans, too.

So is there a workaround for this problem? Well, kind of, but it’s not a particularly good one. About the best you can do is return a list of all the files on a computer, and then use the VBScript function InStr to see if the tilde appears in any of the file names. The following script does just that, although to ensure that the script runs a bit faster we added a WHERE clause that limits the search solely to the Scripts folder. Otherwise, the script would return a list of all the files on the computer, something we don’t want to do in a testing situation:

strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile WHERE Path = '\\Scripts\\'")
For Each objFile in colFiles
    If InStr(objFile.Name,"~") <> 0 Then
        Wscript.Echo objFile.Name
    End If
Next