ENVIRON Sets Environment Variables Used by SHELL in QuickBasic (43691)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0

This article was previously published under Q43691

SUMMARY

The ENVIRON statement in Basic can change the MS-DOS environment variables to different values to be used by that program or a child process invoked with the SHELL statement. When the program ends, the MS-DOS environment variables revert to the same state as before the program ran.

This information applies to Microsoft QuickBasic versions 4.00, 4.00b, and 4.50 for MS-DOS; Microsoft Basic Compiler versions 6.00 and 6.00b for MS-DOS and MS OS/2; and Microsoft Basic PDS version 7.00 for MS-DOS and MS OS/2.

MORE INFORMATION

The ENVIRON statement allows you to alter the environment variables for use by child processes started through Basic. Once set, these variables will remain set to the new values as long as you are in your Basic program or SHELL out to a child process.

Basic makes a copy of the original DOS environment variables, and the local copy can be changed with the ENVIRON statement. These changes to the temporary copy of the DOS environment variables remain in effect when Basic SHELLs to another process.

When altering the environment table through Basic, it is common for an "OUT OF MEMORY" error message to be displayed. For more information on the workaround for this error, query using the following words:

ENVIRON$ Memory QuickBasic

The following code example will print out the current copy of the environment table, alter the PATH and PROMPT environment variables, print out the environment table again, then SHELL and perform the DOS SET command:

Code Example

CLS
i = 1

DO WHILE ENVIRON$(i) <> ""   ' prints Basic's environment table
    PRINT ENVIRON$(i)
    i = i + 1
LOOP

ENVIRON "PATH=;"              ' clear the PATH environment variable
ENVIRON "PATH=C:\DOS"         ' set the PATH environment variable
ENVIRON "PROMPT=;"            ' clear the PROMPT environment variable
ENVIRON "PROMPT=$P$G"         ' set the PROMPT environment variable

PRINT : PRINT

i = 1
DO WHILE ENVIRON$(i) <> ""    ' prints Basic's environment table
    PRINT ENVIRON$(i)
    i + i + 1
LOOP

PRINT : PRINT
SHELL "set"   ' prints the environment table in Basic's child process
END
				

How to Make Basic Change Environment Variables After Exiting

To make the Basic program change the DOS environment after ending, you can perform the following steps using MS-DOS batch files:

  1. Invoke your QuickBasic program from an MS-DOS batch file.
  2. At run time, your QuickBasic program can create a text file containing MS-DOS batch commands to SET environment variables.
  3. After the QuickBasic program ends and returns control to the batch file, the batch file can use the batch Call statement to invoke the batch (text) file created by the QuickBasic program.

Modification Type:MinorLast Reviewed:1/9/2003
Keywords:KB43691