How to add a wildcard on a VB Script

javad 1 Reputation point
2020-12-05T08:37:10.203+00:00

Hi,

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)
Else
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,441 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,644 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 114.2K Reputation points
    2020-12-05T09:56:52.21+00:00

    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
    2020-12-05T10:22:56.353+00:00

    It's because
    strFile = "D:\Scripts\Backup*.csv"
    ...is 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

    and
    vbscript-to-move-file-with-wildcard-if-it-exists

    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,626 Reputation points Microsoft Vendor
    2020-12-07T04:06:59.127+00:00

    Hi,

    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"  
    else  
       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  
         Next  
    End if  
    End Function  
    

    45508-vbscript-result.png

    Hope the above information helps.

    Alex Zhu


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

    0 comments No comments