A Generalize Method for Incrementing a Number
I promised to provide routines, based on my Real Math approach, to efficiently increment or decrement a number. Here they are!

 Incrementing a Number
First, the incrementing routine, INCR.BAT.
:: INCR.BAT - An efficient 'pure DOS' incrementing routine. :: :: Tom Lavedas <lavedas@pressroom.com> :: http:://www.pressroom.com/~tglbatch/ @echo %dbgi% off %5 if [%1]==[] for %%v in (echo goto:End) do %%v Syntax: %0 VarName [Num] if [%3]==[] echo %0 %1 %2 %%%1%% 0 :: :: :: del del del&gt; {temp}.bat %8 {temp}.bat for %%v in ({S} \1 \2 \3 \4 \5 \6 \7 \8 \9) do set %%v=&gt; nul set [CF]=1&gt; %2._1 for %%v in (?._1 ??._1 ???._1 ????._1 ?????._1 ??????._1 ???????._1) do ren %%v 0%%v :Loop call %0 %1 2 3 4 goto:Add Error 0 1 2 3 4 5 6 7 8 9 0 if not [%{S}%]==[Error] if not exist 00000000._1 goto Loop for %%v in (set %For example%) do %%v %1=%[CF]%%{S}% for %%v in ([CF] {S}) do set %%v=&gt; nul for %%v in (del goto:End) do %%v *._1 :Add digits for %%v in (0 1 2 3 4 5 6 7 8 9) do if exist ???????%%v._1 set \%%v= %[CF]%) do shift; for %%v in (1%\0% 2%\1% 3%\2% 4%\3% 5%\4% 6%\5% 7%\6% 8%\7% 9%\8% 10%\9%) do set {S}= for %%v in (\0 \1 \2 \3 \4 \5 \6 \7 \8 \9 [CF]) do set %%v=&gt; nul if exist *._1 ren *._1 ???????._1 %/ Drop last digit /% for %%v in (???????._1) do ren %%v 0%%v %/ Shift right 1 digit /% set {S}=%6%{S}%&gt; nul if [%7]==[] set [CF]=1&gt; nul :End ::
It's not nearly as fast as the approach used in 'A Better Way to Count', but it is much more versatile. Advantages include it being CALLable, nestable and able to start at numbers from zero to greater than 99 million.

This routine bears little resemblence to the ADD.BAT or SUB.BAT routines presented in the 'Real Math' article, but that is its heritage. It uses all of the same concepts, but is implemented with a much more compact set of code. Built this way, it runs faster and is about two-thirds the size of the original ADD.BAT routine. However, the cost is that it is that much more opaic to understanding. I'm not quite certain I understand how it works. I certainly don't feel up to providing an explanation of it's operation. The best I can suggest is that you try it out from the command line something like this ...

set dbgi=:: set for example=echo command /e:2048 /y /c incr test 123
or maybe ...
set dbgi=:: set for example=echo command /e:2048 /c incr test 123 &gt; output.txt
The latter version can be reviewed in a text editor or printed for leasurely evaluation. (Might even be useful for putting yourself to sleep some night.)

This may or may not give you some insight into what is going on. Good luck!

However, difficult it may be to understand, it really is very simple to use. For example, counting from 100 to 125 is easily coded in just these few lines ...

set Numb=100 :Loop echo Number=%Numb% if not %Numb%==125 for %%v in (call goto:Loop) do %%v incr Numb

 Decrementing a Number

The decrementing routine was easily derived from the incrementing procedure. It only requires the reversal of the number count in two places and the addition of a little code to remove a leading zero which results from counting down past a decade boundary (e.g. 100 decremented to 99).
:: DECR.BAT - An efficient 'pure DOS' decrementing routine. :: :: Tom Lavedas <lavedas@pressroom.com> :: http:://www.pressroom.com/~tglbatch/ @echo %dbgi% off %5 if [%1]==[] for %%v in (echo goto:End) do %%v Syntax: %0 VarName [Num] if [%3]==[] echo %0 %1 %2 %%%1%% 0 :: :: :: del del del&gt; {temp}.bat %8 {temp}.bat for %%v in ({S} \1 \2 \3 \4 \5 \6 \7 \8 \9) do set %%v=&gt; nul set [BF]=1&gt; %2._1 for %%v in (?._1 ??._1 ???._1 ????._1 ?????._1 ??????._1 ???????._1) do ren %%v 0%%v :Loop call %0 %1 2 3 4 goto:Add Error 9 8 7 6 5 4 3 2 1 0 9 if not [%{S}%]==[Error] if not exist 00000000._1 goto Loop for %%v in (set %For example%) do %%v %1=%{S}% for %%v in ([BF] {S}) do set %%v=&gt; nul for %%v in (del goto:End) do %%v *._1 :Add digits for %%v in (0 1 2 3 4 5 6 7 8 9) do if exist ???????%%v._1 set \%%v= %[BF]%) do shift; for %%v in (1%\9% 2%\8% 3%\7% 4%\6% 5%\5% 6%\4% 7%\3% 8%\2% 9%\1% 10%\0%) do set {S}= for %%v in (\0 \1 \2 \3 \4 \5 \6 \7 \8 \9 [BF]) do set %%v=&gt; nul if exist *._1 ren *._1 ???????._1 %/ Drop last digit /% for %%v in (???????._1) do ren %%v 0%%v %/ Shift right 1 digit /% if exist 00000000._1 if not %6==0 set {S}=%6%{S}%&gt; nul if not exist 00000000._1 set {S}=%6%{S}%&gt; nul if [%7]==[] set [BF]=1&gt; nul :End ::
That's about it. Use it in about the same way as INCR.BAT. Have fun.


Top | General Notes | Homepage