Home > Articles > Home & Office Computing > Microsoft Windows Desktop

  • Print
  • + Share This
This chapter is from the book

Working with Environment Variables

Although environment variables were initially designed to hold system-configuration information such as the search path, they are also the "working" variables for batch files. You can use them to store filenames, option settings, user input from prompts, or any other information you need to store in a batch program. Environment variables were covered in Chapter 11. In the discussion of environment variable substitution, the set command was introduced as the way to set and modify environment variables.

However, you should know that, by default, changes to environment variables made in a batch file persist when the batch file finishes, because the variables "belong" to the copy of CMD that manages the Command Prompt window and any batch files in it. This is great when you want to use a batch file to modify the search path so that you can run programs from some nonstandard directory. However, it's a real problem if your batch file assumes that any variables it uses are undefined (empty) before the batch file starts. Here's a disaster waiting to happen:

@echo off 
set /p answer=Do you want to erase the input files at the end (Y/N)?
if /i "%answer:~,1%" EQU "Y" set cleanup=YES
... more commands here
... then, at the end,
if "%cleanup%" == "YES" (
    rem they wanted the input files to be erased
    del c:\input\*.dat
)

If you respond to the prompt with Y, the environment variable cleanup will be set to YES, and the files will be erased. However, the next time you run the batch file, cleanup will still be set to YES, and the files will be erased no matter how you answer the question. Of course, the problem can be solved by adding the statement

set cleanup= 

at the beginning of the batch file. In fact, good programming practice requires you to do so in any case (you should always initialize variables before using them), but the point is still important: Environment variables are "sticky."

In the old DOS days, a batch file program would usually add set statements to the end of batch files to delete any environment variables used by the program. However, CMD provides an easier method of "cleaning up."

If you plan on using environment variables as working variables for a batch file, you can use the setlocal command to make any changes to the variables "local" to the batch file. At the end of the batch file, or if you use an endlocal command, the environment will be restored to its original state at the time of the setlocal command. It would be prudent to put setlocal at the beginning of any batch file that does not require its environment changes to persist outside the batch file itself.

Environment Variable Editing

As with the old COMMAND.COM, in any command, strings of the form %var% are replaced with the value of the environment variable named var. One of CMD's extensions is to let you modify the environment variable content as it is being extracted. Whereas the edits for command-line arguments are focused around filename manipulation, the edits for environment variables are designed to let you extract substrings.

The following types of expressions can be used:

Expression

Result

%name:~n%

Skips the first n letters and returns the rest

%name:~n,m%

Skips n letters and returns the next m

%name:,m%

First (leftmost) m letters

%name:~-m%

Last (rightmost) m letters

Using the environment variable var=ABCDEFG, here are some examples:

Command

Prints

echo %var%

ABCDEFG

echo %var:~2%

CDEFG

echo %var:~2,3%

CDE

echo %var:,~3%

ABC

echo %var:~-3%

EFG

Expressions of the form %name:str1=str2% replace every occurrence of the string str1 with str2. Str2 can be blank to delete all occurrences of str1. You can start str1 with an asterisk (*), which makes CMD replace all characters up to and including str1.

Using the environment variable var=ABC;DEF;GHI, here are some examples:

Command

Prints

echo %var:;= %

ABC DEF GHI

echo %var:;=%

ABCDEFGHI

echo %var:*DEF=123%

123;GHI

The first example listed is particularly useful if you want to use the PATH list in a for loop; for wants to see file or folder names separated by spaces, whereas PATH separates them with semicolons. I'll discuss this in more detail later on.

For more details on working with environment variables, see "Setting Variables with the Set Command," p. 476.

  • + Share This
  • 🔖 Save To Your Account