如何使用AutoHotKey对文件夹中创建的新文件进行操作?

时间:2017-08-28 11:00:11

标签: autohotkey

我想让AutoHotKey在磁盘上监听/观看一个文件夹(例如c:\ folder),查看在那里创建的任何新文件。如果新文件显示为up,则脚本应执行一组命令:

Send {Esc} Sleep, 100 Send {F5} Sleep, 100 Send {Home} Sleep, 100 Send s Sleep, 100 Send {Enter} Return

之后,它应该返回查看文件夹以进行新的更改,依此类推。我意识到它应该使用WatchDirectory()功能,但不知道如何实现它。有帮助吗? THX!

2 个答案:

答案 0 :(得分:1)

您可以使用WatchFolder()功能,将其下载到与脚本相同的位置,然后您可以执行以下操作:

#Include %A_ScriptDir%\WatchFolder.ahk

WatchFolder("c:\folder", "myFunc", , Watch := 1)
return

myFunc(path, changes) {
    for k, change in changes
        ; 1 means new file was added
        if (change.action = 1) {
            gosub doStuff
            return
        }
}

doStuff:
; commands you want to execute
return

答案 1 :(得分:0)

感谢您的回答。我添加了你的代码并且它不想工作,脚本似乎没有加载(任务栏中没有图标)。我的最终代码如下所示:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


#Include %A_ScriptDir%\WatchFolder.ahk

WatchFolder("C:\Users\kris\Desktop\temp", "myFunc", , Watch := 1)
return

myFunc(path, changes) {
for k, change in changes
    ; 1 means new file was added
    if (change.action = 1) {
        gosub doStuff
        return
    }
}

doStuff:
  Send {Esc}
  Sleep, 500
  Send {F5}
  Sleep, 100
  Send {Home}
  Sleep, 100
  Send s
  Sleep, 100
  Send {Enter}
return

我已将WatchFolder.ahk插入与脚本相同的文件夹中。