#Include / #IncludeAgain

Causes the script to behave as though the specified file's contents are present at this exact position.

#Include FileOrDirName
#Include <LibName>
#IncludeAgain FileOrDirName

Parameters

FileOrDirName

The path of a file or directory as explained below. This must not contain double quotes, wildcards, or references to non-built-in variables. Escape sequences other than semicolon (`;) must not be used, nor are they needed because characters such as percent signs are treated literally.

Percent signs which are not part of a valid variable reference are interpreted literally. All built-in variables are valid, except for ErrorLevel, A_Args and the numbered variables. Prior to [v1.1.28], only %A_ScriptDir%, %A_AppData%, %A_AppDataCommon% and [in v1.1.11+] %A_LineFile% were supported.

Known limitation: When compiling a script, variables are evaluated by the compiler and may differ from what the script would return when it is finally executed. Ahk2Exe v1.1.30.00 and earlier only support the four variables listed above. [v1.1.30.01+]: The following variables are also supported: A_AhkPath, A_ComputerName, A_ComSpec, A_Desktop, A_DesktopCommon, A_IsCompiled, A_IsUnicode, A_MyDocuments, A_ProgramFiles, A_Programs, A_ProgramsCommon, A_ScriptFullPath, A_ScriptName, A_Space, A_StartMenu, A_StartMenuCommon, A_Startup, A_StartupCommon, A_Tab, A_Temp, A_UserName, A_WinDir.

File: The name of the file to be included, which is assumed to be in the startup/working directory if an absolute path is not specified (except for Ahk2Exe, which assumes the file is in the script's own directory). Note: SetWorkingDir has no effect on #Include because #Include is processed before the script begins executing.

Directory: Specify a directory instead of a file to change the working directory used by all subsequent occurrences of #Include and FileInstall. Note: Changing the working directory in this way does not affect the script's initial working directory when it starts running (A_WorkingDir). To change that, use SetWorkingDir at the top of the script.

LibName

[AHK_L 57+]: A library file or function name. For example, #include <lib> and #include <lib_func> would both include lib.ahk from one of the function library folders. LibName cannot contain variable references.

Remarks

A script behaves as though the included file's contents are physically present at the exact position of the #Include directive (as though a copy-and-paste were done from the included file). Consequently, it generally cannot merge two isolated scripts together into one functioning script.

#Include ensures that FileName is included only once, even if multiple inclusions are encountered for it. By contrast, #IncludeAgain allows multiple inclusions of the same file, while being the same as #Include in all other respects.

The FileName parameter may optionally be preceded by *i and a single space, which causes the program to ignore any failure to read the included file. For example: #Include *i SpecialOptions.ahk. This option should be used only when the included file's contents are not essential to the main script's operation.

Lines displayed in the main window via ListLines or the menu View->Lines are always numbered according to their physical order within their own files. In other words, including a new file will change the line numbering of the main script file by only one line, namely that of the #Include line itself (except for compiled scripts, which merge their included files into one big script at the time of compilation).

#Include is often used to load functions defined in an external file. Unlike subroutine labels, functions can be included at the very top of the script without affecting the auto-execute section.

Like other directives, #Include cannot be executed conditionally. In other words, this example would not work as expected:

if (x = 1)
    #Include SomeFile.ahk  ; This line takes effect regardless of the value of x.

Files can be automatically included (i.e. without having to use #Include) by calling a library function by name.

[v1.1.11+]: Use %A_LineFile%\.. to refer to the directory which contains the current file, even if it is not the main script file. For example, #Include %A_LineFile%\..\other.ahk. [v1.1.28+]: %A_AhkPath%\.. can be used to refer to the directory containing AutoHotkey.exe.

Libraries of Functions, Functions, FileInstall

Examples

Includes the contents of the specified file into the current script.

#Include C:\My Documents\Scripts\Utility Subroutines.ahk

Changes the working directory for subsequent #Includes and FileInstalls.

#Include %A_ScriptDir%

Same as above but for an explicitly named directory.

#Include C:\My Scripts