Causes the script to behave as though the specified file's contents are present at this exact position.
#Include FileOrDirName #Include <LibName> #IncludeAgain FileOrDirName
Type: String
The path of a file or directory as explained below. This must not contain double quotes (except for an optional pair of double quotes surrounding the parameter), wildcards (* and ?) or escape sequences other than semicolon (`;).
Supported built-in variables may be used by enclosing them in percent signs (for example, #Include "%A_ScriptDir%"). Unrecognized references and other percent signs are interpreted literally.
Variables are evaluated at compile time and may differ from their values when the script runs. The following variables are supported: A_AhkPath, A_AppData, A_AppDataCommon, A_ComputerName, A_ComSpec, A_Desktop, A_DesktopCommon, A_IsCompiled, A_LineFile, A_MyDocuments, A_ProgramFiles, A_Programs, A_ProgramsCommon, A_ScriptDir, 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. By default, relative paths are relative to the directory of the file which contains the #Include directive. This default can be overridden by using #Include Dir as described below. Note: SetWorkingDir has no effect on #Include because #Include is processed before the script begins executing.
On Windows, an explicit local named-pipe path (\\.\pipe\Name) reads the included source from the pipe. This applies to both #Include and #IncludeAgain.
Directory: Specify a directory instead of a file to change the working directory used by all subsequent occurrences of #Include and FileInstall in the current file. 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.
Note: This parameter is not an expression, but can be enclosed in quote marks (either 'single' or "double").
Type: String
A library file name, optionally containing a subdirectory. Variable references are not allowed. See Script Library Folders for the search directories and the AhkLibPath environment variable.
In v2.1 mode, the source file's directory is searched first, followed by the library directories. Each directory is checked for the exact filename, then .ks and .ahk extension candidates. There is no underscore fallback, and a directory's __Init file is not included.
In v2.0 mode, only the library directories are searched, with .ks and .ahk appended to LibName. If no file is found, the search repeats after removing the last underscore and the text following it, continuing with each remaining underscore. For example, #Include <lib_func> can include lib.ahk if lib_func.ahk is absent.
These are load-time rules, using the #Requires mode in effect at the directive. A mode selected inside a function, class or included file follows Keysharp's scoping rules; the enclosing mode is restored at the end of that scope or file.
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 the specified file 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 file path may optionally be preceded by *i and a single space, which causes the program to ignore any failure to read the file. For example: #Include "*i SpecialOptions.ahk". This option should be used only when the 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.
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.
Script Library Folders, Functions, FileInstall
Includes the contents of the specified file into the current script.
#Include "C:\My Documents\Scripts\Utility Subroutines.ahk"
Uses modules to resolve a conflict between two libraries with the same name.
; Main.ahk
#Requires AutoHotkey v2.1-alpha.35
#Import "v2.0\Script2.ahk"
#Include <MyLib>
MyLib("Main.ahk")
; Lib\MyLib.ahk #Requires AutoHotkey v2.1-alpha.35 MyLib(Source) => MsgBox(A_LineFile, Source)
; v2.0\Script2.ahk
#Requires AutoHotkey v2.0
#Include <MyLib>
MyLib("Script2.ahk")
; v2.0\Lib\MyLib.ahk #Requires AutoHotkey v2.0 MyLib(Source) => MsgBox(A_LineFile, Source)