[ahk]检测文件夹内文件的更新

更新文件会提醒
[ahk]检测文件夹内文件的更新_第1张图片
源代码

#Persistent
SetBatchLines, -1
Process, Priority,, High
OnExit, ShutApp

WatchFolder  := "I:\ahk\MIM_Screenshot"  ;"C:\"
WatchSubDirs := "1"

EventString := "New File,Deleted,Modified,Renamed From,Renamed To"
StringSplit, EventArray, EventString, `,

DllCall("shlwapi\PathAddBackslashA", UInt, &Watchfolder)

CBA_ReadDir := RegisterCallback("ReadDirectoryChanges")

SizeOf_FNI := ( 64KB := 1024 * 64 )
VarSetCapacity( FILE_NOTIFY_INFORMATION, SizeOf_FNI, 0 )
PointerFNI := &FILE_NOTIFY_INFORMATION

hDir := DllCall( "CreateFile", Str  , WatchFolder, UInt, "1", UInt , "7", UInt, 0, UInt, "3", UInt, "1107296256", UInt , 0 )

Loop
{
   nReadLen  := 0
   hThreadId := 0

   hThread   := DllCall( "CreateThread", UInt, 0, UInt, 0, UInt, CBA_ReadDir, UInt, 0, UInt,0, UIntP,hThreadId )

   Loop
   {
      If nReadLen
      {
         PointerFNI := &FILE_NOTIFY_INFORMATION

         Loop
         {
            NextEntry   := NumGet( PointerFNI + 0  )
            Action      := NumGet( PointerFNI + 4  )
            FileNameLen := NumGet( PointerFNI + 8  )
            FileNamePtr :=       ( PointerFNI + 12 )

            Event := EventArray%Action%

            VarSetCapacity( FileNameANSI, FileNameLen )
            DllCall( "WideCharToMultiByte", UInt,0, UInt,0, UInt,FileNamePtr, UInt, FileNameLen,  Str, FileNameANSI, UInt,FileNameLen, UInt,0, UInt,0 )

            File := SubStr( FileNameANSI, 1, FileNameLen/2 )
            FullPath := WatchFolder . File
            FileGetAttrib, Attr, %FullPath%
 FormatTime, Time , %A_Now%, HH:mm:ss

            If !FileExist(FullPath)
            Tooltip, %Time%`n%Event%`n%File%
            Else
            {
               Loop %FullPath%
               {
 FormatTime, TStamp, %A_LoopFileTimeModified%, yyyy-MM-dd HH:mm:ss
                  Tooltip, %Time%`n%Event%`n%File%`n%A_LoopFileSizeKB%`n%TStamp%`n%A_LoopFileAttrib%
               }
            }

            If !NextEntry
            Break
            Else
            PointerFNI += NextEntry
         }
         Break
      }

      Sleep 100
   }

   DllCall( "TerminateThread", UInt,hThread, UInt,0 )
   DllCall( "CloseHandle", UInt,hThread )
}
Return

ReadDirectoryChanges()
{
   Global hDir,PointerFNI, Sizeof_FNI, WatchSubdirs, nReadlen
   Return DllCall( "ReadDirectoryChangesW", UInt, hDir, UInt, PointerFNI, UInt, SizeOf_FNI, UInt, WatchSubDirs, UInt, "375", UIntP, nReadLen, UInt, 0, UInt, 0 )
}

GuiClose:
ShutApp:
DllCall( "CloseHandle", UInt,hDir )
DllCall( "TerminateThread", UInt,hThread, UInt,0 )
DllCall( "CloseHandle", UInt,hThread )
ExitApp
Return

你可能感兴趣的:(监控,文件夹,AHK)