Page 1 of 1

Variable that all scripts refer to

Posted: 29 Apr 2019, 18:50
by Flipscript
Given i have placed the app „7-Zip“ and its executable file in the following save location:
„D:\7-Zip\7z.exe“

Furthermore I have a lot of scripts that refer to this path to add files to or extract files from an archive. Therefore i set a variable at the beginning of every of those scripts such as:
SevenZipAppExe := "D:\7-Zip\7z.exe "

Now if I change the save location of the 7-Zip-App, which I sometimes like to do, obviously all the scripts no longer find the save location and i would have to rewrite every single script to make them work again.

So for this purpose I need a way to easily change the reference point to the changed save location of the 7-Zip-App for all scripts.

I read a lot about global variables, the command #Include and IniRead and somehow I'm quite confused. And since I don’t have any experience with these tools I would be very happy and thankful if anyone could give me an advice what would be the best way to solve my described problem.

Re: Variable that all scripts refer to

Posted: 29 Apr 2019, 19:33
by Osprey
#Include is probably the most straight forward solution and the best one for you. It simply adds all of the code from another script to the place where it's called. Just create a new script--I would call it Common.ahk, so that you could add other variables later on and not feel that you have to change its name later on--and put your variable declaration in it, like so:

Code: Select all

; Common.ahk
; Common variables to be included in every script

SevenZipAppExe := "D:\7-Zip\7z.exe"
Then, in each of your scripts, simply add (preferably at the top):

Code: Select all

#Include Common.ahk
Of course, you'll have to make sure that that file is always in the same folder as the script that you're running or change each #Include line to give a full path.

Re: Variable that all scripts refer to

Posted: 09 May 2019, 11:41
by Flipscript
Well, maybe that’s it Osprey. And if I come up with the glorious idea of changing the path for the variable-script (/common.ahk) later on then i would have to change every script as well. The problem would remain and I would go round in circles like a perpetuum mobile.

So, at the end of the day there must be at least one clear identifiable path. I tried out a search loop that figures out the save location of the „7z.exe“ at the beginning of the script in order to be path-independent. But that takes a long time and therefore is rather unpractical since the scripts are pretty short and contain just a few commands to add some files to an archive. Another idea would be an ID that refers to the exe-file ot its directory so it wouldn't matter if I change the path. However I have no clue if Windows even assigns such IDs.

Osprey, thank you very much for your help! :thumbup:

One last question concerning the #Include command which I already posted in the Pulover’s Macro Creator thread and remained unanswered: Does anyone know if it’s possible to use the #Include command with Pulover's Macro Creator? I can’t find this command.