Q(uick)BASIC Statement: ENVIRON

Quick View

ENVIRON

A statement that interfaces with DOS to modify a parameter in the DOS environment-string table

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • ENVIRON$ (env-variable$)
  • ENVIRON$ (n%)
  • ENVIRON stringexpression$
Description/Parameter(s)
env-variable$ The name of a DOS environment variable.
n% Specifies that ENVIRON$ returns the nth string from the environment string table.
stringexpression$ The name and setting of a DOS environment variable (such as PATH or PROMPT) in one of the following forms:
  • env-variable$=env-string$
  • env-variable$ env-string$

Changes made by the ENVIRON statement are erased when the program ends.

Example
ENVIRON "PATH=TEST" PRINT ENVIRON$("PATH")
Syntax
  • ENVIRON stringexpression
Description/Parameter(s)

The stringexpression must be of the form

  • parameterid=text

or the form

  • parameterid text

Everything to the left of the equal sign or space is assumed to be a parameter, and everything to the right, text.

If the parameterid has not previously existed in the environment- string table, it is appended to the end of the table. If a parameterid exists in the table when the ENVIRON statement is executed, it is deleted and the new parameterid is appended to the end of the table.

The text string is the new parameter text. If the text is a null string ("") or a semi-colon (";"), then the existing parameter is removed from the environment-string table and the remaining body of the table is compressed.

DOS discards the environment-string table modified by this function when your program ends. The environment-string table is the same as it was before your program ran.

You may use this statement to change the PATH parameter for a "child" process (a program or command started by a SHELL statement) or to pass parameters to a child by inventing a new environment parameter.

Errors in environment-string tables include parameters that are not strings and lack of free space. An Out of memory error message is printed when no more space can be allocated to the environment-string table. The amount of free space in the table is usually quite small.

Example

This example uses the ENVIRON statement to change the setting of the PATH environment variable for the current process to the sales and accounting directories on the A: drive:

' Change value of PATH environment variable. ENVIRON "PATH=A:\SALES;A:\ACCOUNTING"
Syntax
  • ENVIRON stringexpression$
Description/Parameter(s)

stringexpression$ has one of these forms:

  • parameterID = text

or

  • parameterID text
parameterID The name of the environment parameter (such as PATH or PROMPT). The parameterID must contain all uppercase letters.
text The new parameter text.
  • Everything to the left of the equal sign or space is assumed to be a parameter; and everything to the right, text. The following example sets a PATH environment variable:

  • ENVIRON "PATH=C:\CLIENTS;C:\RATES"

  • If parameterID did not previously exist in the environment-string table, it is appended to the end of the table. If parameterID exists in the table when the ENVIRON statement is executed, it is deleted and the new parameterID is appended to the end of the table.
  • If text is a null string ("") or a semicolon (";"), the existing parameter is removed from the environment-string table and the remaining body of the table is compressed.

Important

  • You cannot increase the size of the environment-string table when using the ENVIRON statement. This means that before you can add a new environment variable or increase the size of an existing environment variable, you must first delete or decrease the size of existing environment variable(s).
  • BASIC generates the error message, "Out of memory" when no more space can be allocated to the environment-string table. The amount of free space in the table is usually quite small.
  • The parameterID must contain all uppercase letters. For example:
This statement Has this effect:
ENVIRON "PATH=C:ALES" Changes the path.
ENVIRON "path=C:ALES" Does not change the path (it creates a new environment variable not usable by the operating system).

Usage Notes

  • DOS or OS/2 discards the environment-string table modified by this function when your program ends. The environment-string table is then the same as it was before your program ran.
  • You can use this statement to change the PATH parameter for a "child" process (a program or command started by a SHELL statement) or to pass parameters to a child by creating a new environment variable.
  • If the parameterID did not previously exist in the environment- string table, it is appended to the end of the table. If a parameterID exists in the table when the ENVIRON statement is executed, it is deleted and the new parameterID is appended to the end of the table.
  • If the text is a null string ("") or a semi-colon (";"), the existing parameter is removed from the environment-string table and the remaining body of the table is compressed.
Example

This example uses the ENVIRON$ function to get a copy of your current PATH variable. Your PATH is then changed using the ENVIRON statement. The program then uses the ENVIRON$ function to print out the contents of the environment string table. The example concludes by restoring your original PATH statement.
Note: For this example to run, the value of your path variable must be longer than the new path value in the ENVIRON statement.

DEFINT A-Z 'Initialize. PATH$ = "PATH=" I% = 1 'Get the old PATH. OldPath$ = ENVIRON$("PATH") ENVIRON "PATH=C:\BIN;C:\DOS;C:\BUSINESS" 'Display the entire environment string table. PRINT "Your current environment settings are:" PRINT DO WHILE ENVIRON$(I%) <> "" PRINT ENVIRON$(I%) I% = I% + 1 LOOP 'Change the PATH back to original. ENVIRON PATH$ + OldPath$ 'Verify the change. PRINT PRINT "Your PATH has been restored to:" PRINT PRINT ENVIRON$("PATH")