• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 12. Batch Files for Fun and Profit > Working with Environment Variables

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:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint