Skip to content

Instantly share code, notes, and snippets.

@davebrny
Last active January 19, 2023 03:10
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davebrny/55de3ab40499e33e40324ac6a96b70c0 to your computer and use it in GitHub Desktop.
Save davebrny/55de3ab40499e33e40324ac6a96b70c0 to your computer and use it in GitHub Desktop.
πŸ“ƒ (autohotkey) - create a list of .ahk files to be #included in your main script
/*
[search folders]
folder_1 =
folder_2 =
[ignore paths]
C:\Users\documents\scripts\example script name.ahk
C:\Users\documents\scripts\example folder name
[settings]
disable_warning = true
execute_names = \auto-executes\,\auto executes\
function_names = \functions\,\lib\
save_folder =
[stats]
script count =
list time =
*/
auto_include() {
start_time := a_tickCount
iniRead, disable_warning, % a_lineFile, settings, disable_warning
i := (disable_warning = "true") ? ("*i ") : ("")
iniRead, ignore_list, % a_lineFile, ignore paths
iniRead, execute_names, % a_lineFile, settings, execute_names
iniRead, function_names, % a_lineFile, settings, function_names
iniRead, save_folder, % a_lineFile, settings, save_folder
loop,
{
iniRead, search_folder, % a_lineFile, search folders, folder_%a_index%
if (search_folder = "")
continue
loop, files, % rTrim(search_folder, "\") . "\*.ahk", FR
{
if inStr(ignore_list . "`n", a_loopFileDir . "`n")
ignore_folder := a_loopFileDir
if a_loopFileDir contains %ignore_folder%
continue ; ignore certain folders
else if inStr(ignore_list, a_loopFileFullPath)
continue ; ignore certain files
script_count++
new_line := (a_loopFileDir = last_dir) ? "" : "`n"
last_dir := a_loopFileDir ; (add an extra line between folders)
if a_loopFileFullPath contains %execute_names%
executes .= new_line . "#include, " . i . a_loopFileFullPath . "`n"
else scripts .= new_line . "#include, " . i . a_loopFileFullPath . "`n"
if a_loopFileFullPath contains %function_names%
functions .= new_line . "#include, " . i . a_loopFileFullPath . "`n"
}
}
until (search_folder = "ERROR") ; loop end
if (save_folder = "")
save_folder := a_scriptDir "\include lists"
update_include_list(executes, save_folder "\~executes.ahk")
update_include_list(scripts, save_folder "\~scripts.ahk")
if (function_names)
update_include_list(functions, save_folder "\~functions.ahk")
; iniWrite, % " " script_count, % a_lineFile, stats, script count
; iniWrite, % " " (a_tickCount - start_time) " ms", % a_lineFile, stats, list time
}
update_include_list(new_text, list_file) {
file := fileOpen(list_file, "r `n")
current_text := file.read()
top_text := " `; ! auto-generated file. any changes made here will be overwritten`n"
new_text := strReplace(top_text . new_text, a_scriptDir, "%a_scriptDir%")
if (current_text != new_text)
{
if !fileExist(list_file)
{
splitPath, list_file, , file_dir
fileCreateDir, % file_dir
}
file := fileOpen(list_file, "w `n")
file.write(new_text)
}
file.close()
}
/*
[script info]
version = 1.10
description = create a list of .ahk files to be #included in your main script
author = davebrny
source = https://gist.github.com/davebrny/55de3ab40499e33e40324ac6a96b70c0
*/
@davebrny
Copy link
Author

davebrny commented Nov 4, 2017

include_folder()

this is basic version of the above auto_include() function that is designed to be more compact so it can be included in a self contained script

  • put include_folder() in the auto-execute section
include_folder(a_scriptDir)  
include_folder(a_scriptDir "\or a certain folder")  
  • include the following snippet into your script
include_folder(folder) {
    loop, files, % folder "\*.ahk", FR
        {
        if a_loopFileFullPath contains %a_scriptFullPath%,%a_lineFile%,\lib\
            continue
        stringReplace, loop_path, a_loopFileFullPath,  %a_scriptDir%, `%a_scriptDir`%
        new_includes .= (new_includes ? "`n" : "") . "#include, *i " . loop_path
        }

    file := fileOpen(a_scriptFullPath, "r `n")
    this_file := file.read()

    stringGetPos, pos, this_file, % "`;include_" "these:", R1
    stringMid, current_includes, this_file, pos + 17
    stringMid, text_before, this_file, pos-1, , L
    text_before := inStr(this_file, "`;include_" "these:") ? text_before : this_file

    if (current_includes != new_includes)
        {
        file := fileOpen(a_scriptFullPath, "w `n")
        file.write(text_before "`n`;include_" "these:`n" new_includes)
        }
    file.close()
}
  • ! dont put anything after ;include_these: or after the include files that will appear at the end of your script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment