Function Reference


_WinAPI_FindFirstChangeNotification

Creates a change notification handle and sets up initial change notification filter conditions

#include <WinAPIFiles.au3>
_WinAPI_FindFirstChangeNotification ( $sDirectory, $iFlags [, $bSubtree = False] )

Parameters

$sDirectory The full path of the directory to be watched.
$iFlags The filter conditions that satisfy a change notification wait. This parameter can be one or more of the following values:
    $FILE_NOTIFY_CHANGE_FILE_NAME
    $FILE_NOTIFY_CHANGE_DIR_NAME
    $FILE_NOTIFY_CHANGE_ATTRIBUTES
    $FILE_NOTIFY_CHANGE_SIZE
    $FILE_NOTIFY_CHANGE_LAST_WRITE
    $FILE_NOTIFY_CHANGE_SECURITY
$bSubtree [optional] Specifies whether to monitor the subdirectories of the specified directory, valid values:
    True - Monitor the directory tree rooted at the specified directory.
    False - Monitor only the specified directory (Default).

Return Value

Success: A handle to a find change notification object.
Failure: 0 and sets the @error flag to non-zero.

Remarks

The _WinAPI_Wait... functions can monitor the specified directory or subtree by using the handle returned by this function.
A wait is satisfied when one of the filter conditions occurs in the monitored directory or subtree.

After the wait has been satisfied, the application can respond to this condition and continue monitoring the directory by calling the _WinAPI_FindNextChangeNotification() function and the appropriate wait function.
When the handle is no longer needed, it can be closed by using the _WinAPI_FindCloseChangeNotification() function.

Notifications may not be returned when calling _WinAPI_FindFirstChangeNotification() for a remote file system.

Related

_WinAPI_FindCloseChangeNotification, _WinAPI_FindNextChangeNotification

See Also

Search FindFirstChangeNotification in MSDN Library.

Example

#include <APIFilesConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <WinAPIProc.au3>

Opt('TrayAutoPause', 0)

Global Const $g_sPath = @TempDir & '\~TEST~'

DirCreate($g_sPath)
If Not FileExists($g_sPath) Then
        MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), 'Error', 'Unable to create folder.')
        Exit
EndIf
ShellExecute($g_sPath) ; to ease a file creation in this folder

OnAutoItExitRegister('OnAutoItExit')

Global $g_ahObj[2]
$g_ahObj[0] = _WinAPI_FindFirstChangeNotification($g_sPath, $FILE_NOTIFY_CHANGE_FILE_NAME)
$g_ahObj[1] = _WinAPI_FindFirstChangeNotification($g_sPath, $FILE_NOTIFY_CHANGE_DIR_NAME)

If (Not $g_ahObj[0]) Or (Not $g_ahObj[1]) Then
        MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), 'Error', 'Unable to create change notification.')
        Exit
EndIf

Local $tObjs = DllStructCreate('ptr;ptr')
Local $paObj = DllStructGetPtr($tObjs)
For $i = 0 To 1
        DllStructSetData($tObjs, $i + 1, $g_ahObj[$i])
Next

Local $iID
While 1
        Sleep(100)
        $iID = _WinAPI_WaitForMultipleObjects(2, $paObj, 0, 0)
        Switch $iID
                Case 0 ; WAIT_OBJECT_0
                        ConsoleWrite('A file was created, renamed, or deleted in the directory.' & @CRLF)
                Case 1 ; WAIT_OBJECT_0 + 1
                        ConsoleWrite('A directory was created, renamed, or deleted.' & @CRLF)
                Case Else
                        ContinueLoop
        EndSwitch
        If Not _WinAPI_FindNextChangeNotification($g_ahObj[$iID]) Then
                MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), 'Error', 'Unexpected error.')
                Exit
        EndIf
WEnd

Func OnAutoItExit()
        For $i = 0 To 1
                If $g_ahObj[$i] Then
                        _WinAPI_FindCloseChangeNotification($g_ahObj[$i])
                EndIf
        Next
        DirRemove($g_sPath, $DIR_REMOVE)
EndFunc   ;==>OnAutoItExit