I have a script that will monitor a directory and act on any new file created. The problem is it's an ftp directory and if the file is big, the script tries to process the file before the upload is complete. I figure I need to use case "__InstanceModificationEvent" and check i't size untill it stop growing. I just don't know how; Any help would be appreciated. code block
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colMonitoredEvents = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceOperationEvent WITHIN 2 WHERE " _ & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _ & "TargetInstance.GroupComponent= " _ & "'Win32_Directory.Name=""D:\\\\FTPRoot\\\\FTP\\\\jpmcSSLClient""'")
Do While TRUE Set objEventObject = colMonitoredEvents.NextEvent()
Select Case objEventObject.Path_.Class
Case "__InstanceCreationEvent" Set objFSO = CreateObject("Scripting.FileSystemObject") strReturned = objEventObject.TargetInstance.PartComponent strFilePath = Split(strReturned, "CIM_DataFile.Name=")(1) strFilePath = Replace(strFilePath, """", "") strFilePath = Replace(strFilePath, "\\", "\") Set objFile = objFSO.GetFile(strFilePath) objLogFile.WriteLine(strFilePath) objLogFile.WriteLine( "A new file was just created: " & _ objEventObject.TargetInstance.PartComponent ) objLogFile.WriteLine("A new file was just created: " & objFile.Name) strFile = objFile.Name Call Main() Case "__InstanceDeletionEvent" objLogFile.WriteLine " File deleted " & Now End Select Loop Sub Main () /code block
I have changed my code and now I have 2 questions. 11. how do I insert a vairable in & "TargetInstance.Name='D:\\FTPRoot\\FTP\\jpmcSSLClient\\test.txt'") for the file everything I try reults in unparable query. 2.my if statement will never work, so how do you find when the file is max size? If objLatestEvent.TargetInstance.FileSize = objLatestEvent.PreviousInstance.FileSize Then
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colMonitoredEvents = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceOperationEvent WITHIN 2 WHERE " _ & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _ & "TargetInstance.GroupComponent= " _ & "'Win32_Directory.Name=""D:\\\\FTPRoot\\\\FTP\\\\jpmcSSLClient""'")
Do While TRUE Set objEventObject = colMonitoredEvents.NextEvent()
Select Case objEventObject.Path_.Class
Case "__InstanceCreationEvent" Set objFSO = CreateObject("Scripting.FileSystemObject") strReturned = objEventObject.TargetInstance.PartComponent strFilePath = Split(strReturned, "CIM_DataFile.Name=")(1) strFilePath = Replace(strFilePath, """", "") strFilePath = Replace(strFilePath, "\\", "\") Set objFile = objFSO.GetFile(strFilePath) objLogFile.WriteLine(strFilePath) objLogFile.WriteLine( "A new file was just created: " & _ objEventObject.TargetInstance.PartComponent ) objLogFile.WriteLine("A new file was just created: " & objFile.Name) strFile = objFile.Name Call Mon() Case "__InstanceDeletionEvent" objLogFile.WriteLine " File deleted " & Now End Select Loop Sub mon () strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2") Set colMonitoredEvents = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceModificationEvent WITHIN 2 WHERE " _ & "TargetInstance ISA 'CIM_DataFile' and " _ & "TargetInstance.Name='D:\\FTPRoot\\FTP\\jpmcSSLClient\\test.txt'") Do Set objLatestEvent = colMonitoredEvents.NextEvent objLogFile.WriteLine "File: " & objLatestEvent.TargetInstance.Name objLogFile.WriteLine "New size: " & objLatestEvent.TargetInstance.FileSize objLogFile.WriteLine "Old size: " & objLatestEvent.PreviousInstance.FileSize If objLatestEvent.TargetInstance.FileSize = objLatestEvent.PreviousInstance.FileSize Then Call main () End If Loop End Sub Sub Main ()