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”?
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
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”?
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.
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
Hope the above information helps.
Alex Zhu
If the response is helpful, please click "Accept Answer" and upvote it.