I need to find a way to reference environment variables INSIDE the Inno Setup script file (.iss)...
I've found plenty of references to MODIFYING the environment from an .iss, but none on how to actually use it. Is this possible?
According to this page in the Inno Setup documentation, the value of environment variables can be retrieved using the following syntax:
{%name|default}
The syntax is different, if you want to resolve the variable on install-time or on compile-time. That's why there are two existing answers that show completely different solutions that work for some and not others. Because different readers look for different things here.
If you need to resolve the variable on the target machine, while installing, you can use the {%NAME|DefaultValue}
"constant".
[Files]
Source: "MyApp.dat"; Dest: "{%MYAPP_DATA_PATH|{app}}"
If you need to resolve the variable on the target machine in Pascal Script code, you can use GetEnv
support function.
Path := GetEnv('MYAPP_DATA_PATH');
If you need to resolve the variable on the source machine, while compiling the installer, you can use GetEnv
preprocessor function:
[Files]
Source: "MyApp.dat"; Dest: "{#GetEnv('MYAPP_DATA_PATH')}"
You can use the same syntax even in Pascal Script, though it would make sense only in very special circumstances.
Path := '{#GetEnv('MYAPP_DATA_PATH')}';
I couldn't figure out how to use the {%name|default}
syntax, so this is how I implemented the same (I needed to specify a default value when the env var is not present):
#if GetEnv('EXTRA_FILE_LOCATION') != ""
#define EXTRA_LOCATION=GetEnv('EXTRA_FILE_LOCATION')
#else
#define EXTRA_LOCATION="."
#endif
Source: {#EXTRA_LOCATION}\ExtraFile.data; DestDir: {app};