Hello,
I have two pieces of code put together... one for file change notification and the other to write the changes to a text file. How can I modify this to tell me in the output text file which operation it was? Changed, Deleted, Created. Now it places the path and file and then places the date and time.
Thanks for any help!
I have two pieces of code put together... one for file change notification and the other to write the changes to a text file. How can I modify this to tell me in the output text file which operation it was? Changed, Deleted, Created. Now it places the path and file and then places the date and time.
Thanks for any help!
Code:
strComputer = "."
strDrive = "C:"
strFolder = "\\temp\\"
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile, strText
strDirectory = "c:\Log Folder"
strFile = "\Change Log.txt"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process where name = 'wscript.exe' or name = 'cscript.exe'")
If colProcess.Count > 1 Then
' --- a wscript or cscript process is already running
WScript.quit
End If
' Create the event sink object that receives the events
Set objSink = wscript.CreateObject("WbemScripting.SWbemSink", "SINK_")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.ExecNotificationQueryAsync objSink, "SELECT * FROM __InstanceCreationEvent WITHIN 3 " & _
"Where Targetinstance Isa 'CIM_DataFile' And " & _
"TargetInstance.Drive = '" & strDrive & "' And " & _
"TargetInstance.Path = '" & strFolder & "'"
objWMIService.ExecNotificationQueryAsync objSink, "SELECT * FROM __InstanceModificationEvent WITHIN 3 " & _
"Where Targetinstance Isa 'CIM_DataFile' And " & _
"TargetInstance.Drive = '" & strDrive & "' And " & _
"TargetInstance.Path = '" & strFolder & "'"
objWMIService.ExecNotificationQueryAsync objSink, "SELECT * FROM __InstanceDeletionEvent WITHIN 3 " & _
"Where Targetinstance Isa 'CIM_DataFile' And " & _
"TargetInstance.Drive = '" & strDrive & "' And " & _
"TargetInstance.Path = '" & strFolder & "'"
Done = False
Do While True ' We'll end after first event for this example
wscript.sleep 1000
Loop
Sub SINK_OnObjectReady(wmiObject, wmiAsyncContext)
strText = wmiObject.TargetInstance.Name
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = nothing
set objFolder = nothing
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)
' Writes strText every time you run this VBScript
objTextFile.WriteLine(strText) & " " & Now
objTextFile.Close
wscript.echo wmiObject.TargetInstance.Name
End Sub