Crée un handle de notification de changement et définit les conditions initiales de filtres de notification de changement
#include <WinAPIFiles.au3>
_WinAPI_FindFirstChangeNotification ( $sDirectory, $iFlags [, $bSubtree = False] )
$sDirectory | Le chemin complet du répertoire à surveiller. |
$iFlags | Les conditions de filtre qui répondent à une attente de notification de changement. Ce paramètre peut prendre une ou plusieurs des valeurs suivantes: $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 | [optionnel] Spécifie s'il faut surveiller les sous-répertoires du répertoire spécifié, les valeurs valides sont: True - Surveiller l'arborescence dont le répertoire racine est spécifié. False - Surveiller seulement le répertoire spécifié (par défaut). |
Succès: | Retourne le handle d'un objet notification de recherche de changement. |
Échec: | Retourne 0 et définit @error <> 0. |
Les fonctions _WinAPI_Wait... peuvent surveiller le répertoire ou le sous-arbre spécifié en utilisant le handle retourné par cette fonction.
Une attente est satisfaite lorsque l'une des conditions de filtre se produit dans le répertoire surveillé ou dans le sous-arbre.
Après qu'une attente ait été satisfaite, l'application peut répondre à cette condition et continuer à surveiller le répertoire en appelant la fonction _WinAPI_FindNextChangeNotification() et la fonction wait appropriée.
Lorsque le handle n'est plus nécessaire, il peut être fermé à l'aide de la fonction _WinAPI_FindCloseChangeNotification().
Les notifications ne peuvent être retournés lors de l'appel de _WinAPI_FindFirstChangeNotification() pour un système de fichiers distant.
_WinAPI_FindCloseChangeNotification, _WinAPI_FindNextChangeNotification
Consultez FindFirstChangeNotification dans la librairie MSDN.
#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(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Erreur', 'Impossible de créer le répertoire.') Exit EndIf 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(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Erreur', 'Impossible de créer les notifications de changement.') Exit EndIf Local $tObj = DllStructCreate('ptr; ptr ') Local $pObj = DllStructGetPtr($tObj) For $i = 0 To 1 DllStructSetData($tObj, $i + 1, $g_ahObj[$i]) Next Local $iID, $bStopLoop = False Do Sleep(100) $iID = _WinAPI_WaitForMultipleObjects(2, $pObj, 0, 0) Switch $iID Case 0 ; WAIT_OBJECT_0 ConsoleWrite('Un fichier a été créé, renommé, ou supprimé dans le répertoire.' & @CRLF) $bStopLoop = Not _WinAPI_FindNextChangeNotification($g_ahObj[$iID]) Case 1 ; WAIT_OBJECT_0 + 1 ConsoleWrite('Un répertoire a été créé, renommé ou supprimé.' & @CRLF) $bStopLoop = Not _WinAPI_FindNextChangeNotification($g_ahObj[$iID]) EndSwitch Until $bStopLoop MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Erreur', 'Erreur inattendu.') Func OnAutoItExit() For $i = 0 To 1 If $g_ahObj[$i] Then _WinAPI_FindCloseChangeNotification($g_ahObj[$i]) EndIf Next DirRemove($g_sPath, 1) EndFunc ;==>OnAutoItExit