Environment Variables in Pascal
Here’s the Pascal translation of the environment variables example:
Our program demonstrates how to set, get, and list environment variables in Pascal.
To set a key/value pair, we use SetEnvironmentVariable
. To get a value for a key, we use GetEnvironmentVariable
. This will return an empty string if the key isn’t present in the environment.
To list all key/value pairs in the environment, we use GetEnvironmentVariables
which populates a TStringList
with all environment variables. We then iterate through this list to print all the keys.
Running the program shows that we pick up the value for FOO
that we set in the program, but that BAR
is empty.
The list of keys in the environment will depend on your particular machine.
If we set BAR
in the environment first, the running program picks that value up. In Pascal, you can set an environment variable before running the program like this:
Note that the exact method of setting environment variables before running a program may vary depending on your operating system and Pascal compiler.