brollin
brollin

Reputation: 110

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

I've got the following bit of code, which should work as far as I know. The goal is to continually check for the existence of Z:\auto_run.txt. Once it exists, each line of the file (being a path to a file), should be opened in notepad++. Finally, delete Z:\auto_run.txt.

The last bit I have gotten to work independently. The question is how to continually check for the file's existence? When I run the below code in a standard Autohotkey.ahk, it does not seem to work, and even when the file exists, nothing happens.

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
}

Upvotes: 2

Views: 1222

Answers (1)

Sid
Sid

Reputation: 1719

Would putting the whole thing in a loop work?

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
}

If you don't want to lock the scrip to the loop you can use a timer as well.

#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

Upvotes: 4

Related Questions