函数参考


_WinAPI_ReadDirectoryChanges

Retrieves information that describes the changes within the specified directory.

#Include <WinAPIEx.au3>
_WinAPI_ReadDirectoryChanges ( $hDirectory, $iFilter, $pBuffer, $iLength [, $fSubtree] )

参数

$hDirectory A handle to the directory to be monitored. This directory must be opened with the
$FILE_LIST_DIRECTORY access right.
$iFilter The filter criteria that the function checks to determine if the wait operation has completed.
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_LAST_ACCESS
$FILE_NOTIFY_CHANGE_CREATION
$FILE_NOTIFY_CHANGE_SECURITY
$pBuffer A pointer to the DWORD-aligned formatted buffer that internally used by this function to retrieve the
data. To create a buffer, you can use _WinAPI_CreateBuffer() function. To prevent the crash of the script,
use the buffer at least not less than 64 KB. If the buffer is greater than 64 KB and the application is
monitoring a directory over the network, the function fails. This is due to a packet size limitation
with the underlying file sharing protocols.
$iLength The size of the buffer, in bytes.
$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 The 2D array containing the following information:
[0][0] Number of rows in array (n)
[0][1] Unused
[n][0] The file name relative to the directory handle.
[n][1] The type of change that has occurred (one of the $FILE_ACTION_* constants).
失败: 返回 0 并设置 @error 标志为非 0 值.

注意/说明

When you first call _WinAPI_ReadDirectoryChanges() function, the system allocates a buffer to store change
information. This buffer is associated with the directory handle until it is closed and its size does not change
during its lifetime. Directory changes that occur between calls to this function are added to the buffer
and then returned with the next call. If the buffer overflows, the entire contents are discarded.

To obtain a handle to a directory, use _WinAPI_CreateFileEx() function with $FILE_FLAG_BACKUP_SEMANTICS flag.

The _WinAPI_ReadDirectoryChanges() function works only in synchronous mode.

相关

详情参考

在MSDN中搜索


示例/演示


#Include <APIConstants.au3>
#Include <Array.au3>
#Include <WinAPIEx.au3>

Opt('MustDeclareVars', 1)

Global Const $sPath = @DesktopDir & '\~TEST~'

Global $hDirectory, $pBuffer, $aData

DirCreate($sPath)
If Not FileExists($sPath) Then
    MsgBox(16, 'Error', 'Unable to create folder.')
    Exit
EndIf

$hDirectory = _WinAPI_CreateFileEx($sPath, $OPEN_EXISTING, $FILE_LIST_DIRECTORY, BitOR($FILE_SHARE_READ, $FILE_SHARE_WRITE), $FILE_FLAG_BACKUP_SEMANTICS)
If @error Then
    _WinAPI_ShowLastError('', 1)
EndIf

$pBuffer = _WinAPI_CreateBuffer(8388608)

While 1
    $aData = _WinAPI_ReadDirectoryChanges($hDirectory, BitOR($FILE_NOTIFY_CHANGE_FILE_NAME, $FILE_NOTIFY_CHANGE_DIR_NAME), $pBuffer, 8388608, 1)
    If Not @error Then
        _ArrayDisplay($aData, '_WinAPI_ReadDirectoryChanges')
    Else
        _WinAPI_ShowLastError('', 1)
    EndIf
WEnd