Batch SET Command - chrisbitm/python GitHub Wiki

The SET command is used to manage Environment Variables in Windows. These Variables are useful for storing and manipulating Data during Script Execution. However, it is used as a one time instance. This means that

Basic Syntax

SET [variable=[string]]

  variable  Specifies the environment-variable name.
  string    Specifies a series of characters to assign to the variable.

Listing the Environment Variables

There are two ways to display the various Environment Variables on your System.

Metod 1

This Method will display all Variables in Alphabetical Order.

SET

Method 2

Looking through the list of the various Environment Variables can be overwhelming and can strain the eyes. If you know the First Letter of what the Variable you are trying to search for starts with, it will list all the Variables starting with that Letter.

SET C
  • This will Display all the Variables starting with the Letter C
SET B
  • If you choose a Letter that is not in the list. You will see this Prompt

image

Creating a Variable

You can temporarily declare a Variable in a Shell Session.

SET hobbies=Games

Override

You can override a Variable. All you need to do is change the String when typing the Command in the prompt window.

SET hobbies=Checkers

String Concatenation

SET hobbies=%hobbies% and Poker

Prompt for User Input

SET /p hobbies=What are your Hobbies?:  

Manipulate Strings

Using SET with parameters like /A allows for basic arithmetic:

SET /A result=5+3
ECHO %result%

Conditional Variable Expansion

Use SET with string substitution:

SET text=Game, Start
SET text=%text:Start=End%
ECHO %text%
  • This replaces Start with End in the String passed to text Environment Variable.
  • In the Second Line, the : signifies what word to be replaced. The Exact Text is replaced the Text after the = sign

You cannot use the Environmental variable by itself in Console. You will generate an error if you do. The reason as to why this occurs because when you directly type %greeting% into the console, it evaluates the variable and substitutes its value hello. The Console then tries to execute hello as if it were a command or program, but since hello isn't a valid command or executable, it raises the error.

image