david1337

Look for changes in #include files in compiled script

16 posts in this topic

#1 ·  Posted (edited)

Hey guys

I have a script in which I have included some self-made files in the script directory. In these files I have various variables defined.

This is how the file is included and then called in the main script:

#include "Data\test.au3"


MsgBox(0,"",$variable1)

 

test.au3 could contain something like:

$variable1 = "Hello1"
$variable2 = "Hello2"
$variable3 = "Hello3"

 

Now, if I want to change the value of $variable1 and then run the script, no problem. The script uses the new edited variable.

 

But If I compile the script, the includes are compiled within the EXE file as intended.
Now, if I change the value of $variable1 and run the EXE file, it doesn't look for my change, since it uses whatever variable that was defined when the script was compiled.

 

Is there any way that I can have files with variables that changes, and then have a compiled version using these changes, without having to compile again?

 

Thanks in advance.

David

Edited by david1337

Share this post


Link to post
Share on other sites



Use normal .txt files with filerread or ini files with iniread instead.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi AutoBert

Thank you for answering.

It's not just that simple is it?

Even though I do a fileread on test.txt which contains my variables, it doesn't just mean that my mainscript now is able to use those variables? Or am I missing something?

Edited by david1337

Share this post


Link to post
Share on other sites

A code example is always nice for more in detail help

Share this post


Link to post
Share on other sites

@ l3ill : Thanks, I will try and have a look, to see if I can use your example :)

@ genius257: I actually did give a very simple example. That's all that should be needed according to my question :)

Thanks

Share this post


Link to post
Share on other sites

Hey l3ill

I am beginning to understand how I can use INI files instead :)

Thank you!

- David

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Hey Bill :)

I got everything working with the INI files now, except one thing!

I have a for/next section which uses all that is included in the $Custom[] variable.

Quick example (working):

Global $Custom[] = ["01_Test", "02_Test", "03_Test", "04_Test", "05_Test", "06_Test", "07_Test", "08_Test", "09_Test", "10_Test"]

For $i = 0 To UBound($Custom) - 1

If FileExists $Test[$i] Then
.......
Next

"01_test", "02_Test" etc. needs to be renamed frequently, so I don't want this part to be inside the actual script as in the above example.

So I tried to solve it with an INI file, just like I did with all the variables:

[Test Section]
Test=["01_Test", "02_Test", "03_Test", "04_Test", "05_Test", "06_Test", "07_Test", "08_Test", "09_Test", "10_Test"]

And the code calls it like this:

Global $Test[] = IniRead($DataFile, "Test Section", "Test", "")

Even though a consolewrite of $Test shows what I expect, I guess I can't do it like this? :)

Edited by david1337

Share this post


Link to post
Share on other sites

#10 ·  Posted

On my way out but at quick glance it looks like you are trying to call an array. I would have to test that myself as I have never used ini to hold arrays.

I suggest some error checking to see where your code is skipping and read up on arrays and ini files.

If you don't find it and no one else shows up I will back later...

Good Luck !

Bill 

Share this post


Link to post
Share on other sites

#11 ·  Posted

It does not seem to be possible to write an array to a single value, without some kind of imagination.

Alternatively you could do something like this:

Dim $variable[] = ["a","b","c","d","e"]

IniDelete(@ScriptName&".ini", "variable")

For $i = 0 To UBound($variable, 1)-1
    IniWrite(@ScriptName&".ini", "variable", $i, $variable[$i])
Next

;read example below here:

$i=0
Dim $variable[1]
Do
    $data = IniRead(@ScriptName&".ini", "variable", $i, Null)
    ConsoleWrite(@error&@CRLF)
    If $data=="" Then ExitLoop
    $variable[UBound($variable, 1)-1] = $data
    ReDim $variable[UBound($variable, 1)+1]
    $i+=1
Until 0
ReDim $variable[UBound($variable, 1)-1]

For reading you could also do something with "IniReadSection"

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Interesting fix ^_^

I was thinking more along the lines of array to string then store in INI and then vice versa when reading.

Perhaps a CSV file or excel would  serve better than ini for array storage?

Edited by l3ill

Share this post


Link to post
Share on other sites

#13 ·  Posted

Have a look at:

_ArrayToString

use a more obscure delimiter than a comma like a pipe or something ' | '     

This will give you a single string that you can save to INI

Then when reading from INI you get the string as a variable and use:

StringSplit

with your pipe delimiter and you have your array back ready to work with.

Share this post


Link to post
Share on other sites

#14 ·  Posted

22 minutes ago, l3ill said:

Interesting fix

using ScriptControl and JSON a string conatining any char "should" be able to be saved and loaded like this:

$oSC = ObjCreate("ScriptControl")
$oSC.Language = "JScript"
$oSC.AddCode(BinaryToString(InetRead("https://raw.githubusercontent.com/douglascrockford/JSON-js/master/json2.js")))
$oSC.Eval("Array.prototype.Item = function(i){return this[i];};")
$z = $oSC.Eval('new Function("a","return JSON.stringify(a)")')
Dim $a[] = ['a','b','c']
$b = $oSC.Eval("new Array()")
For $i=0 To UBound($a, 1)-1
    $b.push($a[$i])
Next
;~ MsgBox(0, "", $oSC.Eval("JSON.Stringify()"))
MsgBox(0, "", $z($b))

IniWrite(@ScriptName&".ini", "variable", "array", $z($b))

MsgBox(0, "", IniRead(@ScriptName&".ini", "variable", "array", "nope"))

 

Share this post


Link to post
Share on other sites

#15 ·  Posted

I prefer a simpler approach:

  1. Takes an array and makes it a string
  2. Stores the string to an INI file
  3. Reads the string from INI file
  4. Turns it back into an array
#include <Array.au3>
$sFilePath = @ScriptDir & "/tester.ini"

Global $aCustom[10] = ["01_Test", "02_Test", "03_Test", "04_Test", "05_Test", "06_Test", "07_Test", "08_Test", "09_Test", "10_Test"]
Local $sStringArray = _ArrayToString($aCustom, ',')
Local $sStringRepl = StringReplace($sStringArray, ',', '|')
MsgBox(0, "Array to String", $sStringRepl)

IniWrite($sFilePath, "SectionName", "TestKey", $sStringRepl)

Sleep(3000)
$sTestString = IniRead($sFilePath, "SectionName", "TestKey", Default)
$aString_to_array = Stringsplit($sTestString, '|')

_ArrayDisplay($aString_to_array)

 

Share this post


Link to post
Share on other sites

#16 ·  Posted

Hey guys

