; UDF - FileSystemMonitor по ниже указанной ссылке, изменил AZJIO
; http://www.autoitscript.com/forum/index.php?showtopic=113560&st=0
; RecFileListToArray.au3 можно взять по ниже указанной ссылке
;http://www.autoitscript.com/forum/topic/126198-recfilelisttoarray-new-version-6-mar-11/
#include "RecFileListToArray.au3"
#include <Array.au3>
Global $pDirEvents, $hDir, $pOverLapped, $tFNI, $pBuffer, $TrCh, $iBufferSize, $tOverLapped, $tBuffer, $tDirEvents, $iDirEvents, $hEvent
; $Path - папка в которой контролируются файлы (появление, удаление)
; $Path=@ScriptDir&'\test'
; $Path=@TempDir
$Path=@WindowsDir
$file = FileOpen(@ScriptDir&'\file.log',1) ; путь к файлу в котором ведётся лог
HotKeySet("{ESC}", "_Exit")
Func _Exit()
FileClose($file)
Exit
EndFunc
$aFile0 = _RecFileListToArray($Path, "*", 1, 1, 0, 2)
If @error Then Dim $aFile0[1]=[0]
_FileSysMonSetup(@WindowsDir)
While 1
$TrCh=_FileSysMonDirEventHandler()
If $TrCh Then
$aFile1 = _RecFileListToArray($Path, "*", 1, 1, 0, 2)
If @error Then
Dim $aFile1[1]
$aFile1[0]=0
EndIf
$aNew=_Compare($aFile0, $aFile1)
If $aNew[0] <> '/' Then
FileWrite($file, @CRLF&@CRLF&@YEAR&"."&@MON&"."&@MDAY&" - "&@HOUR&":"&@MIN&":"&@SEC&@CRLF& _ArrayToString($aNew, @CRLF))
EndIf
$aFile0=$aFile1
EndIf
Sleep(1000)
WEnd
Func _FileSysMonSetup($sdir = @ScriptDir)
$tBuffer = DllStructCreate("byte[4096]")
$pBuffer = DllStructGetPtr($tBuffer)
$iBufferSize = DllStructGetSize($tBuffer)
$tFNI = 0
$hDir = DllCall("kernel32.dll", "hwnd", "CreateFile", "Str", $sdir, "Int", 0x1, "Int", BitOR(0x1, 0x4, 0x2), "ptr", 0, "int", 0x3, "int", BitOR(0x2000000, 0x40000000), "int", 0)
$hDir = $hDir[0]
$tReadLen = DllStructCreate("dword ReadLen")
$tOverLapped = DllStructCreate("Uint OL1;Uint OL2; Uint OL3; Uint OL4; hwnd OL5")
For $i = 1 To 5
DllStructSetData($tOverLapped, $i, 0)
Next
$pOverLapped = DllStructGetPtr($tOverLapped)
$iOverLappedSize = DllStructGetSize($tOverLapped)
$tDirEvents = DllStructCreate("hwnd DirEvents")
$pDirEvents = DllStructGetPtr($tDirEvents)
$iDirEvents = DllStructGetSize($tDirEvents)
$hEvent = DllCall("kernel32.dll", "hwnd", "CreateEvent", "UInt", 0, "Int", True, "Int", False, "UInt", 0)
DllStructSetData($tOverLapped, 5, $hEvent[0])
DllStructSetData($tDirEvents, 1, $hEvent[0])
$ret = DllCall("kernel32.dll", "Int", "ReadDirectoryChangesW", "hwnd", $hDir, "ptr", $pBuffer, "dword", $iBufferSize, "int", False, "dword", BitOR(0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x100), "Uint", 0, "Uint", $pOverLapped, "Uint", 0)
Return True
EndFunc ;==>_FileSysMonSetup
Func _FileSysMonDirEventHandler()
Local $ret, $r
$r = DllCall("User32.dll", "dword", "MsgWaitForMultipleObjectsEx", "dword", 1, "ptr", $pDirEvents, "dword", 100, "dword", 0x4FF, "dword", 0x6)
If $r[0] = 0 Then
$ret = DllCall("kernel32.dll", "Int", "ReadDirectoryChangesW", "hwnd", $hDir, "ptr", $pBuffer, "dword", $iBufferSize, "int", False, "dword", BitOR(0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x100), "Uint", 0, "Uint", $pOverLapped, "Uint", 0)
If $ret[0] = 1 Then Return True
EndIf
Return False
EndFunc ;==>_FileSysMonDirEventHandler
Func _Compare($aFile0, $aFile1)
Local $k, $m, $i
Assign('/', 2, 1)
If $aFile0[0]<>0 Then
For $i = 1 To $aFile0[0]
Assign($aFile0[$i]&'/', Eval($aFile0[$i]&'/')+1, 1)
Next
EndIf
; проверка добавленных файлов
$m=0
If $aFile1[0]<>0 Then
For $i = 1 To $aFile1[0]
Assign($aFile1[$i]&'/', Eval($aFile1[$i]&'/')+1, 1)
If Eval($aFile1[$i]&'/') = 1 Then
$aFile1[$m]='+ '&$aFile1[$i]
$m+=1
EndIf
Next
EndIf
If $m <> 0 Then ReDim $aFile1[$m]
; проверка удалённых файлов
$k=0
If $aFile0[0]<>0 Then
For $i = 1 To $aFile0[0]
If Eval($aFile0[$i]&'/') = 1 Then
$aFile0[$k]='- '&$aFile0[$i]
$k+=1
EndIf
Next
EndIf
If $k <> 0 Then ReDim $aFile0[$k]
; объединение результата
Select
Case $k = 0 And $m = 0
ReDim $aFile1[1]
$aFile1[0]='/'
Case $k<>0 And $m<>0
_ArrayConcatenate($aFile1, $aFile0)
Case $k<>0 And $m=0
$aFile1=$aFile0
EndSelect
Return $aFile1
EndFunc