AutoHotkey监视文件夹内文件变更

监视目录中文件的变动事件移动、重命名、创建。

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

WatchFolder  := "E:\desk\rrsyycm"  ;"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

为TA充电
共{{data.count}}人
人已赞赏
AHKV1学习

[教程]AutoHotkey字符串索引窗口标题激活窗口

2016-8-9 13:27:44

AHKV1办公

AutoHotkey内置函数解压文件

2016-8-9 13:33:18

2 条回复 A文章作者 M管理员
  1. 飞虎

    不错不错,👍

  2. Illidan

    不错不错

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索