Jump to content
Sign in to follow this  
ame1011

Monitering a Folder

Recommended Posts

ame1011

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]

Share this post


Link to post
Share on other sites
Valuater

Just place something like this ion a loop

$timer    = TimerInit()
$size    = DirGetSize(@ScriptDir,1)
$diff    = Round(TimerDiff($timer) / 1000)    ; time in seconds
If IsArray($size) Then
    Msgbox(0,"DirGetSize-Info","Size(Bytes):" & $size[0] & @LF _
        & "Files:" & $size[1] & @LF & "Dirs:" & $size[2] & @LF _
        & "TimeDiff(Sec):" & $diff)
EndIf

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
Paulchen

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

Share this post


Link to post
Share on other sites
rover

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...

Share this post


Link to post
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
Sign in to follow this  

×