Sign in to follow this  
Followers 0
rayen99

Dynamic #include. How To?

3 posts in this topic

How can I do includes dynamically? I have a script that scours a directory tree and looks for files named install.au3, when it finds one it executes it. This works good, but there is a major problem with this setup. I would like all of my install.au3 files to be able to share some support functions. Naturally I thought I could put all of these functions in a separate .au3 file and have the parent script pass off the path to the functions script as a parameter to the install.au3 script. Then install.au3 would do an #include $CmdLine[1]. But alas, #include only takes fixed strings. This limitation also prevents me from my original solution to the problem which was to do includes in the parent script to import in the code from each install.au3 as I found them.

So... My next idea was to read in each install.au3 with a FileRead() and then use Execute() on contents, but the restrictions placed on Execute() limit me from doing anything useful with this strategy. Eval won't work for this either.

This seems like a common problem, so I would think there is some way to make this work using AutoIt only. I don't want to have to resort to filling in the gaps with another scripting language if I don't have to.

Also, the reason I need this kind of dynamic capability is because I want complete independence between the directory tree I am searching for install.au3 files and the calling script/helper functions script. That way I can run the parent off a network mount, or CDROM and search some other remote computer or flash drive, CD, etc. Because of this I can't do an #include "path_to_support_script" inside of each install.au3 because that path is unknown till runtime.

Any ideas? Thanks.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Why not simply search for the au3 file that you want to include, then when you find it, open the script that you want to include it in as an array? Then insert the include statement at the beginning of the array and then write the array to a temp file. Once you do that, then execute it. No dynamic include files necessary, just dynamically write the include to the top of a file and then write the dynamically created file to a temporary location and execute it.

EDIT

Here is a function i whipped up to do such a thing:

#include <file.au3>
#include <array.au3>

Func AddInclude($s_FNScriptToAddTo, $s_FNInclude, $s_TempScript)
    
Local $a_Script

_FileReadToArray($s_FNScriptToAddTo,$a_Script)

_ArrayInsert($a_Script,1,'#include "' & $s_FNInclude & '"')

_FileWriteFromArray($s_TempScript,$a_Script,1)

EndFunc
Edited by The Kandie Man

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

Why not simply search for the au3 file that you want to include, then when you find it, open the script that you want to include it in as an array? Then insert the include statement at the beginning of the array and then write the array to a temp file. Once you do that, then execute it. No dynamic include files necessary, just dynamically write the include to the top of a file and then write the dynamically created file to a temporary location and execute it.

EDIT

Here is a function i whipped up to do such a thing:

#include <file.au3>
#include <array.au3>

Func AddInclude($s_FNScriptToAddTo, $s_FNInclude, $s_TempScript)
    
Local $a_Script

_FileReadToArray($s_FNScriptToAddTo,$a_Script)

_ArrayInsert($a_Script,1,'#include "' & $s_FNInclude & '"')

_FileWriteFromArray($s_TempScript,$a_Script,1)

EndFunc

Man I wish I had thought of that before, that's a great solution. Thanks a ton!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0