如何在VBS中的WMI服务查询中使用变量名

How to use variable name in WMI service query in VBS

如何将当前年份放入 CURRENT_YEAR 以便我监视的文件夹是 P:\\Folder1\\Folder2\\Folder3\\2020

我在 vbs 文件中有以下代码:

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\.\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE _
Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""P:\\Folder1\\Folder2\\Folder3\\CURRENT_YEAR""'")
Do
'code to execute if file in folder found
Loop

在 vbscript 中,您可以这样获取年份:


CURRENT_YEAR=Year(Now)
MsgBox CURRENT_YEAR
MsgBox "P:\\Folder1\\Folder2\\Folder3\\"& CURRENT_YEAR &""

CURRENT_YEAR=Year(Now)
sPath = "F:\Folder1\Folder2\Folder3\"& CURRENT_YEAR
sDrive = split(sPath,":")(0)
sFolders1 = split(sPath,":")(1)
sFolders = REPLACE(sFolders1, "\", "\\")
TargetFolder = DblQuote(sDrive & ":" & sFolders)

wscript.echo sPath
wscript.echo TargetFolder

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & _
    strComputer & "\root\cimv2")

Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceCreationEvent Within 2 Where " _
    & "Targetinstance Isa 'CIM_DirectoryContainsFile' and " _
    & "TargetInstance.GroupComponent= " _
    & "'Win32_Directory.Name="& TargetFolder &"'")

' The loop
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop
'-------------------------------------------------------------
Function DblQuote(str)
    DblQuote = chr(34) & str & chr(34)
End Function
'-------------------------------------------------------------