当文件开始存在时如何使用 autohotkey 对文件进行操作?

how to act on a file when it begins to exist with autohotkey?

我有以下代码,据我所知应该可以工作。目标是不断检查 Z:\auto_run.txt 是否存在。一旦存在,文件的每一行(作为文件的路径)都应该在记事本++中打开。最后,删除 Z:\auto_run.txt.

我终于可以独立工作了。问题是如何不断检查文件是否存在?当我 运行 标准 Autohotkey.ahk 中的以下代码时,它似乎不起作用,即使文件存在,也没有任何反应。

IfExist, Z:\auto_run.txt
{
    Loop, read, Z:\auto_run.txt
    {
        IfExist, Z:\%A_LoopReadLine%
            Run, C:\Program Files (x86)\Notepad++\notepad++.exe Z:\%A_LoopReadLine%
    }
    FileDelete, Z:\autohotkey\auto_run.txt
}

将整个事情放在一个循环中行得通吗?

Loop
{
    IfExist, Z:\auto_run.txt
    {
        Loop, read, Z:\auto_run.txt
        {
            IfExist, Z:\%A_LoopReadLine%
                Run, C:\Program Files (x86)\Notepad++\notepad++.exe Z:\%A_LoopReadLine%
        }
        FileDelete, Z:\autohotkey\auto_run.txt
    }

    Sleep, 100 ; Short sleep
}

如果您不想将脚本锁定在循环中,您也可以使用计时器。

#Persistent

fullFilePath := "Path\To\File.txt"
SetTimer, CheckForFile, 500
return

CheckForFile:
    if (FileExist(fullFilePath)) {
        ; Do something with the file...
        Loop, Read, %fullFilePath%
        {
            MsgBox % A_LoopReadLine
        }

        ; Delete the file
        FileDelete, %fullFilePath%
    }
return