Arithmetic with Windows Batch Process Programming

So you have heard that it’s impossible. There is simply no way to get a Windows batch process program, or any Windows shell function for that matter, to perform advanced arithmetic.

And though it’s true that there is no DIRECT way to perform advanced mathematical functions within the shell, it IS possible by integrating one of the shell’s best friends – Visual Basic Scripting. In this series, we’ll use Windows batch process programs to create and execute Visual Basic Scripts, which will hold the information key to our calculations.

Introduction

In this lesson, we will be utilizing the write log commands within the Windows shell. This series of commands will allow us to write and log new Windows Visual Basic Scripts containing arithmetic expressions.

We will then execute said scripts through the same batch process and echo the results. In other words, we will have the batch program collect variables from the user, apply those variables within expressions for a VBS file to execute later and relay the results back to the shell.

Gathering Variables

To collect the variable table we will need to utilize the SET /P command within the shell. This command will prompt a user with a message, and correspond the response to a preset variable. For example:

SET /P var= Hello, how are you?

Now, the user response is correspondent to the variable var . For instance, if the user were to have entered “GOOD”, then var=GOOD. What we will be using these command lines for is gathering variables. For example, if we are trying to find the sum of two numbers, then we will need to collect the desired value of those numbers first, like so:

SET /P VALUE1= Please enter the first variable:

SET /P VALUE2= Please enter the second variable:

Configuring The Batch Program To Write In VBS

What we’ll essentially be doing is having our batch program write and execute a valid VBS file by expressing ECHO statements linearly. In other words, if we want the VBS to contain something that looks like this:

HELLO THERE!

HOW ARE YOU!

I AM FINE!

then we will need to configure our output program to look something like this:

ECHO HELLO THERE!  gt; test.vbs

ECHO HOW ARE YOU!  gt; gt; test.vbs

ECHO I AM FINE!  gt; gt; test.vbs

Obviously, this is not the authentic way in which to configure a VBS file, but this should help demonstrate the concept. Notice how each line requires its own ECHO statement and output location. This method of configuration is known as; linear programming, or line programming.

It means that, instead of programming objects, we’re programming the shell to read the code line for line in order. It’s integral to conduct the output program in the correct fashion because there is a notable incompatibility -VBS is object-oriented. This means that; as shell functions read and write in a linear fashion, VBS identifies properties of script classes called objects and does not necessarily read the code in it’s ordered path.

In the next example, we will actually build a simple program that is designed to write functions to our VBS file labeled test.vbs. Notice the variable input set by the command SET /P. This command will help gather the initial information necessary for the VBS file to perform the equation.

@ECHO OFF

:INPUT

SET /P comp1= Enter the value of the first variable?

SET /P comp2= Enter the value of the second variable?

 

:OUTPUT

ECHO comp1= %comp1%  gt; test.vbs

ECHO comp2= %comp2%  gt; gt; test.vbs

ECHO equ= comp1 + comp2  gt; gt; test.vbs

ECHO wscript.echo equ  gt; gt; test.vbs

 

:EXECUTION

start test.vbs

 

:END

exit

 

After each of the ECHO commands, notice the code lines configured for VBS. Each of these lines will be written to a VBS file named test.VBS using the input variables gathered in the:INPUT module, then executed by the START test.VBS command. The completed VBS file would contain code that looks like the following:

 

comp1= 2

comp2= 2

equ= comp1 + comp2

wscript.echo equ

 

This program is designed to take the variables comp1 and comp2, established within the BATCH file, find their sum (equ=comp1+comp2) , label the sum EQU and VBS ECHO (wscript.echo equ) the value of the sum variable EQU. In other words, we’ve enabled the shell to gather all of this important information, and relay it to the VBS file to execute. Do note that this lesson does not demonstrate the use of advanced arithmetic, but that future lessons will.

 

In future lessons, we will go over the various ways in which to ECHO sum variables within the shell and apply different modes of advanced mathematical logic to them, but all of that is a bit above our heads right now. This process can become so powerful within a batch process program, that there are literally no boundaries between it and other programming languages.

With this technique, there is truly no limit to what one can do with BATCH. I recommend experimenting with this material, and getting more familiar with VBS and batch process programming so that you may one day make something great! 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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