Q(uick)BASIC Function: ENVIRON$

Quick View

ENVIRON$

A function that interfaces with DOS to retrieve an environment string from 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$ (environmentstring)
  • ENVIRON$ (n)
Description/Parameter(s)

The environmentstring is a string constant or variable containing the name of an environment variable. The argument n is a numeric expression.

If you specify an environmentstring name, but it cannot be found in the environment-string table, or there is no text following it, then ENVIRON$ returns a null string. Otherwise, ENVIRON$ returns the text following the equal sign in the environment-string table.

If you specify a numeric argument (n), the nth string in the environment-string table is returned. In this case, the string includes all of the text, including the environmentstring name. If the nth string does not exist, ENVIRON$ returns a null string. The n argument can be any numeric expression; it is rounded to an integer.

Example

This example uses the ENVIRON$ function to print the current settings in the environment-string table:

CLS ' Clear screen I = 1 DO WHILE ENVIRON$(I) <> "" PRINT ENVIRON$(I) I = I + 1 LOOP

Sample Output:

COMSPEC=C:\COMMAND.COM TMP=c:\tmp PATH=C:\TOOLS;C:\BIN PROMPT=$ec4$l$ec7$p$ec4$g$ec7$eb1 INIT=c:\tools LIB=c:\lib INCLUDE=c:\include
Syntax
  • ENVIRON$ (environmentstring$)
  • ENVIRON$ (n)
Description/Parameter(s)
  • The argument environmentstring$ is a string constant or variable that contains the name of an environment variable. The name of the environment variable must be uppercase. For example, ENVIRON$ ("PATH") returns the PATH environment variable; ENVIRON$ ("path") returns a null string.

Usage Notes

  • If you specify an environment-string name, but it cannot be found in the environment-string table, or there is no text following it, then ENVIRON$ returns a null string. Otherwise, ENVIRON$ returns the text following the equal sign in the environment-string table.
  • If you specify a numeric argument (n), the nth string in the environment-string table is returned. In this case, the string includes all of the text, including environmentstring$. If the nth does not exist, ENVIRON$ returns a null string. The n argument can be any numeric expression; it is rounded to an integer.
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")