SOLVED: Error: #Include file "myfile.ahk" cannot be opened

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

SOLVED: Error: #Include file "myfile.ahk" cannot be opened

Post by Miguel7 » 27 Sep 2014, 15:18

Hi every1,

I'm sure I'm missing something microscopic but important (as is the case with most programming errors lol) but when trying to include a file I get this:

Error at line 13 [or wherever I try to put the #Include statement]
#Include file "myfile.ahk" [with or without quotes] cannot be opened [why? lol]
The program will exit. [Gee thanks]

The script I'm trying to include is in the same folder and is spelled correctly in the script I'm trying to include it in. I right-clicked it and went to Properties, and it's not marked as Read-Only. So for some mysterious reason, it just "cannot be read"... Any idea what's going on? Thanks. :)
Last edited by Miguel7 on 27 Sep 2014, 21:50, edited 1 time in total.

lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: Error: #Include file "myfile.ahk" cannot be opened

Post by lexikos » 27 Sep 2014, 20:38

The path of a file or directory as explained below. This must not contain double quotes, wildcards, or variable references except %A_ScriptDir%, %A_AppData%, %A_AppDataCommon% and (in v1.1.11+) %A_LineFile%. Escape sequences other than semicolon (`;) must not be used, nor are they needed because characters such as percent signs are treated literally.

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.

Source: #Include
A script's initial working directory is determined by how it was launched. For example, if it was run via shortcut -- such as on the Start Menu -- its working directory is determined by the "Start in" field within the shortcut's properties.
Source: SetWorkingDir
Some other unlikely causes: the user running the script might not have permission to access the file, or some other program might have it locked for reading.

Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

Re: SOLVED: Error: #Include file "myfile.ahk" cannot be open

Post by Miguel7 » 27 Sep 2014, 21:51

Yeah, I didn't think adding quotes would help, but at that point I was grasping at straws. But the part you put in bold ("...assumed to be in the startup/working directory...") seems to be what caused the problem. That's still a bit of a surprise, since I've never had to do this before, but whatever - bottom line is, I got it working by sticking "%A_ScriptDir%/" (without the quotes) before the file name. Thanks again. :)

lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: SOLVED: Error: #Include file "myfile.ahk" cannot be open

Post by lexikos » 27 Sep 2014, 23:08

Did you change the way that you were launching the script?
I wrote:
A script's initial working directory is determined by how it was launched.

Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

Re: SOLVED: Error: #Include file "myfile.ahk" cannot be open

Post by Miguel7 » 29 Sep 2014, 08:20

I'm not really sure what you mean, "how" it was launched... do you mean whether or not I did a right-click and chose "Run Script" vs. double-click on the file? I think I'm going to need to research the page you are referring to (about the "working" directory - which up till now has meant the same thing as %A_ScriptDir%).

User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: SOLVED: Error: #Include file "myfile.ahk" cannot be open

Post by Masonjar13 » 29 Sep 2014, 15:52

By "how", he means directly opening it vs. opening it with a shortcut/script/etc.. By default, the working directory is the script directory. There can be an exception in that when opening it with another script or shortcut. The working directory can be changed at (almost) any time with SetWorkingDir, so they are not the same.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build

lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: SOLVED: Error: #Include file "myfile.ahk" cannot be open

Post by lexikos » 29 Sep 2014, 16:40

By default, the working directory is the script directory.
It only appears that way because you usually launch scripts using Explorer. In v1, the initial working directory is exactly whatever the parent process set it to when launching AutoHotkey.exe. When launching by double-clicking in Explorer, that's usually the directory containing the file being launched. (The difference is that the working directory is set by Explorer, which knows nothing about scripts or script directories.)

In v2, #include is relative to the directory which contains the current file (A_LineFile), and the working directory defaults to A_ScriptDir.

David

Re: SOLVED: Error: #Include file "myfile.ahk" cannot be opened

Post by David » 13 May 2015, 13:01

Can anyone translate this in german?
I also have this problem...

User avatar
gregster
Posts: 9224
Joined: 30 Sep 2013, 06:48

Re: SOLVED: Error: #Include file "myfile.ahk" cannot be opened

Post by gregster » 13 May 2015, 14:58

David wrote:Can anyone translate this in german?
I also have this problem...
Hallo David, ich probiers mal:

Hier wurde keine explizite Pfadangabe für das include-File verwendet. In so einem Fall sucht AHK im aktuellen Arbeitsverzeichnis des Skripts nach dem include-File.
Das Problem lag hier jedoch darin begründet, dass das Arbeitsverzeichnis des Skripts ein anderes war als vom Fragesteller erwartet. Dieses 'working directory' ist nämlich davon abhängig, wie das Skript gestartet wird (z. B. Doppelklick im Explorer oder Verknüpfung im Start-Menü).
Hier wurde das Problem gelöst, indem eine Pfadangabe hinzugefügt wurde - in diesem Fall ergab das: #Include %A_ScriptDir%\myfile.ahk, weil hier der include-File im gleichen Verzeichnis lag wie das Hauptskript; andere (auch absolute) Pfadangaben sind natürlich auch möglich (z. B. #Include C:\My Documents\Scripts\myfile.ahk )
Oder man setzt alternativ vorher das Arbeitsverzeichnis explizit mit SetWorkingDir oder z. B.:
#Include %A_ScriptDir% ; Setzt das Arbeitsverzeichnis für folgende #Includes

Wir haben hier übrigens auch ein deutschsprachiges Forum http://ahkscript.org/boards/viewforum.php?f=8 . Am besten fügst du immer den Programmcode zu deinem Post hinzu, der bei dir nicht funktioniert (idealerweise noch weitere Infos wie Windows-Version und AHK-Version) und beschreibst dein Problem so genau wie möglich.

partof
Posts: 110
Joined: 16 Jan 2016, 08:38

Re: SOLVED: Error: #Include file "myfile.ahk" cannot be opened

Post by partof » 06 Nov 2020, 08:47

be careful the error mesage isn't accurate:

I got the error:
Error: This parameter contains a variable name missing its ending percent sign.
but the path was ok, it's only the script I included which contained an error.

Try to comment stuff on your script to check if it comes from there.

User avatar
gregster
Posts: 9224
Joined: 30 Sep 2013, 06:48

Re: SOLVED: Error: #Include file "myfile.ahk" cannot be opened

Post by gregster » 06 Nov 2020, 08:54

partof wrote:
06 Nov 2020, 08:47
be careful the error mesage isn't accurate:

I got the error:
Error: This parameter contains a variable name missing its ending percent sign.
but the path was ok, it's only the script I included which contained an error.
That's clearly a different error message than discussed in this old topic.

Post Reply

Return to “Ask for Help (v1)”