
| Batfiles: The DOS batch file programming handbook & tutorial © 1997-2003 by Laurence Soucy
|  
|  Batch Showcase | 
|
|
| 
| 
| | | Batfiles: The DOS batch file programming handbook & tutorial © 1997-2003 by Laurence Soucy
| The purpose of this page is both to showcase selected work of other authors
and to familiarize the reader with other batch styles. The batch files here are presented as written by their respective authors. Keep in mind that many will require adaptation to meet the individual needs of the user. All rights are retained by the respective authors. Warning: most of these batch files assume the default (or current) directory is writeable. John Savage | Tom Lavedas | Herbert Kleebauer | Ed Schwartz | Todd Vargo | Vernon Frazee | Johnathan Liu Benny Pedersen | Laura Fairhead  John Savage - a new, fast counter for DOS batch files  Following on from my recent disclosure that the MODE function can perform one-character-truncation of a string, I have re-visited the batch task of incrementing an integer counter. In previous incarnations of counters I have variously exploited the dos 3.3 and dos 5 "FOR %%X in (/STRING)
DO" method and more latterly the CHOICE command, but neither of these has offered the efficiency and speed that comes with use of the MODE command. (Admittedly, this should be referred to as "mis-use" of the MODE command, as I'm definitely exploiting undocumented behaviour here.)  Here's a batch file which takes an integer argument, and stores "that value plus one" in the environment variable
named "M".  :: incr.bat @echo off :: performs integer sum (arg1 + 1) and :: saves result in env variable M :: call with one argument, example usage: INCR 497 :: author: John Savage 27 Aug 1999 :: mode %%3%1/>d:tmp.bat %2 echo set m=%%3>invalid.bat call d:tmp.bat if %1==9 set m=10%3 if %m%9==%1 %0 %m% :: 0%3 if %m%8==%1 set
m=%m%9%3 if %m%7==%1 set m=%m%8%3 if %m%6==%1 set m=%m%7%3 if %m%5==%1 set m=%m%6%3 if %m%4==%1 set m=%m%5%3 if %m%3==%1 set m=%m%4%3 if %m%2==%1 set m=%m%3%3 if %m%1==%1 set m=%m%2%3 if %m%0==%1 set m=%m%1%3 del d:tmp.bat del invalid.bat :: The filename prefix d: denotes use of a ramdrive; if you don't use a ram drive then delete that prefix wherever it occurs. Lack of a ramdrive isn't so much of a drawback, since I explored a few algorithms
to gauge the speed differences and finally settled on this compromise which minimizes the number of file operations involved.  As written, the counter will accommodate "integers" up to at least 35 digits. Some of these places can be leading zeros, or some can be alphabetic chars, e.g., serial-1999-aw$0005291738, where the right-hand digits get incremented.  In some uses it might be desirable that the new value be saved to a file for ready access at some future time, so you could append lines such as: echo set m=%m%>somefile.bat set m=  The minimalist program presented here can also be modified to first retrieve the stored value from that file, e.g., by adding to the start: if "%1"=="" if exist somefile.bat call somefile.bat if "%1"=="" %0
%m% Extra tests and checks can be added, according to your need and the intended use of the program.  The undocumented ability of MODE to do right-side truncation offers superior speed as far as this arithmetic is concerned, compared with methods that are reliant on CHOICE to explode the numeric string.  Tom Lavedas - an efficient 'pure DOS' adding routine  Add.bat is much shorter than all other adders I have seen (just 34 lines), but no less difficult to comprehend. I believe it is also much faster than the others out there, though I have not done speed trials. Its speed surprises
me, taking a blink of an eye to add four and five digit numbers on my K6 - 200 MHz machine at home. Heck, it only takes a second or two on my ancient 486Dx2 at work. I think it's almost fast enough to be considered a tool, rather than just a toy. But you can be the judge. Try it yourself and let me know what you think.  :: add.bat :: Tom Lavedas <lavedas@pressroom.com> ::
http://www.pressroom.com/~tglbatch/ @echo off %4 if [%3]==[] for %%v in (echo goto:End) do %%v Syntax: %0 VarName Num1 Num2 set {0}=%0 %1 for %%v in (%1 [CF] {S} \0 \1 \2 \3 \4 \5 \6 \7 \8 \9) do set %%v=>nul > %2.{1 echo.%/ Pad numbers out to eight digits /% > %3.{2 echo. for %%v in (?.{? ??.{? ???.{? ????.{? ?????.{? ??????.{? ???????.{?) do ren %%v 0%%v > %temp%.\{t}.bat %{0}% {1 {2 goto:Parse call :Parse numbers if exist *.%2 copy *.%2 %%_%256%%? >
nul %/ Get last digit /% >>%temp%.\{t}.bat for %%v in (0 %%_%256%%?) do echo set %2}=%%v if exist *.%2 ren *.%2 ???????.%2 %/ Drop last digit /% for %%v in (???????.%2) do ren %%v 0%%v %/ Shift right 1 digit /% for %%v in (shift %4) do %%v %temp%.\{t}.bat for %%v in (00000000.{? %%_??56%%?) do del %%v %{0}% 2 3 goto:Add 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 :Error for %%v in (echo goto:End) do %%v ERROR - Improperly formed number. :Add
digits set \%{1}%= 1 2 3 4 %[CF]%) do shift; for %%v in (%\0% x%\1% x%\2% x%\3% x%\4% x%\5% x%\6% x%\7% x%\8% x%\9%) do goto Error set \%{1}%=> nul set \%{2}%= x) do shift; for %%v in (%\0% x%\1% x%\2% x%\3% x%\4% x%\5% x%\6% x%\7% x%\8% x%\9%) do goto Error for %%v in (set shift) do %%v {S}=%0%{S}%> nul for %%v in (\%{2}% [CF]) do set %%v=> nul if [%9]==[] set [CF]=1> nul >
%temp%.\{t}.bat if exist *.{? %{0}% {1 {2 goto:Parse call for %%v in (%{0}%) do set {0}=%%v> nul echo.%{0}%=%[CF]%%{S}% set %{0}%=%[CF]%%{S}%> nul :End for %%v in (*.{? %temp%.\{t}.bat?) do del %%v for %%v in ([CF] {S} {0} {1} {2}) do set %%v=> nul ::  Herbert Kleebauer - echo any character by inserting it's hex value  :: echohex.bat @echo off echo Bj@jzh`0X-`/PPPPPPa(DE(DM(DO(Dh(Ls(Lu(LX(LeZRR]EEEUYRX2Dx=>echohex.com echo 0DxFP,0Xx.t0P,=XtGsB4o@$?PIyU!WvX0GwUY Wv;ovBX2Gv0ExGIuht6>>echohex.com echo ?A}I}KIuwHHTCI[GAGExtc{OCIKGMgELCI?GGgELAs?GH`LRBcx=k_K?Ax>>echohex.com echo
VD?fCo?CaBBHAv0>>echohex.com echohex This line contains $3c $3e $7c $25 and $24 $0d$0a del echohex.com  Herbert Kleebauer - echo all characters without a CR/LF  ::
echoall.bat @echo off :: also works in NT echo XP,;P_,EP0Ex0Ez0Em3Em-ZBP(Eiu Herbert Kleebauer - universal batch
input  @echo off echo BATCH_INPUT___hhQX-PPP^XPPP D"HH0D40DF0De0Dgs>myinput.com echo myinput.bat hoAX5@@PZYh@xX5@D3!r/P[VX,b,.PZh>>myinput.com echo X3!X2DqP,{,{PYX4=0DqVX,j,.PZh@@X3!=set input>>myinput.com echo Enter name: myinput.com call myinput.bat del myinput.com del myinput.bat echo You entered %input%  This works with DOS and in a NT DOS-box, but not if you start the batch file with the NT explorer (then " ^ & are not allowed within the echo command). Use the following code instead:  echo BATCH_INPUT___hgQX-PPP_XPPP E#HH0E50EG0Ef0Ehs>myinput.com echo myinput.bat hoAX5@@PZYh@xX5@D3!r/P[WX,b,-PZh>>myinput.com echo X3!X2ErP,{,{PYX4=0ErWX,j,-PZh@@X3!=set input>>myinput.com  Ed Schwartz - system information email: Edwin.Schwartz@dlr.de This version does not work in MS-DOS 7.x.  :: SI.BAT :: Syntax: SI :: :: creates six environment variables that
:: contain DayOfWeek, Date, Time, :: Drive, Directory, and DOS-Version :: The exact format of the variables is :: country dependent, e.g. for GERMANY: :: :: DAY=Mon :: DATE=12.10.92 :: TIME=10:13:24 :: DRIVE=C: :: DIR=C:\BATCH :: VER=6.2 :: :: Ed Schwartz, Oct. 92 :: @if
%1*==* echo off @set Day=%1 @set Date=%2 @set Time=%3 @set Drive=%5 @set Dir=%6 @set Ver=%9 @if %1*==* %comspec% /e:4097 /c %0 %0 > %temp%\{SI}.BAT @if %1*==%0* prompt call %0 $d $t $n: $p $v @call %temp%\{SI}.BAT  Todd
Vargo - system information This version also works in MS-DOS 7.x.  :: si.bat :: Optimized for Windows 95/98 (US English) :: Original batch author: Edwin Schwartz, Oct 1992 :: This version's author: Todd Vargo, May 23, 2000 @if setvars==%1 prompt set Date=$d$_set Time=$t$_set Drive=$n $_set Dir=$p$_set Ver=$v$_ @if getday==%1 set Day=%2 @if getday==%1 set Date=%3 @if not *==%1* goto
end @echo off %comspec% /e:4096 /c %0 setvars > %temp%.\{SI}.BAT for %%? in (call del) do %%? %temp%.\{SI}.BAT call %0 getday %date% set % added to display variables % :end  Vernon Frazee - return errorlevel  @echo off :ERRLVL.BAT v2.11 --------------- Vernon Frazee 12/94 :Purpose: Displays errorlevel set by last program run :---------------------------------------------------- set EL=Errorlevel for %%x in (A B) do set %%x=0 1 2 3 4 5 if not %EL% 250 set A=%A% 6 7 8 9 if not %EL% 200 set B=%A% for %%x in (1 2) do if %EL% %%x00 set C=%%x for %%x in (%B%) do if %EL% %C%%%x0 set D=%%x if
(%C%%D%)==(0) set D= set C=%C%%D% for %%x in (%A%) do if %EL% %C%%%x set ErrLvl=%C%%%x set %EL%=%ErrLvl% echo %EL%=%ErrLvl% for %%x in (A B C D EL ErrLvl) do set %%x=  Johnathan Liu - return errorlevel Inspired by Vernon Frazee's
errlvl.bat, above.  :: Johnathan Liu, April 2001 @ECHO OFF SET A=0 1 2 3 4 5 6 7 8 9 IF ERRORLEVEL 200 SET A=0 1 2 3 4 5 FOR %%a IN (0 1 2) DO IF ERRORLEVEL %%a00 SET EL=%%a FOR %%a IN (%A%) DO IF ERRORLEVEL %EL%%%a0 SET EL=%EL%%%a IF NOT errorlevel 250 SET A=0 1 2 3 4 5 6 7 8 9 FOR %%a IN (%A%) DO IF ERRORLEVEL %EL%%%a SET EL=%EL%%%a ECHO %EL% FOR %%a IN (A EL) DO SET %%a= ::
 Benny Pedersen - return errorlevel  @ECHO off ::1 SET E= ::2 SET E= % two spaces %
FOR %%! IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL %%!0 SET E=%%! rem --------^----------------------------------------------^ FOR %%! IN (10 11 12 13 14 15 16 17 18 19 20 21 22 23 24) DO IF ERRORLEVEL %%!0 SET E=%%! FOR %%! IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL
%E%%%! SET E=%E%%%! FOR %%! IN (0 1 2 3 4 5 ) DO IF ERRORLEVEL 25%%! SET E=25%%! ECHO. Errorlevel is %E%, (NOT %E% +1). :: Modifications: :: The program/tool works as it is but it can be modified as this: :: -- Align LEFT: Remove the ::1 in line num 1 & the first zero in line num 3. :: -- Align RIGHT: Substitute in line num 3: the (SET E=%%!) with (SET E= %%!), ::
& then either remove the (::2 ) OR substitute the first zero :: (0 ) in line num 3 with (" "), i.e. Quote, Space and Quote. :: If the (::2 ) is removed, then you may also remove the zero. :: --
Three digits i.e.: both left and right aligned: Substitute in line num 3: :: the (SET E=%%!) with (SET E=0%%!). :: Notes: As it is, OR if the modification for left alignment is done, then all :: the elements in the first FOR loop can be removed into the second/next loop. :: If desired, the longest line (the second FOR loop) can be done in two lines :: like this: :: FOR %%! IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL 1%%!0 SET E=1%%! :: FOR %%! IN
(0 1 2 3 4 ) DO IF ERRORLEVEL 2%%!0 SET E=2%%! :: -- Notice: If you split the second FOR loop as shown above, then you :: can't remove the elements as described in the first note.  Laura Fairhead - echo without appending a CR/LF  :: echos.bat @ECHO off :: ASCII Assembler by Laura Fairhead ECHO XPYP[*'CC-\1P\QX,=P,APZ5O!PQ2O~5aaI~}Ksx>%temp%.\echos.com %temp%.\echos.com AAAAA % example line% %temp%.\echos.com BBBBB % example line% DEL %temp%.\echos.com GOTO end Here is the DEBUG source code; ----------------------------------------------------------------->8 A POP
AX PUSH AX POP CX PUSH AX POP BX SUB AH,[BX] INC BX INC BX SUB AX,315C PUSH AX POP SP PUSH CX POP AX SUB AL,3D PUSH AX
SUB AL,41 PUSH AX POP DX XOR AX,214F PUSH AX PUSH CX XOR CL,[BX+007E] XOR AX,6161 DEC CX JNG 01A2 DEC BX JNC 01A0 NAS4 RCX 28 W Q
----------------------------------------------------------------->8 AX is now explicitly initialised to 0 by pinching the 0-word that DOS has on the stack at program entry. It is fine to do this here because the program sets up it's own 0-word to return to DOS. The DI register was taken out to compensate for the extra byte of code added, and in the end I managed to make the whole program 1 byte less! Also nice is the '%' character has gone! :end
 Laura Fairhead - decr.bat, based on J. Savage's incr.bat  @ECHO OFF :: decrement value given in parameter 1 and output the result SET _=%1 ::
********************************************** :: * DECR - DECREMENT VALUE BY 1 * :: * Entry: %_%=value * :: * Exit: %_%=value-1
* :: * Last Modified: 15/11/00 * :: * Compatibility: DOS 5+ * :: * Notes:
* :: * + Leading 0's unaffected * :: * + Invalid values cause unspecified results * :: * + Locale dependent -- requires US DOS * ::
********************************************** :DEC ECHO SET S=%%3>INVALID.BAT SET R= GOTO DECK0 :DECL0 SET _=%S% SET R=9%R% :DECK0 MODE %%1%_%/>$.BAT CALL $.BAT IF %S%0==%_% GOTO DECL0 IF %S%1==%_% SET D=0 IF %S%2==%_% SET D=1 IF %S%3==%_% SET D=2 IF %S%4==%_% SET D=3 IF %S%5==%_% SET D=4 IF %S%6==%_% SET D=5 IF %S%7==%_% SET D=6 IF %S%8==%_% SET D=7 IF %S%9==%_% SET D=8 SET _=%S%%D%%R% FOR %%_ IN
(INVALID.BAT $.BAT) DO DEL %%_ FOR %%_ IN (S R D) DO SET %%_= :: ********************************************** ECHO %1-1=%_% SET _= Batfiles: The DOS batch file programming handbook & tutorial ©
1997-2003 by Laurence Soucy
| 
|  
|
| 
| 
| http://purl.oclc.org/net/Batfiles/ © 1997-2003 by Laurence Soucy. All rights reserved. 
| | 
|