Thanks for your suggestions!
I am working on the implementation to the actual script right now.
Array to string - to INI - to array, is an interesting approach Bill. That might be the solution :)
 

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

  • Similar Content

    • AmarokStudios
      Include Full Directory
      By AmarokStudios
      Good afternoon AutoIt community!,
      I was on Stackoverflow the other day and came across this question: How To Include Files From A Directory. This got me thinking... There has to be a way to do it... After a bunch of research, I wasn't able to find anything. So, I created this UDF to dynamically include every file from the directory. Of course, there are some bugs that I'd like to fix eventually, but for the most part, it works. Simply call the main function (Shown below) right after the rest of the includes before any of your actual code, and the UDF will include all of the au3 files in the specified directory. Without further ado, here is the _includeDir UDF and how to use it!
       
      _includeDir.au3
       
      Download this code (_includeDir.au3 attached as well) and place it into the directory with your current script. Next, include it in your main file. For now, I'm going to be using one called Example.au3.
       
      #Include "_includeDir.au3" Now, include whatever else you're going to be including in this script, then call the _includeDir function. NOTE: THIS MUST BE CALLED AT THE TOP OF THE SCRIPT, BEFORE ANY CODE IS WRITTEN! THE FUNCTION FORCES THE SCRIPT TO RESTART SO PUTTING IT LATER IN THE SCRIPT WILL RE-RUN THE CODE!
       
      Example.au3
      #Include "_includeDir.au3" #Include <File.au3> ; Not needed. Just here as an example of a normal script. #Include <Array.au3> ; Not needed. Just here as example of normal script. _includeDir("Directory to Include") MsgBox(0,"Example","This is just an example!") See how the function is called near the top? This is the proper use of the UDF. If you had a folder called "Directory to Include" and had a bunch of .au3 files inside of it, the function would include them all into the Example.au3 script.
      If you run the Example.au3 file now, it will most likely tell you "The directory Things to Include does not exist!". Make sure you enter the name of the directory you're trying to include. Just as a side note, when including files, you should put all of the code in the INCLUDED files inside of functions so they aren't automatically run when included. Variables can be outside of the functions so they are automatically set. Remember, if you have a variable in one included file with the same name of variable in another included file, it will be overwritten with whichever include file was included last.
       
      Anyways, if you have pointed the directory to include parameter to a folder that exists and run the Example.au3, it will generate a folder called IncludeDirUDF. It will also write a new line inside of Example.au3. It will write the line 
      on line one. Of course, you don't always want it to be written to line one, right? Maybe you want this bit of code to be written on line 3 in order to keep your code organize. Is there a way to do this? Absolutely! Simply add the line number as a second parameter to the function. For example, we want to have this bit of code written on line 3, we would set up our Example.au3 file to look like this.
      #Include "_includeDir.au3" #Include <File.au3> ; Not needed. Just here as an example of a normal script. #Include <Array.au3> ; Not needed. Just here as example of normal script. _includeDir("Directory to Include", 3) MsgBox(0,"Example","This is just an example!") See how we added the 3 to the end of _includeDir? This will tell the function to write the #Include "IncludeDirUDF\loadIncludes.au3" on line 3 of Example.au3. Note, the line HAS TO EXIST in order to be written to it. For example, if your Example.au3 code only has 6 lines, and you specify to be written on line 7, it WILL NOT WORK.
       
      The code is designed to include and restart in order to process the included files. For some reason, if you want to JUST generate the included file and NOT restart, you can add one more parameter to the code. If you don't want the code to restart, simply set your code up to look like this:
      #Include "_includeDir.au3" #Include <File.au3> ; Not needed. Just here as an example of a normal script. #Include <Array.au3> ; Not needed. Just here as example of normal script. _includeDir("Directory to Include", 3, False) MsgBox(0,"Example","This is just an example!") Note, if you set this last parameter to false, it will simply generate the included file and write to line 3 of Example.au3 (As specified before) and exit before the code reaches the MsgBox() and it will not be displayed.
       
      The last important thing to note: In order to re-include a different set of files, you must delete the #Include "IncludeDirUDF\loadIncludes.au3" from your main script (In this case, Example.au3) and delete the generated file, IncludeDirUDF. Now you can simply rerun Example.au3 and it will include any the dir with any changes you made to it. Only .au3 files should be in the directory you're trying to include as it will not process the other files and will generate an error.
       
      I hope this UDF helps somebody out! Comment any questions/concerns you may have and I will try to address them as soon as possible!
       
      Thanks,
      Timothy
      CEO - Amarok Studios
      _includeDir.au3
    • ur
      To skip code if it is a library
      By ur
      Is there any way to skip the execution of a particular block of code like any condition or expression, if the au3 file is included as library.?
      Like, in my code I wrote all the functionality as functions and called them in 4 lines.
      Now I have another requirement where I need to use the same code but different.So I am importing this code so that I can use these functions.
      But the code block which is outside the functions (main code) is being executed when I import the au3 file.Is there any condition to check whether the file is running directly or included in another au3 file as library, so that I can keep the same here.
       
      Please suggest if any ideas.
    • TheDcoder
      Is it possible to include file(s) in root folder?
      By TheDcoder
      Hello ,
      X:\Root Folder\Sub Folder
      Root Folder contains the script to include in the script located in Sub Folder... Is it possible without specifying the exact path?
      #include "X:\Root Folder\Sub Folder"  
      Thanks in Advance! TD
    • h.thanh1410
      #include "../file/file.au3" Erros, Please, help me
      By h.thanh1410
      in the file button/ BLoginvip72.au3 I want to get data of file input/ Passwordvip72.au3 and input/Usernamevip72.au3
      in file button .
      #include <AutoItConstants.au3>
      #include <MsgBoxConstants.au3>
      #include "..input/Passwordvip72.au3"
      #include "..input/Usernamevip72.au3"
      :(, Erros ,Please, help me. 


    • jaeger52
      User account causing script errors?
      By jaeger52
      Hello all,
      I've received great help from the forum community before, so I'm hoping for a repeat.  I have a series of compiled scripts that do the following: launch MS Access database, execute macros, provide mid-process credentials, and launch the following database for a rinse-and-repeat. I've used my credentials for the scripts, and after some adjustments everything runs just as intended. I've got dozens of good runs under my belt on my profile with my credentials. However, when my coworker attempts to run the scripts on his profile, the scripts only get through the login step. When the macros finish and the database closes, the script just hangs. It's still running and unpaused, but it's not handing off like it's supposed to. I can post script if anyone would like to see it, but I wonder if anybody has any idea why this script would work for one user and not another. For the record, we also created a duplicate script using his credentials instead of mine, and had the exact same problem. Any ideas? 
      Thanks in advance!