Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to Access Environment Variables in an MS-DOS Batch File


Summary

The article explains how you can access environment variables within MS-DOS batch files.

↑ Back to the top


More Information

The following example shows how to test for the existence of an environment variable:


IF "%VARIABLE%" == "" GOTO MODULE
This example checks for the existence of the environment variable "VARIABLE." If VARIABLE does not exist, the statement is true and control of the batch file jumps to the MODULE section. If the statement is not true, that is, the VARIABLE variable does exist, program control skips to the next line of the batch file and continues.


NOTE: The quotation marks are necessary only when a variable is used in a logical IF statement.


The following example illustrates the use of the PATH environment variable within a batch file:


IF "%PATH%" == "" GOTO NOPATH
:YESPATH
@ECHO The PATH environment variable was detected.
PATH=C:\DOS;%PATH%
GOTO END
:NOPATH
@ECHO The PATH environment variable was NOT detected.
PATH=C:\DOS;
GOTO END
:END
This example tests for the existence of the PATH variable. If it exists, control passes to the YESPATH section; otherwise, control is passed to the NOPATH section.


If a path is detected and control is passed to the YESPATH section, C:\DOS is added to the existing path by using the %PATH% variable.

↑ Back to the top


Keywords: kb, kbbillprodsweep

↑ Back to the top

Article Info
Article ID : 121170
Revision : 4
Created on : 8/20/2020
Published on : 8/20/2020
Exists online : False
Views : 190