Tuesday , 2 March 2021
Breaking News
Home » Software » Arithmetic with Windows Batch Process Programming
Arithmetic with Windows Batch Process Programming

Arithmetic with Windows Batch Process Programming

Mathematical operations are essential functions in Windows batch process programs, and though, in batch commands lines are not used with great frequency, still have the ability to create the opportunity for magnificent function. Every programming language has some form of mathematical operation, but few have access to as powerful and functional resources located within a Windows file system like batch process programs do. In this lesson, we’ll go over the most basic of command-line arithmetic including addition, subtraction, multiplication and division, and go over some of the ways in which they can be used in this environment. Want me to do your computer science homework? Read on!

Addition and Subtraction

To apply simple expressions such as addition and subtraction to our script, we will need to utilize the SET /A command. This command literally tells the shell to recognize the variable (set by “A”) as a number or variable arithmetic expression. Once again, this will equate an actual variable name to the number value. This can get tricky, but by making sure that each variable is named after the value in which it is meant to represent can make this process much more organized. For instance, a variable that is mean to represent the sum of two numbers would be most easily recognized as “SUM”, and the two values that it consists of: “value1” and “value2”. Using this technique will ensure that your scripts stay focused and organized.

Easily enough, the characters that we will be using to represent these interactions are the standard + and – . The shell is also capable of recognizing and handling negative integers just as it would subtractive interactions, meaning that: 5-5 can also be written 5+(-5). Since the shell does follow the standard order of operations, it is important to note that parentheses will be required for more complex interactions like the example above.

To get started, simply start your command-line with the SET /A command followed by the variable name and the expression. For example:

SET /A sum=2+2

Note that the variable sum does NOT equal 4, but is meant to represent the interaction between the two twos, however, ECHO-ing the variable sum will still present the message: “4”. I’ll say that one again – sum DOES NOT equal 4, but equals 2+2. It is almost the same thing and will yield the same result, but as we get into more intense mathematical expressions, it is important to understand.

To subtract two numbers, apply the same logic like so:

SET /A difference=2-2

Again, this is a very simple concept, and extremely easy to implement, however, with that being said – there are a few things that are important to know. Setting a variable with the switch “A” will not gather user input. The “A” switch is designed only to perform arithmetic function. In order to gather input from a user, the “P” switch will need to be utilized. Note that with the “P” switch, another variable name will be gathered, so using that value in an expression will constitute a ‘call of variable’, like so:

SET /P var=What is the number?

SET /A sum=%var%+2

Instead of being able to use a numeric value, we were forced to call the variable by using the % (percentage sign). After, and only after this exchange can the variable sum be called or ECHOed, because again – sum doesn’t actually equal the sum, but is meant to represent the interaction between the variable and the number 2 (and thus can’t be called into another expression).

Multiplication and Division

We can apply the same basic logic to expressions that require division or multiplication, though interactions can become more complex and often braided when we do. It’s important to REALLY apply the “parentheses rule” to any expression involving either of the two, because once again – the shell follows the basic order of operations, and unordered expressions can often have undesired results. Take a look at the following example and notice the way in which the expression was organized.

SET /A product=((4*5)*10)

Note that the shell would have calculated the inner set before multiplying the product by 10, giving us the answer 200. Also note that instead of the good, old-fashioned “X” to represent the multiplicative interaction, we are using the * (star) character. The X value will just confuse the shell – trust me!

To demonstrate a divisive interaction, we will use the “/” character, like so:

SET /A quotient=(20/5)

This won’t get much more complicated, but will yield a great deal to remember. When using mathematics in batch process programs, its easiest just to remember these three simple rules:

-Use the SET /A command to perform routines

-Use the SET /P command to gather data for routines

-Always use the “parenthesis rule” and outline expressions well

With that being said, it’s important for future lessons that you are well-versed in algebra. Be sure to brush up on some of the basic functions of algebra, for they will be most helpful in performing more advanced commands in the future. A great (and free) site to go about doing so is: http://ocw.mit.edu/OcwWeb/Mathematics/18-06Spring-2005/CourseHome/

a linear algebra course taught by Professor Gilbert Strang. As always, make sure to experiment with this material – arithmetic within language structures like this can produce extremely functional programs! And as always – have fun and happy programming!

Thanks for reading, and don’t forget to continue on to learn more about command-line and batch process programming by referring to the Arithmetic With Windows Batch Process Programming series.

About Emma Gilbert

Working in the marketing industry since 2002. This blog is one of my hobbies.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.