Jump to content

Recommended Posts

Posted

Hi there, I was wondering if there is a special event that is thrown when the contents of a folder change so that my script can preform an action every time this happens?

I want to have a small portion of my script run in the taskbar 24/7 and monitor a certain folder, if files are added or taken away or renamed it should update my database that stores all of these files. If there is no event, what is the best way to accomplish this? Just do a dir on the folder every so often?

Thanks in advance

[font="Impact"] I always thought dogs laid eggs, and I learned something today. [/font]
Posted

You can use WMI with __InstanceCreationEvent.

Here a sample that based on #337058

;~ WMI monitoring
Dim $strQuery, $SINK, $objContext, $objWMIService

Dim $Eventtime= 2

;Create Object 
Sink_Start($sink)

;~ Set Sink
_find_folders("C:\Temp")

func _find_folders($path,$subtree=False)
    $monitordrive  = stringleft($path,2)
    $monitorpath = StringReplace(stringtrimleft($path,2),"\","\\") & "\\"
    SINK_Add($monitordrive,$monitorpath,$sink)
    ConsoleWrite($path & @crlf)
    
    if $subtree=false then Return
    local $first=FileFindFirstFile($path & "\*.*")
    while 1
        $fichier = FileFindNextFile($first)
        if @error then ExitLoop
        if stringinstr(FileGetAttrib($path & "\" & $fichier),"D") then _find_folders($path & "\" & $fichier)
    WEnd        
EndFunc 

While 1
    Sleep(200)
WEnd

func Sink_Start(byref $Sink)
    $SINK = ObjCreate("WbemScripting.SWbemSink")
    ObjEvent($SINK, "SINK_")
EndFunc

func SINK_Add($wmidrive,$wmipath,$sinkref)
    $objContext = ObjCreate("WbemScripting.SWbemNamedValueSet")
    $strQuery = "SELECT * FROM __InstanceOperationEvent WITHIN " & $Eventtime &  " WHERE TargetInstance ISA 'CIM_DataFile' and TargetInstance.Path = '" & $wmipath & "' and TargetInstance.drive = '" & $wmidrive & "'"
    $objWMIService = ObjGet("winmgmts:!\\.\root\cimv2")
    $objWMIService.ExecNotificationQueryAsync ($sinkref,$strQuery, Default, Default, Default, $objContext)
EndFunc

;******************************************************************************
Func SINK_OnObjectReady ($objLatestEvent, $objAsyncContext)   ;Trap asynchronous events.
   ;$objAsyncContextItem = $objAsyncContext.Item ("hostname")
    $filename= $objLatestEvent.TargetInstance.Properties_.item("Name").value
    ConsoleWrite($filename & @CRLF)
EndFunc  ;==>SINK_OnObjectReady
Posted

Hi there, I was wondering if there is a special event that is thrown when the contents of a folder change so that my script can preform an action every time this happens?

I want to have a small portion of my script run in the taskbar 24/7 and monitor a certain folder, if files are added or taken away or renamed it should update my database that stores all of these files. If there is no event, what is the best way to accomplish this? Just do a dir on the folder every so often?

Thanks in advance

@ame1011

have a look at this folder monitoring script

a (broken) monitor file changes script which uses ReadDirectoryChangeW

http://www.autoitscript.com/forum/index.php?showtopic=69044

I see fascists...

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
  • Recently Browsing   0 members

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