28

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?

mghie
  • 32,028
  • 6
  • 87
  • 129
dicroce
  • 45,396
  • 28
  • 101
  • 140

6 Answers6

20

I ran into the same problem when trying to specify the source location of files in the [Files] section. I used the GetEnv function to define a new constant.

#define Qt5 GetEnv('QT5')
[Files]
Source: {#Qt5}\bin\Qt5Concurrent.dll; DestDir: {app}; 
goran
  • 411
  • 4
  • 6
  • 1
    this is the correct way to use in defines. the {%name|default} notation isnt working there. – pHiL Jan 07 '14 at 22:18
18

According to this page in the Inno Setup documentation, the value of environment variables can be retrieved using the following syntax:

{%name|default}
Nathan Osman
  • 71,149
  • 71
  • 256
  • 361
  • 1
    I am unable to use the env variables for some reason. Can someone post an example? I added {%OS} to the output file name and all I get back is the original file name with the string "{%OS}" appended - not the value I want from the environment. – Tim Jun 13 '10 at 04:45
  • 3
    @Tim: If you want to use the variable in a string inside your [code], use ExpandConstant('{%OS}') – panschk Nov 20 '10 at 16:12
  • Syntax is different,if you want to resolve the variable on install-time or on compile-time. See [my answer](https://stackoverflow.com/q/2002247/850848#54286490) for details. -- As for the `ExpandConstant` - it's an overkill, there's `GetEnv`. Also noted in my answer. – Martin Prikryl Jan 21 '19 at 09:53
16

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.


On install-time

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');

On compile-time

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')}';
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 1
    This is actually more helpful than the currently accepted answer, because the compile time and installation time distinction is important in some cases. – Striezel Aug 29 '22 at 00:27
2

It can be even easier:

OutputDir={#GetEnv("TEMP")}\
1

If the variable TEMP does not exist then the default value will be used - ..\..\distr\ ))))

OutputDir={#StringChange(GetEnv("TEMP")+"\", StringChange(GetMD5OfString(GetEnv("TEMP")), "d41d8cd98f00b204e9800998ecf8427e", "\"), "..\..\distr\")}
0

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};
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
  • 1
    To explain the difference: `{%name|default}` resolves on **run time** on the **target** machine. While you code resolves the variable on **compile** time on the **build** machine -- See also my comments to the answers by @МаксимРумянцев. – Martin Prikryl Jan 20 '19 at 12:09