Including files and folders

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kiwichick
Posts: 139
Joined: 21 Jan 2014, 22:03

Including files and folders

15 Aug 2017, 18:30

Hi there,

I'm a little confused about how including files and folders works. At top of my script I have:
#NoEnv
#SingleInstance force
SetWorkingDir %A_ScriptDir%
#include %A_ScriptDir%\docs
#include ColorConstants.ahk
#include Functions.ahk
#include text_for_welcome.ahk
#include text_for_intro.ahk

Then in other places in the script I have my includes for other files in the docs folder:
#include file-in-docs-folder.ahk
#include another-file-in-docs-folder.ahk

That all works perfectly for every #include file I have and I'm assuming that's because all of the included files are in the docs folder. But now my docs folder is getting very full and I want to move some of the files to subfolders. That is where my problem begins. I started by changing the top of the script to:
#NoEnv
#SingleInstance force
SetWorkingDir %A_ScriptDir%
#include %A_ScriptDir%\docs
#include %A_ScriptDir%\docs\subfolder1
#include %A_ScriptDir%\docs\subfolder2
#include %A_ScriptDir%\docs\subfolder3
#include ColorConstants.ahk
#include Functions.ahk
#include text_for_welcome.ahk
#include text_for_intro.ahk

But that throws an error:
Error at line 8.
#Include file "ColorConstants.ahk" cannot be opened.
The program will now exit.

I'm assuming (again) it's because, by adding the subfolder includes, I have (as per the Help file) changed the working directory used by all subsequent occurrences of #Include". So I figure that means all subsequent includes are then being looked for in subfolder3. So I removed the subfolder includes and changed the included files to:
#include subfolder1\file-in-docs-folder.ahk
#include subfolder2\another-file-in-docs-folder.ahk

That works but my question is, is it possible change the top of the script to have the additional included folders there and, if so, how? Obviously it makes for better maintenance if I only have to make any future changes to one #include line up there rather than searching and making changes to every occurrence.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Including files and folders

15 Aug 2017, 21:45

This is all speculation, I haven't tested. But maybe if you have included SetWorkingDir %A_ScriptDir% inside of any of those included files, it updates to where that included file is rather than the "parent" script. If this is the case, I would bet it could be bypassed/resolved by including SetWorkingDir %A_ScriptDir% between every #include __
kiwichick
Posts: 139
Joined: 21 Jan 2014, 22:03

Re: Including files and folders

15 Aug 2017, 23:22

Exaskryz wrote:This is all speculation, I haven't tested. But maybe if you have included SetWorkingDir %A_ScriptDir% inside of any of those included files, it updates to where that included file is rather than the "parent" script. If this is the case, I would bet it could be bypassed/resolved by including SetWorkingDir %A_ScriptDir% between every #include __
Thanks Exaskryz, Unfortunately, that didn't work. But would putting SetWorkingDir %A_ScriptDir% inside the included files work? If they're not included in the main script how would the main script be able to access them in the first place to then be able to read the #include inside?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Including files and folders

16 Aug 2017, 11:50

Ignore the spoiler. Aye, aye, aye. I had a long post going on trying to solve this, but, you're totally right that you were changing the directory for subsequent #Includes. You weren't including all the ahk scripts inside each subfolder, no. You were setting the directory for subsequent additions.
Directory: Specify a directory instead of a file to change the working directory used by all subsequent occurrences of #Include and FileInstall.
So, it sounds like what you'd want for the top of your script is this:

Code: Select all

#NoEnv
#SingleInstance force
SetWorkingDir %A_ScriptDir%
#include %A_ScriptDir%\docs
#include ColorConstants.ahk
#include Functions.ahk
#include text_for_welcome.ahk
#include text_for_intro.ahk
#include %A_ScriptDir%\docs\subfolder1
#include file-in-docs-folder.ahk ; except it's not in the docs folder anymore, you moved it to subfolder1
#include %A_ScriptDir%\docs\subfolder2
#include another-file-in-docs-folder.ahk ; except it's not in the docs folder anymore, you moved it to subfolder2
#include %A_ScriptDir%\docs\subfolder3
Spoiler
kiwichick
Posts: 139
Joined: 21 Jan 2014, 22:03

Re: Including files and folders

18 Aug 2017, 16:58

