home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol12n07.zip / WITH.ZIP / WITH.BAT next >
DOS Batch File  |  1993-03-12  |  4KB  |  90 lines

  1. @ECHO OFF
  2. REM WITH.BAT
  3. :* Initialization done?  If yes, go to count.
  4. IF '%!%'=='%0' GOTO Count
  5. :* Any command present on command line?  If not, give error.
  6. IF '%4'==''    GOTO syntax
  7. :* In secondary command processor?  If yes, do initialize.
  8. :* Else start secondary processor.
  9. IF '%1'=='*'   GOTO Init
  10. :* Store command and its parameters to temporary file $.BAT
  11. ECHO %4 %5 %6 %7 %8 %9 > $.BAT
  12. :* Load secondary command processor and re-invoke this batch file, so
  13. :* that all e-vars and recursion stack are cleared upon termination.
  14. :* Param * (asterisk) causes jump to label Init on re-invocation.
  15. %COMSPEC% /c %0 * %1 %2 %3 
  16. :* Delete the temporary file $.BAT
  17. DEL $.BAT
  18. GOTO end
  19.  
  20. :===== Init ========
  21. :* Initialize environment variables.  Note: Previous re-invocation of
  22. :* batch file using * (asterisk) as the 1st param has pushed ename,
  23. :* m and n parameters into the 2nd, 3rd, and 4th parameter slots.
  24. :* Set e-var & (ampersand) to param 4 (stop count) plus ampersand.
  25. SET &=%4&
  26. :* Set e-var n$ to param 4 (stop count).
  27. SET n$=%4
  28. :* Set e-var m$ to param 3 (start count).
  29. SET m$=%3
  30. :* Set e-var # (number sign) to param 2 (counter variable).
  31. SET #=%2
  32. :* Set e-var ! (exclamation point) to name of this batch file.
  33. SET !=%0
  34. :* Leading 0? If leftmost digit of m$ (start ct) = 0 but m$ not = 0,
  35. :* set @ (leading zero flag) to 0.
  36. IF NOT '%m$%'=='0' FOR %%a IN (/%m$%z) DO IF '%%a'=='0' SET @=0
  37. :* Re-invoke WITH w/o params - this time it jumps to label Count.
  38. %!%
  39.  
  40. :===== Count =======
  41. :* E-var & initially contains stop count plus ampersand.  Each recursion
  42. :* clips the initial character.  When only & is left, goto Process
  43. IF '%&%'=='&' GOTO Process
  44. :* Clip the first character from e-var &.
  45. FOR %%a IN (/%&%) DO SET &=%%a
  46.  
  47. :===== Recurse =====
  48. :* (1st time here results from last Init statement which invokes
  49. :* WITH, passing no params.  E-var + set to null, below, in that case.)
  50. :* 1st param of a nested call?  Set + to 1st param to find out.
  51. SET +=%1
  52. :* If %1=0, yes -- set + to value of lead 0 flag, @. Else, + remains set
  53. :* to 1st param. (@ is 0 if start count has leading zeros, null else.)«MDNM».
  54. IF '%1'=='0' SET +=%@%
  55. :* Re-invoke batch file.  Prefix each unit digit (0 - 9) of count w/the
  56. :* count's leftmost digits, held in e-var +.«MDNM»
  57. :*  For binary counting, truncate CALL %!% statement starting at %+%2.
  58. :*  For hexadecimal, add %+%A to %+%F.
  59. CALL %!% %+%0 %+%1 %+%2 %+%3 %+%4 %+%5 %+%6 %+%7 %+%8 %+%9
  60. :* process next parameter
  61. SHIFT
  62. IF NOT '%1'=='' GOTO Recurse
  63. :* append an ampersand to e-var & (now a re-nesting flag).«MDNM»
  64. SET &=%&%&
  65. GOTO end
  66.  
  67. :===== Process =====
  68. :* So $.bat can access it, store 1st param (number being processed)
  69. :* into the count variable. (E-var # holds count var's name.)«MDNM»
  70. SET %#%=%1
  71. :* Set e-var ^ to $ when the start count is reached.  For it and
  72. :* all numbers to stop count, $.BAT will be executed.
  73. IF '%1'=='%m$%' SET ^=$
  74. CALL %^%
  75. :* If reached stop count, exit secondary command processor,«MDNM»
  76. :* ending all recursion and clearing all variables.
  77. IF '%1'=='%n$%' EXIT
  78. SHIFT
  79. :* Process next command line parameter
  80. IF NOT '%1'=='' GOTO Process
  81. GOTO end
  82.  
  83. :===== syntax ======
  84. ECHO      Syntax: WITH ename=m,n command [p1 p2 ... p5]«MDNM»
  85. ECHO          - command can be internal, COM, .EXE, or .BAT
  86. ECHO          - if with leading zeros, n and m must have same no. of digits
  87. ECHO          - ename is any variable, usable in p1, p2, etc. as %%ename%%«MDNM»
  88.  
  89. :===== end =========
  90.