Jump to content

Help with with COM script


johnmcloud
 Share

Recommended Posts

Hi guys, i have found on the forum this script:

$strComputer = "."
$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootcimv2")
$colMonitoredEvents = $objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE " _
     & "Targetinstance ISA 'CIM_DataFile' and " _
             & "TargetInstance.Path = '1' and " _
             & " TargetInstance.Drive = 'C:' ")
While 1
     $objEventObject = $colMonitoredEvents.NextEvent()
Select
     Case $objEventObject.Path_.Class()="__InstanceCreationEvent"
         ConsoleWrite ("A new file was just created: " & $objEventObject.TargetInstance.Properties_.Item("Name").Value & @CR)
;~       Case $objEventObject.Path_.Class()="__InstanceDeletionEvent"
;~       ConsoleWrite ("A file was just deleted: " & $objEventObject.TargetInstance.Properties_.Item("Name").Value & @CR)
;~       Case $objEventObject.Path_.Class()="__InstanceModificationEvent"
;~       ConsoleWrite ("A file was just modified: " & $objEventObject.TargetInstance.Properties_.Item("Name").Value & @CR)
EndSelect
WEnd

I need to monitoring a folder for new created files, i don't know if this way is the best.

Anyway i have three question:

1) Can't exit, i need to add an HotKeySet and exit + close wmiprvse process?

2) How to add a custom folder? Now the folder is C:1, but I'd like to make a string like:

$Folder = "D:TestMyFolder"

3) There is another way for check it out new file created in a folder with Autoit?

Thanks for suggestion ;)

Edited by johnmcloud
Link to comment
Share on other sites

There are some filesystem monitoring scripts available on the forum like written by ptrex.

Search the forum for "filesystem monitor" and you will find a lot of hits.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Hi water,

Thanks for the answer, but you have posted the same script i have posted on first post :D

I don't need something complex, just an ConsoleWrite when you add a new file in a specific folder. I have searched before doing this, but i have found only 10 result, and that was the only work for me but i have some problem on customization/close correctly.

Another cool example is but work as opposite i need...if you delete a file from that folder you have the ConsoleWrite.

So, if someone has some idea how to doing my objective or improve the first post please feel free to post here an example script ;)

Edited by johnmcloud
Link to comment
Share on other sites

Sorry, didn't check the content. But as I have done now the two scripts are not 100% identical.

If I take your script checking for a variable should look like this:

$sDrive = "D:"
$sPath = "TestMyFolder"
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootcimv2")
$colMonitoredEvents = $objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE " _
& "Targetinstance ISA 'CIM_DataFile' and " _
& "TargetInstance.Path = '" & $sPatch & "' and " _
& " TargetInstance.Drive = '" & $sDrive & "' ")
While 1
$objEventObject = $colMonitoredEvents.NextEvent()
Select
Case $objEventObject.Path_.Class()="__InstanceCreationEvent"
ConsoleWrite ("A new file was just created: " & $objEventObject.TargetInstance.Properties_.Item("Name").Value & @CR)
;~ Case $objEventObject.Path_.Class()="__InstanceDeletionEvent"
;~ ConsoleWrite ("A file was just deleted: " & $objEventObject.TargetInstance.Properties_.Item("Name").Value & @CR)
;~ Case $objEventObject.Path_.Class()="__InstanceModificationEvent"
;~ ConsoleWrite ("A file was just modified: " & $objEventObject.TargetInstance.Properties_.Item("Name").Value & @CR)
EndSelect
WEnd

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thanks for help, you have make a little mistake in your script ( $sPatch ), i have little edited for add easily the path:

HotKeySet("{ESC}", "_Exit")

$rPath = @WorkingDir
$sDrive = StringLeft($rPath, 2)
$sPath = StringTrimLeft(StringReplace($rPath, "", ""),2) & ""

$strComputer = "."
$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootcimv2")
$colMonitoredEvents = $objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE " _
& "Targetinstance ISA 'CIM_DataFile' and " _
& "TargetInstance.Path = '" & $sPath & "' and " _
& " TargetInstance.Drive = '" & $sDrive & "' ")
Static $iCount = 0
While 1
$objEventObject = $colMonitoredEvents.NextEvent()
Select
Case $objEventObject.Path_.Class() = "__InstanceCreationEvent"
$iCount += 1
ConsoleWrite($iCount & " - " & $objEventObject.TargetInstance.Properties_.Item("Name").Value & @CR)
;~ Case $objEventObject.Path_.Class()="__InstanceDeletionEvent"
;~ ConsoleWrite ("A file was just deleted: " & $objEventObject.TargetInstance.Properties_.Item("Name").Value & @CR)
;~ Case $objEventObject.Path_.Class()="__InstanceModificationEvent"
;~ ConsoleWrite ("A file was just modified: " & $objEventObject.TargetInstance.Properties_.Item("Name").Value & @CR)
EndSelect
WEnd

Func _Exit()
Exit 0
EndFunc

Don't exit properly when i add a new file:

>Process failed to respond; forcing abrupt termination...
>Exit code: 1 Time: 12.831

How to close this script?

Edited by johnmcloud
Link to comment
Share on other sites

You press "Exit" and then you get the "Process failed to respond; forcing abrupt termination..." message?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Yes, i think that script has some problem...searching i have found another, much better:

#include <FileSystemMonitor.au3>
 
HotKeySet("{Esc}", "_Exit")
 
_FileSysMonSetup(3, @WorkingDir, "")
Static $iCount = 0
 
While 1
_FileSysMonDirEventHandler()
WEnd
 
Func _FileSysMonActionEvent($event_type, $event_id, $event_value)
Switch $event_type
Case 0
Switch $event_id
    Case 0x00000001 ; file / folder added
     $iCount += 1
     ConsoleWrite($iCount & " - " & $event_value & @CRLF)
EndSwitch
EndSwitch
EndFunc ;==>_FileSysMonActionEvent
 
Func _Exit()
Exit 0
EndFunc ;==>_Exit

From i have not found it in the search...

Anyway, i have a last question.

I have add a progressive number to every file like:

1 - C:Test.exe
2 - C:Test.txt
3 - C:bala.rtf
etc.

Now i want to add an action to every number of the file, so i want to read from a txt the corresponding line, so if file is 1 read line 1, file 2 read line 2 etc...

How to make this?

Thanks

Edited by johnmcloud
Link to comment
Share on other sites

Use FileOpen / FileReadLine / FileClose. FileReadLine lets you specify the linenumber. Use FileOpen to get a handle for performance reasons.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I have do this:

Local $FileList = FileOpen("List.txt", 2)

Func _FileSysMonActionEvent($event_type, $event_id, $event_value)
 Switch $event_type
  Case 0
   Switch $event_id
    Case 0x00000001 ; file / folder added
     $iCount += 1
;~   ConsoleWrite($iCount & " - " & $event_value & @CRLF)
     FileWrite($FileList, $iCount & " - " & $event_value & @CRLF)
     MsgBox(0,0, FileReadLine($FileList, $iCount))
;~   FileClose($FileList) ; If i put here fileclose i see only one value
   EndSwitch
 EndSwitch
EndFunc   ;==>_FileSysMonActionEvent

But i can't see nothing in the MsgBox, what is my error?

Edited by johnmcloud
Link to comment
Share on other sites

First close the file, then read it. The line you have written might still be in the buffer.

But I wouldn't use this in production because it slows down your script. Open the file at script startup, close it at the end.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Hope you get it working.

If you have further questions we will be happy to assist.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...