Exaskryz wrote:Ignore the spoiler. Aye, aye, aye. I had a long post going on trying to solve this, but, you're totally right that you were changing the directory for subsequent #Includes. You weren't including all the ahk scripts inside each subfolder, no. You were setting the directory for subsequent additions.
Directory: Specify a directory instead of a file to change the working directory used by all subsequent occurrences of #Include and FileInstall.
So, it sounds like what you'd want for the top of your script is this:

Code: Select all

#NoEnv
#SingleInstance force
SetWorkingDir %A_ScriptDir%
#include %A_ScriptDir%\docs
#include ColorConstants.ahk
#include Functions.ahk
#include text_for_welcome.ahk
#include text_for_intro.ahk
#include %A_ScriptDir%\docs\subfolder1
#include file-in-docs-folder.ahk ; except it's not in the docs folder anymore, you moved it to subfolder1
#include %A_ScriptDir%\docs\subfolder2
#include another-file-in-docs-folder.ahk ; except it's not in the docs folder anymore, you moved it to subfolder2
#include %A_ScriptDir%\docs\subfolder3
Yeah I had thought of that, too, but if I do that, they're all loaded and executed straight away, and that breaks the script. The included files are mostly part of g-label targets so only get executed when needed. But even so, I would still have to change every line that includes the subfolders if I wanted to do something as basic as renaming one of those subfolders - and some of the files are #includeagain so I would still have to find all of those to change as well. I can't even assign variables to the subfolders because variables aren't permissible with #include (apart from the exceptions in the Help file). Bit of an irony really, seeing as this is the kind of thing variables are meant for. It's just a shame that #include works the way it does. Not that there's necessarily anything wrong with it, it would just be nice if it was easier to include multiple folders or, at the very least, allow variables to be used as part of the #include filepath (although I'm sure there's a very good reason they're aren't allowed). But, for now, I've left things as they are and added the subfolders to each #include, and just hope I don't want to make too many changes later on.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Including files and folders

18 Aug 2017, 17:20

You can include files at the bottom of a script below the auto-execute section.

I can't imagine there's any easier way if you're going around renaming folders. AHK has to be able to find the file somehow. I have done massive folder restructuring maybe three times in my life when I felt like I needed to sort out some clutter. But maybe you like to do it daily :think:

The reason you can't do variables for an #Include is because #Include takes priority over executing any line of the script. If you treated #Include like a regular command, like a MsgBox, then you run the risk of the variable being changed by what was just Included.
TAC109
Posts: 1111
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: Including files and folders

18 Aug 2017, 18:31

@kiwichick
As stated in the #include documentation, #includes are processed before the script is executed. Therefore there are no variables defined (except as documented).

Also, as SetWorkingDir is a normal executed command, it can't have any effect on what directories are used when the #includes are processed. (This is also stated in the #include documentation).

I get the impression that you are attempting to use #includes to hold data which could be changed occasionally. If this is the case it would be better to hold this data in a file (possibly an .ini file) and read it into your program during initialisation to be used as required in other parts of your scripts.
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Including files and folders

18 Aug 2017, 19:21

Here are some tips re. #Include:

Specify the working directory when you use Run:

Code: Select all

Run, "%A_AhkPath%" "%vPathScript%", "%vDirScript%"
The use of '..' is mentioned here:
A_UserName to be allowed in #include - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 13&t=15926

Use '*i' and '..':
- '*i' (include a file only if it exists)
- '..' (backtrack by one directory):

Code: Select all

;e.g. specify a script that is on the Desktop

;C:\Users\%username%\AppData\Roaming
;-1 -> C:\Users\%username%\AppData
;-1 -> C:\Users\%username%
;+1 -> C:\Users\%username%\Desktop

;C:\Documents and Settings\%username%\Application Data
;-1 -> C:\Documents and Settings\%username%
;+1 -> C:\Documents and Settings\%username%\Desktop

#Include *i %A_AppData%\..\..\Desktop\MyScript.ahk
#Include *i %A_AppData%\..\Desktop\MyScript.ahk
A file that you include could just be an empty ahk file, with an include to some other file/files.

See also:
#Include
https://autohotkey.com/docs/commands/_Include.htm
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CrowexBR, Google [Bot] and 252 guests