Teaching DOS About Numbers
This is a topic that is still pretty hot on the 'net, even as DOS usage diminishes (or is it?). Probably because Windows does not offer this ability either. So it just seems right that DOS should provided some minimum support of numbers. I have seen a number of ingenious attempts to use numbers, but they're all so complex that it takes a genius (or the author) to understand them. As a member of the latter group (author), I've even cobbled together a few routines, but was never paticularly happy with my efforts. The results were not only complex, but very slow as well.

For my most recent effort, you may want to check out the routines that add, subtract, increment and decrement numbers. I have put the most effort into describing the adding routine, so I'ld suggest you start there. It also contains references to the subtraction routine, a new approach to string parsing, a new comparison routine and routines to increment and decrement a number.

UPDATE - I have an even better way to count in DOS. You may want to read about numbered sequences first.
Yet, I didn't stop trying and a while back an approach came to me that uses the /C switch for the FIND utility. This opened a whole new vista of number manipulations in batch files. In a nutshell, the /C switch counts the number of lines that match a template string in the FIND command. It literally counts the linefeed characters (10 decimal, 0A hex).

To count then, one need only add one line at a time to a file and count the number of lines after each increment, something like this ...

::COUNTER.BAT - An example of one way to count in a batch file. :: Tom Lavedas <lavedas@pressroom.com> :: http://www.pressroom.com/~tglbatch/ @echo off &gt; {c} if [%1]==[] for %%v in (echo goto:End) do %%v Syntax: %0 Number :Loop &gt;&gt;{c} echo ! &lt; {c} find /c "!" &lt; {c} find /c "!" | find "%1" &gt; nul if errorlevel 1 goto Loop {This requires DOS version 6.x or higher} :End del {c}
COUNTER is executed by typing its name followed by a number greater than zero at the DOS command prompt. All it does is show all the numbers, one number to a line, from one to the number limit supplied as input on the command line.

Now, if all you want to do is count, the previous example is about as simple as it gets. However, in most situations where I want to count, I also want access to the value of the count at each iteration.

Getting access to the value of the count requires some changes in the logic and just two more lines of code, as shown below.

:: LOOP.BAT - An example of a counted loop in a batch file. :: Tom Lavedas <lavedas@pressroom.com> :: http://www.pressroom.com/~tglbatch/ @echo %dbgc% off &gt; {c} if [%1]==[] for %%v in (echo goto:End) do %%v Syntax: %0 Number &gt; ----------.bat echo set {n}=%%2 :Loop &gt;&gt;{c} echo ! &gt; {c}.bat find /c "!" {c} call {c} { Requires version 3.0 or later } echo %{n}% if not %{n}%==%1 goto Loop :End for %%v in ({?}.* ----------.bat?) do del %%v
The first of the added lines creates a temporary batch file with a funny name, ----------.BAT. The name is purposely chosen to match the string of hyphens that FIND outputs when the name of the file to be searched is supplied on the command line. To complete the process, FIND's output is sent to another batch file, {C}.BAT. When {C} is called, it in turn executes ----------.BAT, because DOS looks for a batch file named by the first string on the line (ten hyphens) that was stored in {C}.BAT by the FIND command. There are two other strings on the line of text in {C}.BAT. These are passed to the second batch file as command line input arguments and the second argument is in turn stored into the environment variable, {N}. Once stored there, the calling procedure can access it for use, as well as test it against the loop limit.

This second example is actually a pretty useful implementation. By replacing the 'echo %{n}%' line with the process that needs to be counted, a useful procedure can easily be developed. An offshoot of this could be used to do something that has often been asked on the net. That is, name files with sequential numbers. But, I've got an even better way to number files sequentially.

However, there is another completely different approach to using numbers that I like to use. That is, a numbered list (a future link).




Top | General Notes | Homepage