How to batch import ahk scripts using 'Loop Files'? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
XMB-7
Posts: 18
Joined: 08 Jan 2023, 20:58
Contact:

How to batch import ahk scripts using 'Loop Files'?

12 Apr 2023, 11:35

I'd like to import (using #include) multiple AHK scripts into one single script.
However, the following code snippet reported an error that the '.../a_loopfilepath' file doesn't exist.
Appreciate any guidance on this issue.

Code: Select all

base_dir := 'utils'
Loop Files, (base_dir . '/' . '*.ahk'), 'R'
{
    #Include A_LoopFilePath
    ; A_LoopFilePath='utils/test.ahk'
    ; directly #Include 'utils/test.ahk' works
    break
}
User avatar
boiler
Posts: 17390
Joined: 21 Dec 2014, 02:44

Re: How to batch import ahk scripts using 'Loop Files'?

12 Apr 2023, 11:43

#Include happens before the script is run, so you can't have it be part of a loop or anything like that. You can automate the creation of the list of files and then paste that text into your script. It cannot be done dynamically.
ntepa
Posts: 439
Joined: 19 Oct 2022, 20:52

Re: How to batch import ahk scripts using 'Loop Files'?  Topic is solved

12 Apr 2023, 15:13

You can automatically create a file instead, then include that:

Code: Select all

#Include *i auto_includes.ahk

AutoInclude("R", "utils")

; recurse - R to recurse
; folders - variadic list of folders to include
AutoInclude(recurse?, Folders*) {
    static writeFile := A_WorkingDir "\auto_includes.ahk"
    newIncludes := ""
    for folder in Folders {
        if !(FileExist(folder) ~= "D")
            throw Error("Folder not found", -2, folder)
        loop files, folder "\*.ahk", "F" (recurse??"") {
            if A_LoopFileFullPath = writeFile
            || A_LoopFileFullPath = A_ScriptFullPath
            || A_LoopFileFullPath = A_LineFile
                continue
            newIncludes .= "#Include *i " A_LoopFileFullPath "`n"
        }
    }
    currentIncludes := FileRead(writeFile, "`n UTF-8")
    if currentIncludes != newIncludes {
        fileObj := FileOpen(writeFile, "w`n")
        fileObj.Write(newIncludes)
        fileObj.Close()
        Reload()
        Exit()
    }
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: emp00 and 52 guests