3

誰かが私が間違っているところを助けてくれますか?このスクリプトは、作成、削除、または変更されたテキストファイルのフォルダを監視するためのものです

sPath = "C:\scripts\test"
sComputer = "."
sDrive = split(sPath,":")(0)
sFolders1 = split(sPath,":")(1)
sFolders = REPLACE(sFolders1, "\", "\\") & "\\"

Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " _
& "TargetInstance ISA 'CIM_DataFile' AND " _
& "TargetInstance.Drive='" & sDrive & "' AND " _
& "TargetInstance.Path='" & sFolders & "' AND " _
& "TargetInstance.Extension = 'txt' ")

Wscript.Echo vbCrlf & Now & vbTab & _
"Begin Monitoring for a Folder " & sDrive & ":" & sFolders1 & " Change Event..." & vbCrlf

Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Select Case objLatestEvent.Path_.Class

    Case "__InstanceCreationEvent"
    WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName & "." & objLatestEvent.TargetInstance.Extension _
    & " was created" & vbCrlf

    Case "__InstanceDeletionEvent"
    WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName & "." & objLatestEvent.TargetInstance.Extension _
    & " was deleted" & vbCrlf

    Case "__InstanceModificationEvent"
    If objLatestEvent.TargetInstance.LastModified <> _
    objLatestEvent.PreviousInstance.LastModified then
    WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName & "." & objLatestEvent.TargetInstance.Extension _
    & " was modified" & vbCrlf
    End If
    End Select
Loop

Set objWMIService = nothing
Set colMonitoredEvents = nothing
Set objLatestEvent = nothing

このスクリプトは、私が書くときに完璧に実行されます

sPath = "\\ComputerName\C$\scripts\test"

sPath = "C:\scripts\test"

ありがとうございました....

4

1 に答える 1

4

グーグルで検索する"WMI TargetInstance.Drive"と、ドライブ文字にコロンが必要であることがわかります。次のようなクエリ

SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'CIM_DataFile' AND TargetInstance.Drive='E:' AND TargetInstance.Path='\\trials\\SoTrials\\answers\\10041057\\data\\' AND TargetInstance.Extension = 'txt'

期待どおりに動作します。

于 2012-04-06T21:36:20.163 に答える