Creates a change notification handle and sets up initial change notification filter conditions.
#Include <WinAPIEx.au3>
_WinAPI_FindFirstChangeNotification ( $sDirectory, $iFlags [, $fSubtree] )
$sDirectory | The full path of the directory to be watched. |
$iFilter | 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 |
$fSubtree | [可选参数] 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) |
Success | A handle to a find change notification object. |
失败: | 返回 0 并设置 @error 标志为非 0 值. |
在MSDN中搜索
#Include <APIConstants.au3>
#Include <WinAPIEx.au3>
Opt('MustDeclareVars', 1)
Opt('TrayAutoPause', 0)
Global Const $sPath = @ScriptDir & '\~TEST~'
Global $hObj[2], $tObj, $pObj, $ID
DirCreate($sPath)
If Not FileExists($sPath) Then
MsgBox(16, 'Error', 'Unable to create folder.')
Exit
EndIf
OnAutoItExitRegister('OnAutoItExit')
$hObj[0] = _WinAPI_FindFirstChangeNotification($sPath, $FILE_NOTIFY_CHANGE_FILE_NAME)
$hObj[1] = _WinAPI_FindFirstChangeNotification($sPath, $FILE_NOTIFY_CHANGE_DIR_NAME)
If (Not $hObj[0]) Or (Not $hObj[1]) Then
MsgBox(16, 'Error', 'Unable to create change notification.')
Exit
EndIf
$tObj = DllStructCreate('ptr;ptr')
$pObj = DllStructGetPtr($tObj)
For $i = 0 To 1
DllStructSetData($tObj, $i + 1, $hObj[$i])
Next
While 1
Sleep(100)
$ID = _WinAPI_WaitForMultipleObjects(2, $pObj, 0, 0)
Switch $ID
Case 0 ; WAIT_OBJECT_0
ConsoleWrite('A file was created, renamed, or deleted in the directory.' & @CR)
Case 1 ; WAIT_OBJECT_0 + 1
ConsoleWrite('A directory was created, renamed, or deleted.' & @CR)
Case Else
ContinueLoop
EndSwitch
If Not _WinAPI_FindNextChangeNotification($hObj[$ID]) Then
MsgBox(16, 'Error', 'Unexpected error.')
Exit
EndIf
WEnd
Func OnAutoItExit()
For $i = 0 To 1
If $hObj[$i] Then
_WinAPI_FindCloseChangeNotification($hObj[$i])
EndIf
Next
; DirRemove($sPath)
EndFunc ;==>OnAutoItExit