How to add a wildcard on a VB Script

javad paasha 1 Reputation point


I am using this vbscript in SCOM to monitor the existence of a file. It works without a wildcard, but when I add a wildcard it doesn't work. I also need to find the existence of 2 different filenames with wildcards within the same location.

Dim oAPI, oBag
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFile = "D:\Scripts\Backup*.csv"
If objFSO.FileExists(strFile) Then
Call oBag.AddValue("Status","Ok")
Call oAPI.Return(oBag)
Call oBag.AddValue("Status","Error")
Call oAPI.Return(oBag)
End If

Operations Manager
Operations Manager
A family of System Center products that provide infrastructure monitoring, help ensure the predictable performance and availability of vital applications, and offer comprehensive monitoring for datacenters and cloud, both private and public.
1,413 questions
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,563 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 111.7K Reputation points

    If you know the names, then use two conditions:

    If objFSO.FileExists("D:\Scripts\Backup1.csv") And objFSO.FileExists("D:\Scripts\Backup2.csv") then…

    Do you want to check two files using wildcards like “SomeFile*.csv” and “OtherFile*.csv”?

    0 comments No comments

  2. Jeff 156 Reputation points

    It's because
    strFile = "D:\Scripts\Backup*.csv" treated as a string literal.

    i.e. The script will look for a file that is called "Backup*.csv" in the folder "D:\Scripts"

    To put it another way, it sees/treats the * as part of the filename rather than the wildcard that you're (quite reasonably) expecting.

    Have a look at these two articles (both on websites other than this one):
    Tek-Tips - Finding a file using a wildcard


    Your other choices might be to use a batch file (which will happily use wildcards, though I suspect you're using VBS because SCOM won't run batch files, in which case, fair play to you! :-) ), or use PowerShell.

    0 comments No comments

  3. AlexZhu-MSFT 5,476 Reputation points Microsoft Vendor


    FileExists function does not support wildcard. As Jeff-2958 suggested, we may use a function to achieve this (however, the sample code contains some minor error since VB script does not support LIKE operator).

    This is the script I've tested and you may change it slightly to suit for the real environment.

    result = GetWildFile("c:\temp", "Backup")  
    if result ="" then  
       WScript.StdOut.WriteLine "Error"  
       WScript.StdOut.WriteLine "OK"  
    end if  
    Function GetWildFile(strFolder, strWild)  
    Dim objfs  
    Dim objFolder  
    dim objFiles  
    Dim objFile  
    Dim strDesc  
    Set objfs = CreateObject("Scripting.FileSystemObject")  
    On error resume next ' Intercept No Folder'  
        Set objFolder = objFS.GetFolder(strFolder)  
        if Err.Number <> 0 then strDesc = Err.Description  
    On error goto 0  
    If Len(strDesc) = 0 then  
         Set objFiles = objFolder.Files  
         For Each objFile in ObjFiles  
             'WScript.StdOut.WriteLine objFile.Name'  
             if instr(1, objFile.Name, strWild, vbtextcompare) > 0 and instr(1, objFile.Name, ".csv", vbtextcompare) > 0 then  
                 GetWildFile = objFile.Name  
                 Exit For  
             End if  
    End if  
    End Function  


    Hope the above information helps.

    Alex Zhu

    If the response is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments