ENVMATH.COM - from Slashback Software - http://www.SlashbackSoftware.com I. Introduction ENVMATH.COM is a DOS utility which performs math on DOS environment variables. ENVMATH does not alter its own copy of the environment, but the environment of the program which started it. When run from a batch file or at the command line, it changes the DOS environment owned by that instance of COMMAND.COM. ENVMATH.COM is shareware (see LICENSE.TXT). You may evaluate it for 30 days. If you decide to keep it, you must register it by sending $10 US check or money order to: John Homerstad 460 E. Bryan Avenue Salt Lake City, Utah 84115 II. Purpose Environment variables can be used in a number of ways. For example, programs can look for specific variables to set default paths. Within DOS batch files, singly or in combination, they can be tested using the IF command, or used to pass parameters. See Appendix B for more information. ENVMATH was first desired to test a DOS database program which took a series of numeric command line parameters to determine function, record, and data. Within batch files, it can control loops and branching; programatically control delays (using the CHOICE command); or, in conjunction with ENVSAVE.COM in AUTOEXEC.BAT, count the number of times the computer is booted... III. Use Perhaps the best way to understand how to use ENVMATH is to run and study the included DEMO.BAT; however, following is a more detailed description. ENVMATH uses integers from 0 through 65535. Variables can be assigned values, incremented, decremented, added, or subtracted. Two values can be compared. Five operators are utilized: = assignment + increment/add - decrement/subtract : compare # immediate (i.e., a number follows, not a variable name). Following the pattern of the SET command, ENVMATH changes the value of one variable at a time. The name of the variable altered begins with the first non-space character on the command tail and is followed by an equal sign; variable names may contain spaces and tabs (see Appendix A). A given variable name cannot appear on both the right and left sides of the equal sign. The immediate operator indicates that a number follows; without it, ENVMATH searches for a variable with that number as its name. It only works on the right side of the equal sign, and acts just as the value of a variable. Variable values and immediate numbers are evaluated as follows: ASCII characters 1 through 9 indicate their place value; other characters are considered zero: #1abc2 equals 10002, #abc12 equals 12, #a1b2c equals 1020. Any variable names on the right side of the equal sign must already exist in the environment. Zero, one, or two variable names may be on the right side of the equal sign. The following functions are supported: Assignment: ENVMATH varname=varname1 Delete: ENVMATH varname= Increment: ENVMATH varname=+ Decrement: ENVMATH varname=- Assign & Add: ENVMATH varname=varname1+varname2 Add to: ENVMATH varname=+varname1 Assign & Subtract: ENVMATH varname=varname1-varname2 Subtract from: ENVMATH varname=-varname1 Assign & Increment: ENVMATH varname=varname1+ Assign & Decrement: ENVMATH varname=varname1- Compare: ENVMATH varname=varname1:varname2 An immediate number can be used for any varname1 or varname2. The only time a variable is deleted from the environment is using the syntax above; if the variable's value reaches zero, for instance, it still exists. Note, however, that if an error occurs, results are undefined: the variable may have a nonsense value or may have been deleted. Finally, the Compare operation sets the variable to one of three strings: LT (Less Than), EQ (Equal), or GT (Greater Than). Again, the simplest way to understand all this is to observe the included DEMO.BAT. APPENDIX A: THE DOS EVIRONMENT When an instance of COMMAND.COM is started, it reserves a block of memory for environment variables. The size of the environment (bytes of memory allocated) can be specified with the /E:### switch when starting the DOS session. Every program run from this DOS prompt receives its own copy of the DOS environment; if it alters this environment, changes are lost when the program terminates. Each entry in the environment is a string of characters including a name, an equal sign (=), and a value, followed by a null byte (ASCII zero). The final entry is followed by a second null byte. Any additional memory allocated to the environment beyond the pair of null bytes is undefined. DOS includes the SET command to allow variables names to be assigned values, but offers no manipulation beyond defining and deleting. The SET command always forces the letters of the variable name to upper case, but the value is not altered. Most characters can be used in the name or value; those not allowed include certain control characters (i.e., null, Ctrl-C, backspace, line feed, carriage return, Ctrl-P, Ctrl-S, Ctrl-T, Ctrl-Z), the equal sign (used as separator between name and value), and the DOS file redirection and piping characters (<, >, |). Note that these characters could exist in the environment block if a program desired it, but as SET and IF cannot use them, results would be unpredictable. Interestingly, when Windows starts a DOS session, it creates an environment variable named "windir" in lower case; it is essentially invisible to DOS. APPENDIX B: VARIABLES IN DOS BATCH FILES Within DOS batch files, the value of an environment variable can be referenced by its name surrounded by percentage signs (i.e., %varname%). This value can be used in any place on a command line; it could be the name of a program or command, or a parameter passed to a program or command. For example: SET MY PROGRAM NAME=DIR SET A PARAMETER=*.DOC %MY PROGRAM NAME% %A PARAMETER% would result in DOS running the following command: DIR *.DOC The IF command tests two strings to see if they are identical: SET MYVARNAME=Aboringstring IF %MYVARNAME%==Aboringstring goto end ECHO They didn't match. :end The comparison is case sensitive. IF NOT can be used to find strings which do not match. Any valid command line can follow the comparison. Variable names and values may contain spaces, but apparently IF only compares up to the first space. Note that if command line parameters are passed to a batch file, they are referenced by %1, %2, etc. Both environment variables and command line parameters may contain the percentage sign, but the value replacements are done once per batch file line, so they cannot refer to other parameters or variables.