This is useful in, for example, a FOR loop. As in the following example, where we have a numbered list of variables:. Note that the variable! This is double expansion of the variable i. Thus it increases to 2 on the 2nd pass through the loop, and to 3 on the 3rd pass. Thus the string echoed to the screen alters with each pass. When multiple variables are defined at the beginning of the batch, a short definition form may be used by employing a replacement string. It is possible to create a set of variables that can act similar to an array although they are not an actual array object by using spaces in the SET statement:.
It is also possible to declare your variable using indexes so you may retrieve specific information. If the variable you use is not defined you will get empty string when the batch file is running.
From the command line you get the literal e. When variables store numeric values, you can perform operations with them. Batch commands support grouping, arithmetic, logical, assignment operators and more. All these operators give you the ability to keep count of things.
When you use arithmetic expressions, you can avoid using the percent signs to denote variables. That means that the following 2 expressions are equivalent:. So what happens if a variable does not store a numeric value? Then the value is replaced by zero. If you are using arithmetic expressions involving any of these characters, use double quotes to avoid confusions.
Like this:. Without the quotes you get an error when running this command. The if command provides some operators to compare numeric variables. An example on how to use these operators can be. When variables hold strings, they can be used to save values, folder names, etc. They can be used in comparisons just like numbers, and denoted with replacements too.
Another interesting point is that variables can store parts of instructions to be executed. This can be useful when you have a value entered by the user, and based on it define where the code should execute, as this:.
This makes the value of variables more powerful and versatile than in other languages. Complete instructions can be executed this way, as follows:. These 2 instructions will result in the string being echoed. This makes environment variables even more powerful than in some other languages. You can create code on the fly and execute it. Pretty cool.
You will know why as we move on further. I can use either a number or a string in the place of value like as shown below. Like all other languages, we also have certain restrictions on the variable name that the names must not be built in commands etc. You just need to follow them while creating a variable. To print a message on the command prompt we already know that we use ECHO command. But, it assumes that myVar is a string that needs to be echoed back on the console.
The above program prints the value of myVar on the command prompt. Give it a try and observe it. Now let us write a program to carry out addition of two variables. But that is not the case here. Following is an example of a custom-defined function for seeing the length of a string. The following example shows how this can be accomplished.
This used to align text to the right, which is normally used to improve readability of number columns. Batch Script - Strings Advertisements. Previous Page. Next Page.
0コメント