question

javadpaasha-3413 avatar image
0 Votes"
javadpaasha-3413 asked AlexZhu-MSFT edited

How to add a wildcard on a VB Script

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

dotnet-visual-basicmsc-operations-manager
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

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”?


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Jeff-2958 avatar image
0 Votes"
Jeff-2958 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlexZhu-MSFT avatar image
0 Votes"
AlexZhu-MSFT answered AlexZhu-MSFT edited

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.


[1]: /answers/storage/attachments/45525-vbscript-source-code.png



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.