home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / info / dostips1.arc / DOSBATCH.TXT < prev    next >
Encoding:
Text File  |  1985-11-25  |  44.8 KB  |  946 lines

  1.                     Batch Copying Improvement
  2.        (PC Magazine Vol 3 No 25 Dec 25, 1984 User-to-User)
  3.  
  4.      COPYNEW.BAT copies files selectively from one drive to
  5. another if the files to be copied are not on the targeted drive. 
  6. COPYNEW verifies that the source and target drive designators 
  7. have been entered and that they are not the same.
  8.      To copy all of the files from the disk in drive A: that are 
  9. not already on drive C:, you would type (at the DOS A> prompt):
  10.           COPYNEW A C
  11. and hit the Enter key.  You omit the colons after the drive
  12. letters; the batch file puts them in for you.
  13.  
  14. echo off
  15. rem       This program copies files selectively from one drive to
  16. rem       another only if the file is NOT on the target drive.
  17. rem       The syntax is:  
  18. rem                      copynew x y
  19. rem       where x and y are the source and target drives.  Note
  20. rem       these are entered like x and not like x:
  21. rem
  22. if x%1 == x goto error 1
  23. if x%2 == x goto error 2
  24. if %1 == %2 goto error 3
  25. echo on
  26. %1:
  27. for %%f in (*.*) do if not exist %2:%%f copy %1:%%f %2:%%f
  28. goto end
  29. :error1
  30. echo Source drive name missing
  31. goto end
  32. :error2
  33. echo Target drive name missing
  34. goto end
  35. :error3
  36. echo Source and Target drive names can't be the same
  37. goto end
  38. :end
  39. echo on
  40.  
  41. -----------------------------------------------------------------
  42.                 Batch Test for Empty DOS Variable
  43.                (PC World Nov 1984 The Help Screen)
  44.  
  45.      An on-line help system consisting of several ASCII files 
  46. with a full screen of information on a particu-lar command, can 
  47. be developed.  A batch file called HELP.BAT calls up the screens.  
  48. The ASCII files are straightforward.  Each should be given the 
  49. name of the corresponding command and the extension .HLP; for 
  50. example, COPY.HLP gives information on the COPY command.
  51.      HELP.BAT is also straightforward.  Its only instruction 
  52. (other than coloring the screen to identify a "help" screen) is 
  53. TYPE %1.HLP.  A HELP file can also be included which explains how 
  54. to use the help facility.  Including the HELP file requires that 
  55. HELP.BAT be prepared as:
  56. if exist %1.hlp goto does
  57. if x==%1x goto help
  58. echo Help unavailable for %1
  59. goto end
  60. :does
  61. type %1.hlp
  62. goto end
  63. :help
  64. type help.hlp
  65. :end
  66.  
  67. -----------------------------------------------------------------
  68.                       Subdirectory Fixer II
  69.         (PC Magazine Vol 4 No 3 Feb 5, 1985 User-to-User)
  70.  
  71.      User-to-User published a trick using TREE.COM, TREEFIX.BAT, 
  72. and TREEFIX.BAS to locate files across subdirectories.  While the 
  73. TREEFIX trio works -- and provides information that DOS does not 
  74. -- the FINDFILE.BAT batch file below provides an easier way to 
  75. find a file buried deep inside one of your subdirectories.
  76.      FINDFILE relies on a little-used feature of CHKDSK.COM, the 
  77. /V option.  The DOS manual is a bit murky on CHKDSK and most users 
  78. don't do much more with this command than see how much space is 
  79. left on their disks.  The /V option lists all files in all
  80. subdirectories.  The DOS 3.0 manual is more helpful both in
  81. explaining the many features of the powerful CHKDSK.COM command.
  82.      FINDFILE.BAT first makes sure you have CHKDSK.COM, FIND.EXE, 
  83. and MORE.COM on your disk.  If you're PATHing to a subdirectory 
  84. that contains these files, you should leave out the dozen lines 
  85. beginning with the second one ("if exist chkdsk.com ...") and 
  86. ending with ":C".  FINDFILE then redirects CHKDSK/V into a file 
  87. and uses the FIND filter (and the MORE filter if appropriate) to 
  88. locate the file in question.
  89.      To search for BASICA.COM, for instance, you would type: 
  90. FINDFILE BASICA.  If you typed: FINDFILE BASIC the batch file 
  91. would locate both BASIC.COM and BASICA.COM, and any other
  92. filename with the capital letters BASIC in it.  You may also use
  93. parts of names.  FINDFILE BASICA would find BASICA.COM.  This
  94. comes in handy if you want to look for files with the same
  95. extensions.  FINDFILE .COM will list all your .COM files.
  96. Remember to enter capital letters only.  And don't put quotation
  97. marks around the filenames or parts of filenames you want to find
  98. -- the batch file will do this for you automatically.  FINDFILE
  99. won't display a special message telling you no matches were found
  100. if it comes up empty.  But this will be obvious when no matches
  101. are displayed on your screen.
  102.  
  103. FINDFILE.BAT batch file to locate specific files across all
  104. subdirectories on your disk:
  105.  
  106. echo off
  107. if exist chkdsk.com goto A
  108. echo PUT CHKDSK.COM on your disk
  109. goto D
  110. :A
  111. if exist find.exe goto B
  112. echo PUT FIND.EXE on your disk
  113. goto D
  114. :B if exist more.com goto C
  115. echo PUT MORE.COM on your disk
  116. goto D
  117. :C
  118. echo NOW SEARCHING DIRECTORIES FOR "%1"
  119. chkdsk /v>--------
  120. find "%1" -------- | more
  121. del --------
  122. :d
  123. rem ...DONE
  124.  
  125. -----------------------------------------------------------------
  126.                            Better FIND
  127.        (PC Magazine Vol 4 No 7 April 2, 1985 User-to-User)
  128.  
  129.      The FINDFILE.BAT (see article above) is a useful utility.
  130. To make the heart of the program, subroutine :C, more elegant,
  131. use DOS's piping rather than redirection.  The 15th line of the 
  132. replacement FINDFILE.BAT below replaces the second, third, and 
  133. fourth lines in subroutine :C of the original FINDFILE.BAT.  With 
  134. piping, DOS automatically takes care of creating and deleting the 
  135. intermediate files.  The new, improved FINDFILE.BAT:
  136.  
  137. echo off
  138. if exist chkdsk.com goto A
  139. echo PUT CHKDSK.COM on your disk
  140. goto END
  141. :A
  142. if exist find.exe goto B
  143. echo PUT FIND.EXE on your disk
  144. goto END
  145. :B
  146. if exist more.com goto C
  147. echo PUT MORE.COM on your disk
  148. goto END
  149. :C
  150. echo NOW SEARCHING DIRECTORIES FOR "%1"
  151. chkdsk /v | find "%1" | more
  152. :END
  153. REM ...done
  154.  
  155. -----------------------------------------------------------------
  156.                          DOS FIND Sieves
  157.       (PC Magazine Vol 4 No 8 April 16, 1985 User-to-User)
  158.  
  159.      The DOS FIND filter gives users a fast way to retrieve
  160. information from large databases, document files, programs or any 
  161. ASCII file.  But there's a way to make it even more useful.  This 
  162. can be done with the SEARCHUM.BAT file below.  If you want to
  163. retrieve from a large client address file called CLIENT.DAT all 
  164. cases in which the client is female, lives in Boston and is
  165. self-employed, the command is:
  166.           SEARCHUM CLIENT.DAT "female" "Boston" "self-employed"
  167.      SEARCHUM.BAT first turns off the echoing of subsequent
  168. command lines to the screen.  The first FIND command searches file 
  169. CLIENT.DAT (passed as dummy variable %1) for any instance of the 
  170. first characteristic, "female", passed via dummy variable %2.  All 
  171. such lines are written to the first of the SIEVE#.DAT file via 
  172. redirection of output along with the sequence line number since 
  173. the /N parameter was used with FIND.  Then each subsequent command 
  174. line searches for the subset of lines in the latest SIEVE#.DAT 
  175. file that contains the character string passed to the command 
  176. line via the dummy variables.
  177.      If no lines contain all the properties that you specify in 
  178. the list, then one or more of the later SIEVE#.DAT files will be 
  179. empty.  Just search back until you find the file with the
  180. information you seek.  DOS 2.0 allows up to nine dummy variables
  181. in the list after the command file name. So this method lets you
  182. specify up to eight characteristics in addition to the name of
  183. the file you wish to have searched if you extend the number of
  184. FIND command statements within the .BAT file.
  185.      Instead of requiring that all the properties in the list be 
  186. present simultaneously, you can structure the .BAT file so that 
  187. you can find lines that contain any (i.e., logical OR) of a list 
  188. of characteristics and then concatenate the accumulated files at 
  189. the end with a COPY command.  Or, you can tailor a search to
  190. detect some combination of jointly present and alternative
  191. conditions.  On a PC-XT, FIND can readily scan and report the
  192. presence of two instances of a unique character sequence located
  193. in the middle and at the end of a 7,000-line file in about 10
  194. seconds.  For ASCII files under a few thousand lines, the response
  195. seems instantaneous.
  196.  
  197. SEARCHUM.BAT:  ECHO OFF
  198.                FIND/N %2 %1         > SIEVE1.DAT
  199.                FIND/N %3 SIEVE1.DAT > SIEVE2.DAT
  200.                FIND/N %4 SIEVE2.DAT > SIEVE3.DAT
  201.  
  202. Editor's Note:  It is possible to save a bit of keystroking by 
  203. putting the quote marks in the batch file (e.g., %1" rather than 
  204. %1, "%2" rather than %2, etc.) so you don't have to type in the 
  205. quote marks when beginning the search, but this works only if 
  206. you're searching for single words.  And FIND is case-sensitive,
  207. so you have to be sure your database doesn't contain the words 
  208. female, Female and FEMALE.
  209.  
  210. -----------------------------------------------------------------
  211.                          Clear-Cut TREEs
  212.       (PC Magazine Vol 4 No 8 April 16, 1985 User-to-User)
  213.  
  214.      The DOS TREE utility shows the structure of all subdirectories
  215. ies on a disk, but its display is not very clear.  The TREEFIX.BAT 
  216. file redirects the DOS TREE output to a file, TREE1.LST, which is 
  217. then processed by the BASIC TREEFIX.BAS program to give you a 
  218. much clearer picture of how your subdirectories are arranged.  To 
  219. use this, you can either have TREEFIX.BAT and TREEFIX.BAS on the 
  220. disk you want to examine and just type TREEFIX while in DOS, or 
  221. you can specify a separate drive to examine, for instance,
  222. TREEFIX B:.  (Note: TREEFIX.BAS is on the BASIC disk.
  223.  
  224. TREEFIX.BAT:   ECHO OFF
  225.                ECHO Now searching directory ...
  226.                TREE %1>TREE1.LST
  227.                BASICA TREEFIX.BAS
  228.  
  229. Editor's Note:  Be sure to either have TREE.COM and BASICA.COM on 
  230. whatever subdirectory the two TREEFIX files are on, or have a 
  231. PATH to TREE.COM and BASICA.COM.  This technique works with up to 
  232. six levels of subdirectories; after that the display will wrap.
  233.      
  234. -----------------------------------------------------------------
  235.                             FOR Play
  236.        (PC Magazine Vol 3 No 24 Dec 11, 1984 User-to-User)
  237.  
  238.      FOR allows you to execute a DOS command for each of the 
  239. files in a specified set of files.  It's real usefulness comes 
  240. from the fact that the set of files can be specified using
  241. wildcard names.  But FOR works only from within batch files.
  242.      To perform the same operation with a set of files with the 
  243. same extension, create a batch file called REPEAT.BAT that
  244. contains one line:
  245.           FOR %%A IN (%2) DO %1 %%A %3
  246. The syntax for running the batch file is:
  247.           REPEAT program filespec [/program switches]
  248.      The first command line parameter becomes the name of the 
  249. program to be run, the second parameter is the set of files to be 
  250. worked on, and the third parameter is any switches that program 
  251. may need after the program name.  For example:
  252.           REPEAT EDIT *.DOC
  253. would translate into the FOR command required to edit a set of 
  254. DOC files.  REPEAT is also useful for the many public-domain
  255. utilities that do not allow wildcard filenames.
  256.  
  257. -----------------------------------------------------------------
  258.                        Nested Batch Files
  259.       (PC Magazine Vol 4 No 12 June 11, 1985 User-to-User)
  260.  
  261.      You can nest batch files simply by putting COMMAND/C before 
  262. the name of the batch file to be called.  When each file completes 
  263. execution, control returns to the calling batch file.  This
  264. feature is sort of documented in the DOS manual under "Invoking a 
  265. Secondary Command Processor," but the manual doesn't really tell 
  266. how to use it this way.
  267.      Tests indicate that performance doesn't degrade when in the 
  268. secondary processor.  To see how this works, create the four batch 
  269. files LEVEL1.BAT, LEVEL2.BAT, LEVEL3.BAT, and LEVEL4.BAT.  To run 
  270. it, type LEVEL1.  Once all the batch files have finished running, 
  271. you'll see a list of echoed "Return to ..." messages onscreen 
  272. that shows how the files were nested.
  273.      In an unrelated subject, to print a list of subdirectories, 
  274. something DOS does readily, just type:
  275.           DIR|FIND"<DIR>"
  276. Four simple files to demonstrate batch file nesting using COMMAND 
  277. /C to pass control.  To use this properly, create the BEEP.BAS 
  278. program and have it and BASICA.COM on disk:
  279.  
  280.      echo off
  281.      echo THIS IS LEVEL1.BAT
  282.      basica beep
  283.      command /c level2
  284.      echo Returned to LEVEL1.BAT
  285.  
  286.      echo off
  287.      echo THIS IS LEVEL2.BAT
  288.      basica beep
  289.      command /c level3
  290.      echo Returned to LEVEL2.BAT
  291.  
  292.      echo off
  293.      echo THIS IS LEVEL3.BAT
  294.      basica beep
  295.      command /c level4
  296.      echo Returned to LEVEL3.BAT
  297.  
  298.      echo off
  299.      echo THIS IS LEVEL4.BAT
  300.      basica beep
  301.  
  302.      10 BEEP 
  303.      20 FOR DELAY=1 TO 100:NEXT
  304.      30 SYSTEM
  305. --------------------
  306. Two ways to list subdirectories.  Make sure proper combinations of 
  307. TREE.COM, FIND.EXE, CHKDSK.COM, MORE.COM and SORT.EXE are on your 
  308. disk or PATHed to:
  309.  
  310.           tree /f | find "Path" | more
  311.  
  312.           chkdsk /v | find "Directory" | sort | more
  313.  
  314. -----------------------------------------------------------------
  315.                       The Fully Powered PC
  316.           (PC World July 1985 by B. Alperson, et. al.)
  317.  
  318.      The purpose of batch files is to automate commonly used
  319. procedures.  But often you may want to modify the way a procedure 
  320. works at the time you call its batch file.  For this DOS provides 
  321. replaceable parameters.  When you create a batch file that will 
  322. have variable information, simply use a replaceable parameter 
  323. variable at each point where such information will be needed. 
  324. Replaceable parameter variables consist of a single percent sign 
  325. and an integer (%0 through %9).  When you execute the batch file, 
  326. any words you type on the command line following the batch file 
  327. name will be substituted for those variables.
  328.      DOS assigns the value of each replaceable parameter in
  329. sequence, according to the position of the words on the command 
  330. lines.  The name of the batch file, the first word on the command 
  331. line, is used as the value for parameter %0.  The next word on
  332. the command line is the value for replaceable parameter %1, etc.
  333.      Consider the word processing batch file GO.BAT (below).  It 
  334. includes the replaceable parameter %1 in two lines: the line that 
  335. calls a directory and the line that calls WordStar.  If you were 
  336. to invoke this file by typing GO DOCUMENT.DOC, the parameter
  337. DOCUMENT.DOC would be substituted for each %1.  Thus, the line
  338. `DIR B:%1/W' would be interpreted by DOS as DIR B:DOCUMENT.DOC/W, 
  339. and DOS would search for the file DOCUMENT.DOC.  The line `A:WS 
  340. %1' would be interpreted by DOS as A:WS DOCUMENT.DOC.  By WordStar 
  341. convention, the command WS filename loads WordStar and then opens 
  342. the specified file.  WordStar would then bypass its opening menu 
  343. and go immediately into document mode with the file DOCUMENT.DOC 
  344. in place for editing.
  345.      Before DOS 2.0, you were restricted to ten replaceable
  346. parameters per batch file.  With the SHIFT command DOS 2.0 removed
  347. the ten-parameter restriction.  Now the only limitation on the
  348. number of parameters is that they all fit on a logical command
  349. line.  (A logical command line is everything you type from the DOS
  350. prompt through the carriage return and may be up to 127 characters.)
  351.      Imagine a command line with 14 elements:
  352.           A>command w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12 w13
  353. You still have only ten replaceable parameter variables available,
  354. %0 through %9.  The first ten words on the command line (the 
  355. command itself and w1 through w9) are assigned to those variables.
  356. The SHIFT command does not increase the number of variables; but
  357. each time you use it, the variable assignments are "bumped over"
  358. one position.  With the first SHIFT command, the variables %0
  359. through %9 are reassigned the values w1 through w10, respectively.
  360. The value originally assigned to %0 is bumped off.  Use the SHIFT
  361. command again, and the variables %0 through %9 acquire the values
  362. w2 through w11.
  363.      However, the true value of the SHIFT command goes beyond 
  364. getting more replaceable parameter values from the command line. 
  365. The following application shows how useful SHIFT can be.
  366.      DOS 2.0 provides a PRINT command to set up a print queue but 
  367. the command has at least two serious problems.  The queue created 
  368. by PRINT is limited to 10 files,  and the PRINT command does not 
  369. recognize path names.  Consequently, all the files to be printed 
  370. must be in the same directory, and you have to print from that 
  371. directory.  (DOS 3.0's PRINT command does not have those
  372. limitations.  The optional /Q: command enables you to specify the
  373. number of files -- up to 32 -- that the queue will hold.  The
  374. PRINT command in DOS 3.0 also recognizes path names.)
  375.      QUE.BAT shows how the SHIFT command can set up a queue
  376. program in DOS 2.0 (in this case called QUE.BAT) without the
  377. limitations imposed by the PRINT command.  QUE.BAT is invoked
  378. with the following syntax:
  379.           QUE filename1, filename2, filename3 .....
  380. This batch file allows you to queue up to 62 files, depending on 
  381. the length of the file names.  The printout of each file begins on 
  382. a new page with a header consisting of the file name and a line 
  383. of hyphens.
  384.      The first two lines of QUE.BAT turn off the echo and clear 
  385. the screen.  The fourth line, `A:BASICA A:FORMFEED', makes use of 
  386. a simple BASIC program called FORMFEED.BAS to start the file on a 
  387. new page.  FORMFEED.BAS consists of the line:
  388.           10 LPRINT CHR$(12);:SYSTEM 'ECHO PRINTING FILE %1'
  389. displays the name of the file currently being printed.  The next 
  390. three lines send output to the printer: the first sends the
  391. current file name; the second sends a line of hyphens, which is
  392. part of the header; and the third TYPEs the file's contents.
  393. All output is redirected to the printer with `>PRN'.  By using
  394. the TYPE command instead of PRINT, QUE.BAT allows you to use
  395. path names freely in file names specified as replaceable
  396. parameter values for QUE.BAT.
  397.      The `:PRINT' and `IF NOT ZIP==ZIP%1 GOTO PRINT' lines in 
  398. QUE.BAT are discussed below.  These lines determine whether more 
  399. files must be printed.  If not, the batch file terminates.
  400.      The SHIFT command makes this batch file work.  The first time 
  401. through the batch file, the value %1 is filename1; all references 
  402. to %1 use that value.  But before the procedure in the batch file 
  403. repeats, the SHIFT command is invoked, causing all references to 
  404. %1 to use the value filename2.  The third time through the batch 
  405. file, %1 takes on the value filename3, and so on.  Thus, the SHIFT 
  406. command allows the batch file to pick up each file name on the 
  407. command line with the use of a single DOS variable.
  408.      Normally the execution of a batch file flows from the first 
  409. to the last statement in the file.  With branching you can
  410. redirect the flow.  Branches can be conditional or unconditional.
  411. An unconditional branch uses the syntax GOTO label.  GOTO
  412. transfers control to the line containing the next executable
  413. command following label.  The label can be any string of up to
  414. eight characters prefixed by a colon.  The label line can include
  415. other words; however, the label must be the first word on the line.
  416.      The conditional branch allows the batch file to choose
  417. between actions depending on whether a certain condition is met. 
  418. The command uses the syntax:
  419.           IF [NOT] condition command
  420. The condition parameter names any of three conditions for
  421. testing.  It can test whether or not two string values are the
  422. same (IF [NOT] string1==string2 command).  It can test for the
  423. existence of a file (IF [NOT] EXIST filespec command).  Finally,
  424. condition can test for an exit code (IF [NOT] ERRORLEVEL number
  425. command).  When a program terminates, it can set a numerical
  426. value, called an exit code that indicates the manner in which the
  427. program terminated.  By checking this value with ERRORLEVEL, you
  428. can test whether the previous program terminated successfully or
  429. with an error.  This test is potentially useful, but the only
  430. programs in DOS 2.0 that produce exit codes are BACKUP and RESTORE.
  431. Unless your application involves these two commands or sets its own
  432. exit codes, ERRORLEVEL is of limited value.
  433.      The EXIST filename test is crippled by the exclusion of path 
  434. names from the filename parameter.  The specified file must be in 
  435. the default directory or it will not be found.  This limitation 
  436. has been eliminated in DOS 3.0.  However, even the weak IF
  437. commands of DOS 2.0 add substantially to your control over batch 
  438. operations.
  439.      The QUE.BAT batch file would not work if it weren't possible 
  440. to branch within the batch file. The critical line is the
  441. conditional branch instruction `IF NOT ZIP==ZIP%1 GOTO PRINT'.
  442. Each time the batch file reaches this point, it tests whether the 
  443. string `ZIP' is the same as the string `ZIP%1'.  If the value of 
  444. %1 is filename1, the line finds that `ZIP' does not equal 
  445. `ZIPfilename1', control is transferred to the next executable 
  446. line after the label `:PRINT', and the file is printed.  The SHIFT 
  447. command bumps the assignment of %1 to the next value, filename2. 
  448. The test is then performed again, with the same outcome.
  449.      Finally, no file names remain on the command line.  At this 
  450. point the test finds that `ZIP==ZIP%1', because %1 has no
  451. assigned value.  Therefore, the GOTO command is not executed, and
  452. control passes to the next line.  A final formfeed is sent to the 
  453. printer, and the batch file terminates.
  454.      `ZIP' is used as part of the string test because batch file 
  455. conditional commands cannot compare a string to a null value. 
  456. Some character(s) must be included, and the arbitrary choice ZIP 
  457. is as good as any other.  Also note that the replaceable variable 
  458. is placed on the right side of the double equal signs.  This 
  459. prevents the errant behavior that occurs when the right string 
  460. matches the beginning of the left string but is shorter.
  461.      DOS 2.0 also allows you to generate batch file loops.  Within 
  462. certain limits, you can have the batch file recycle the same 
  463. command with different arguments.  To do this, use the syntax:
  464.           FOR %%variable IN (set) DO command
  465. The DOS manual tells you, "The %% variable is sequentially set to 
  466. each member of set, and then the command is evaluated and
  467. executed."  You may find it more helpful to think of the command
  468. as follows:  FOR each item IN (this collection of items) DO the
  469. action specified in the command using the items appropriately.
  470. For example, if you had to back up three files in a financial
  471. program and you wanted to copy those files from drive A: to drive
  472. B: with a batch file, you could use the command:
  473.      FOR %%Z IN (TRNS.DOC CHKS.DAT DEPS.LST) DO COPY A:%%Z B:
  474. %%Z is a variable whose value is set sequentially to each name in 
  475. the collection of items.  The batch file would operate once as 
  476. though instructed to COPY A:TRNS.DOC B:.  It would then operate as 
  477. though instructed to COPY A:CHKS.DAT B:.  Finally, it would act as 
  478. though instructed to COPY A:DEPS.LST B:.  Since no items would
  479. remain in the set at this point, the loop would terminate.
  480.      DIRALL.BAT shows how the FOR command can keep track of files 
  481. within a complex directory structure that is possible in DOS 2.0. 
  482. For example, DIRALL.BAT, with the command DIRALL *.BAT, checks 
  483. each of the 14 subdirectories listed in the FOR commands, the 
  484. root directories of drives A: and C:, and reports to the printer 
  485. the existence of each batch file.  These reports are arranged 
  486. within each of the 16 subdirectories and the 2 root directories. 
  487. You would have to type 18 DIR commands, 1 for each directory, to 
  488. duplicate the behavior of the single command DIRALL.
  489.      DIRALL shows that you can intermix DOS command line
  490. variables (replaceable parameters) and the item variable of the
  491. FOR command in batch files.  The FOR command's item variables are 
  492. identified with a double percent sign and an alphanumeric character
  493. (%%0 through %%9 and %%A through %%Z).  The FOR command can also
  494. be used from the command line, in which case you must change the
  495. double percent signs preceding the letter variable to a single
  496. percent sign.  For example, at the command line you would type 
  497. FOR %Z IN (A:\B:\) DO DIR %Z to display the root directories of 
  498. drives A: and B:.
  499.      One of the most powerful techniques employed in programming 
  500. is the subroutine, a program statement or set of statements that 
  501. accomplishes a specific task.  Batch files have always been able 
  502. to call other batch files into execution.  Normally, however, the 
  503. second batch file does not return control to the first.  Although 
  504. the capability is not well documented in the DOS manual, in
  505. versions of DOS later than 2.0, a batch file itself can be a
  506. subroutine and can be invoked at any time by calling it with an
  507. instruction in another batch file.  Once it has accomplished its 
  508. task, the subroutine batch file automatically returns control to 
  509. the next instruction in the calling batch file.
  510.      You call for the batch file subroutine with the syntax:
  511.           COMMAND/C filespec [%# ...%#]
  512. COMMAND/C is the subroutine call (equivalent to GOSUB).  Filespec 
  513. lists the drive, the path, and the name of the batch file to use 
  514. as a subroutine; %# ...%# are the values for any replaceable 
  515. parameters in the subroutine.  The only special attribute of the 
  516. subroutine batch file is that it must contain the word EXIT as 
  517. its last command.  This command is the equivalent of RETURN in 
  518. many computer languages returns control to the parent batch file.
  519.      You could, for example, set up an AUTOEXEC.BAT file to
  520. perform three tasks: setting up the clock, setting up the monitor, 
  521. and setting up the printer. If each of these tasks were a
  522. subroutine, your AUTOEXEC.BAT file would consist of only three
  523. major instructions.  You would set up the file as follows:
  524.           ECHO OFF
  525.           CLS
  526.           COMMAND/C SETCLOCK
  527.           ECHO OFF
  528.           CLS
  529.           COMMAND/C SETMON
  530.           ECHO OFF
  531.           CLS
  532.           COMMAND/C SETPRINT
  533. The three files serving as batch subroutines would be:
  534.           SETCLOCK.BAT:  ECHO OFF
  535.                          CLS
  536.                          SETCLOCK
  537.                          EXIT
  538.  
  539.           SETMODE.BAT:   ECHO OFF
  540.                          CLS
  541.                          MODE 40,R
  542.                          EXIT
  543. and,
  544.           SETPRINT.BAT:  ECHO OFF
  545.                          CLS
  546.                          MODE COM1:96,N,8,1,P
  547.                          MODE LPT1:=COM1
  548.                          EXIT
  549.      You can "nest" batch file subroutines; that is, one
  550. subroutine can call another, which can call another, etc.  But be
  551. careful; each level of nesting will cost you 3K of RAM.  Also
  552. remember that DOS always turns on the echo at the beginning of
  553. any batch file.  To keep the echo off, begin each batch file with
  554. the command ECHO OFF.  Unfortunately, this command, like the first
  555. command of all batch files, is displayed.  But it disappears
  556. almost immediately if you follow it with a CLS command.
  557.      The AUTOEXEC.BAT file shown earlier is too simple to be of 
  558. much use, but it illustrates the techniques to use for more
  559. complex subroutine operations. By using batch file subroutines
  560. you can easily maintain and modify batch files of great complexity. 
  561. This technique also reduces debugging problems dramatically by 
  562. locating instructions in well-defined modules, and it enables you 
  563. to write the instructions for a commonly used procedure only once 
  564. and then call the routine from any batch file.
  565.  
  566. -----------------------------------------------------------------
  567.            BASIC Switches and Concurrently OPEN Files
  568.               (PC World July 1985 The Help Screen)
  569.  
  570.      You can use BASIC's switches and a filename with the DOS 
  571. commands BASIC or BASICA to call a BASIC program from DOS and 
  572. still use the BASIC switches /F (to set the maximum number of 
  573. files that may be OPEN simultaneously) and /S (to set the file 
  574. buffer size to match the maximum record length the BASIC program 
  575. uses) by putting the BASIC program you're calling as the second 
  576. parameter on the DOS command lines.  For example, in this excerpt:
  577.           .
  578.           SORT /+15 < A:SUBCEN.DAT > B:BAIN.DAT
  579.           BASICA BARPT
  580.           SORT /+73 < B:BAOUT.DAT > B:CASE.DAT
  581.           BASICA CASRPT /F:7/S:512
  582.           .
  583.      You can also include <stdin and >stdout redirection when 
  584. calling a BASIC program that switches, but the redirection
  585. parameters in a BASIC call must precede the switch parameters.
  586. You also need to include the line FILES=11 in your CONFIG.SYS
  587. file.  The FILES=x parameter sets the number of simultaneously
  588. OPEN files by reserving enough RAM for x file handles.  All file
  589. accesses -- reads, writes and closes -- can then be performed by
  590. telling DOS which file handle to use.  But because DOS uses three
  591. file handles for stdin, stdout, stderr, stdaux and stdprn, and
  592. BASIC uses one more file handle for LOAD, SAVE, CHAIN, NAME and
  593. MERGE commands, the FILES= parameter in the CONFIG.SYS file must
  594. be set to exceed by four the number in BASIC's /F: switch.
  595.  
  596. -----------------------------------------------------------------
  597.                         Quick Text Editor
  598.        (PC Magazine Vol 4 No 18 Sept 3, 1985 User-to-User)
  599.  
  600.      BUILD.BAT lets you create text files or short programs easily.
  601. When in DOS, enter "BUILD filename" where filename is the name of the
  602. file you wish to create.  BUILD.BAT will clear the screen, display a
  603. ruler line and save all your input in an ASCII file with whatever
  604. filename you assigned.  When you're finished entering text, hit the
  605. F6 key and Enter.  If a file with the same name as your filename
  606. already exists, BUILD.BAT will rename it with a .BAK extension.  By
  607. specifying PRN: as the filename, all text entered is dumped to the
  608. current list device.  This is useful for short memos or notes.
  609.      BUILD.BAT does not allow text to be edited except for the current
  610. line.  But it's useful to create small batch files or write memos or
  611. address envelopes quickly and easily in DOS.  And it's forgiving
  612. enough not to write over an existing file.
  613.  
  614. BUILD.BAT:
  615.  
  616. ECHO OFF
  617. IF %1==PRN: GOTO START
  618. IF NOT EXIST %1 GOTO START
  619. REN %1,????????.BAK
  620. :START
  621. CLS
  622. ECHO     5   10   15   20   25   30   35   40   45   50   55   60   65   70   75
  623. ECHO ----!----!----!----!----!----!----!----!----!----!----!----!----!----!----!----
  624. COPY CON:=%1
  625. :END
  626.  
  627. -----------------------------------------------------------------
  628.                          Quick One-Liner
  629.              (PC World September 1985 Star-Dot-Star)
  630.  
  631.      CPY.BAT copies files from the default disk drive to another drive,
  632. copying only those files that don't exist on the second drive:
  633.  
  634.           for %%F in (%1) do if not exist %2%%F copy %%F %2
  635.  
  636. CPY.BAT is invoked with the command, CPY filespec d:.  For example, the
  637. command CPY *.DOC A: copies all files with the extension .DOC from the
  638. default drive to drive A:.  Path names will not work with it unless the
  639. corresponding directories exist on both disks, and the source drive
  640. must be the default disk drive.
  641.  
  642. -----------------------------------------------------------------
  643.               Batching Back to a Previous Directory
  644.              (PC World October 1985 The Help Screen)
  645.  
  646.      Batch files are used extensively as menu selections for specific
  647. applications.  For example, if Lotus 123 is in a separate subdirectory,
  648. a batch file can automatically switch to that subdirectory, prompt the
  649. user to place the system disk in drive A:, and load LOTUS.COM.  When
  650. LOTUS.COM finishes, the batch file returns the user to the root
  651. directory and displays the main menu.
  652.      Since not all users are DOS wizards, a series of help screens
  653. for the DOS commands can be used using the HELP.BAT routines (PC World
  654. May 1985 and PC World December 1984).  However, if the help screens are
  655. kept in their own subdirectory, a problem arises.  To execute the
  656. IF EXIST %1.HLP batch command, which tests for the existence of a
  657. specified help file, you must be in the directory where the help
  658. screens are located.  To get HELP.BAT to work, you have to change to
  659. the HELP subdirectory.  In creating the batch file, the alternatives
  660. are to leave users in the HELP subdirectory or return them to the root
  661. and the main menu.
  662.      Editor's Note:  DOS 3.0 and later versions accept paths in the
  663. IF EXIST command, so you don't have to leave the current directory to
  664. test whether a file exists in another directory.  For DOS 2.x, output
  665. redirection and BASIC can get the user back to the directory from which
  666. HELP.BAT was called.  Modify HELP.BAT as follows:
  667.  
  668. echo off
  669. cls
  670. cd >c:\help\original.dir
  671. cd c:\help
  672. if exist c:%1.help goto does
  673. if x==%1x goto help
  674. echo Sorry.  Help unavailable for %1
  675. goto end
  676. :does
  677. type c:%1.hlp
  678. goto end
  679. :help
  680. type c:help.hlp
  681. :end
  682. pause
  683. basica c:reset_cd
  684.  
  685.      There are three basic differences between this HELP.BAT and the
  686. one used.  The line "cd>c:\help\original.dir" redirects the output of
  687. this CD command (display current directory) into a file named
  688. ORIGINAL.DIR.  The drive designator C: has been prefixed to the file
  689. names in HELP.BAT so that those files are found even when C: is not the
  690. default drive.  And the new batch file ending, "basica c:reset_cd"
  691. returns the user to the directory that was current when HELP.BAT was
  692. called.
  693.      Load BASIC(A) and create RESET_CD.BAS:
  694. 10 OPEN "C:ORIGINAL.DIR" FOR INPUT AS #1
  695. 20 INPUT #1,A$
  696. 30 CHDIR A$
  697. 40 SYSTEM
  698.      Use the command SAVE "C:\HELP\RESET_CD" to save this program in
  699. the HELP subdirectory on drive C:.  This program reads the name of the
  700. user's original subdirectory from the file ORIGINAL.DIR, changes to
  701. that directory and exits to DOS.
  702.      This routine assumes that BASIC(A) and HELP.BAT are available via
  703. the extended directory search path defined by the latest PATH command.
  704. For example, with HELP.BAT in a directory called BATCH and BASIC(A) in
  705. a directory called DOS, your hard disk's AUTOEXEC.BAT file might
  706. contain the command "PATH=C:\BATCH;C:\DOS;" so that the commands in
  707. those directories are available from any directory.
  708.  
  709. -----------------------------------------------------------------
  710.                  Creating Batch Files With Echo
  711.              (PC World November 1985 Star-Dot-Star)
  712.  
  713.      You can reduce the space that batch files use by having the ECHO
  714. command create them on a RAM disk.  Since a single batch file can
  715. contain any number of ECHO commands, a couple of large batch files
  716. filled with ECHO commands can replace dozens of the space-wasting kind.
  717.      An as example, you can include the line:  echo basica menu.bas/
  718. f:6 > d:menu.bat  among others in an AUTOEXEC.BAT file.  (You can
  719. place it in any batch file, as long as it executes before you need the
  720. file it creates.)  The greater-than symbol sends the output of the ECHO
  721. command to a file instead of to the screen.  When the AUTOEXEC.BAT file
  722. executes, it creates the batch file MENU.BAT on drive D:.  The RAM disk
  723. runs the batch file more quickly than a floppy.
  724.      This technique can be enhanced by using the FOR subcommand.  For
  725. example, the command:  echo for %%%%f in (cls dbase pause d:menu) do
  726. %%%%f > d:db.bat  creates a batch file that clears the screen, runs
  727. dBASE, waits for you to press a key, and then runs a menu program on
  728. drive D:.  Note that the lines use quadruple percent signs; they are
  729. necessary because DOS strips some of them as the command executes.
  730.      Editor's Note:  You can create multiline batch files by sending
  731. several ECHO statements in a row to the same file, using double
  732. greater-than signs to append the new lines to those already sent.
  733. For example, if you place the lines shown in BUILDBAT.BAT below in an
  734. AUTOEXEC.BAT or other batch file, they create a five-line batch file
  735. called D.BAT, which turns echo off, clears the screen, changes the
  736. DOS prompt, waits for the user to press a key, and then lists the
  737. directory of the current disk.  Also, with the aid of the IF subcommand
  738. it's easy to create a complex menu system that uses just two batch
  739. files.
  740. - - - - -
  741. BUILDBAT.BAT:
  742.  
  743. echo echo off > d.bat
  744. echo cls >> d.bat
  745. echo prompt $p$g >> d.bat
  746. echo pause >> d.bat
  747. echo dir >> d.bat
  748.  
  749. -----------------------------------------------------------------
  750.                           Batch Tricks
  751.        (PC Magazine Vol 4 No 26 Dec 24, 1985 User-to-User)
  752.  
  753.      With DOS 2.1, it's simple to create blank lines in batch files by
  754. typing ECHO following it with two spaces.  However, in DOS 3.1, this
  755. trick won't work.  DOS will interpret an ECHO followed by blanks as a
  756. request for the current ECHO status, and since most users turn ECHO OFF
  757. in the first line of a batch file, all you'll get is an "ECHO is off"
  758. message.
  759.      If you create your batch files using the DOS COPY CON: command,
  760. the way to get around this and end up with a blank line in DOS 3.1 is
  761. to type in the word ECHO and then hold down the space bar until the
  762. cursor wraps to the next line.  When the cursor moves down to the next
  763. line, type your message.
  764.      Another batch file problem is that it's handy when writing
  765. instructions for novices to refer to the Enter key by actually drawing
  766. on-screen the crooked arrow that IBM put on the Enter key.  You can
  767. assemble a representation of this crooked arrow by printing characters
  768. 17, 196, 196, and 217.  To see this, run this BASIC program:
  769.  
  770. 10 PRINT CHR$(17);
  771. 20 PRINT STRING$(2,196);
  772. 30 PRINT CHR$(217)
  773.  
  774.      However, while DOS allows you to print virtually any of the 256
  775. possible characters on the screen by holding down the Alt key, typing
  776. in the ASCII number of the character on the number pad, and then
  777. releasing the arrow key, it can't handle characters with ASCII values
  778. lower than 32.
  779.      One way to create an arrow is to first load BASICA, type in a
  780. line number, a space, the "remakr" statement REM, and a space.  Then
  781. use the Alt plus number pad trick with 17, 196, 196, and 217.  Save the
  782. file as ARROW.BAS.  Then load your word processor and delete the line
  783. number and the REM statement, leaving just the arrow itself.  If you
  784. use WordStar, the arrow will appear as ^QDDY, but it will resemble an
  785. arrow once you're back in DOS.  They you can insert this pictorial
  786. arrow into any batch file instead of having to say "Hit the <Enter>
  787. key."
  788.      Editor's Note:  While the first technique does work, you have to
  789. add several extra spaces at the beginning of your message on the second
  790. line to compensate for the space taken up by the word "ECHO," or else
  791. this message will start as the end of the first line.  Also, the DOS
  792. COPY CON: trick can handle only 127 characters, which limits the kinds
  793. of messages you can write.  A better way to end up with a blank line
  794. in DOS 3.1 is to type ECHO, follow it with one or two spaces, and then
  795. follow the spaces immediately with a CHR$(255), which you can produce
  796. by holding down the Alt key, typing 255 on the number pad, and then
  797. releasing the Alt key.  The ECHO+space+space+CHR$(255) blank line trick
  798. works in DOS 3.1.  Who knows what Microsoft and IBM will do with future
  799. versions?
  800.      There are two easier ways to display the "arrow" (Enter) key.  One
  801. is to write a small BASIC file:
  802.  
  803. 10 OPEN "arrow." FOR OUTPUT AS #1
  804. 20 PRINT #1,CHR$(17);
  805. 30 PRINT #1,STRING$(2,196);
  806. 40 PRINT #1,CHR$(217)
  807. 50 CLOSE
  808.  
  809. Another even simpler way is to use DEBUG:
  810.  
  811. A>debug
  812. -n arrow
  813. -e 100 11 c4 c4 d9
  814. -r cx
  815. -4
  816. -w
  817. -q
  818.  
  819. -----------------------------------------------------------------
  820.                           Disk Memo Pad
  821.        (PC Magazine Vol 4 No 26 Dec 24, 1985 User-to-User)
  822.  
  823.      The DD.BAT file creates a small memo or diskette description file
  824. and lets you update it automatically without having to use a word
  825. processor.  You can copy the small DD.BAT file to all your disks and
  826. use it to remind yourself what's on the disk -- and update the reminder
  827. automatically.  After you've entered any information, typing DD will
  828. display it.  To update the information, just type DD followed by up to
  829. nine words of text.  The next time you type DD the new message will
  830. appear, appended to the old.
  831.      Editor's Note:  The nice thing about this is that you can leave
  832. memos to yourself up to 23 lines long and update the memos without
  833. having to load a word processor.  The ^G in the 7th line from the
  834. bottom is a beep.  You can enter this in WordStar by typing Ctrl-P
  835. Ctrl-G.
  836.  
  837. DD.BAT:
  838.  
  839. echo off
  840. if z==z%1 goto display
  841. echo %1 %2 %3 %4 %5 %6 %7 %8 %9 >> %0.doc
  842. :display
  843. cls
  844. if not exist dd.doc goto :oops
  845. type %0.doc
  846. goto end
  847. :oops
  848. echo ^GYou haven't entered anything yet .....
  849. echo To enter date, type DD and then type up
  850. echo    to 9 words on each line.
  851. echo You can enter up to 23 lines.
  852. echo ----
  853. echo To see what you've typed, just type DD
  854. :end
  855.  
  856. -----------------------------------------------------------------
  857.                            ...To the Rescue
  858.          (PC Magazine Vol 3 No 14 July 24, 1984 User-to-User)
  859.  
  860.      Most computers can provide a help facility.  By typing the word
  861. HELP, optionally followed by a program name, the user gets a quick
  862. how-to on the requested program. 
  863.      Create the batch file HELP.BAT (below).  Then, using your word
  864. processor (of simply by typing COPY CON:<filename>:HLP, create files
  865. with the .HLP extension that explain each of the programs.  With this
  866. batch file and your .HLP files, you can keep lots of information at
  867. your fingertips.  To hold the files in a subdirectory called HLPFILES,
  868. use CD\HLPFILES at the DOS prompt.
  869.  
  870. HELP.BAT:
  871. echo off
  872. cls
  873. if %1==SUBJECTS goto subjects
  874. if %1==subjects goto subjects
  875. if not exist \hlpfiles\%1.hlp goto nofile
  876. copy \hlpfiles\%1.hlp con
  877. goto endbatch
  878.  
  879. :subjects
  880. dir \hlpfiles\*.hlp/w
  881. goto endbatch
  882.  
  883. :nofile
  884. echo Type HELP followed by the file name (or HELP * for all)
  885. echo Type HELP subjects for a list of available topics
  886.  
  887. :endbatch
  888.  
  889. -----------------------------------------------------------------
  890.                        Magic Batch Fingers
  891.        (PC Magazine Vol 4 No 2 Jan 22, 1985 User-to-User)
  892.  
  893.      Some DOS commands, such as FORMAT and ERASE*.*, require keyboard
  894. input and thus cannot be used in an unattended batch file.  For
  895. example, if you enter FORMAT A:/V, you will be instructed to place the
  896. disk in drive A: and to press any key to continue.  You will then be
  897. asked to enter a VOLUME ID for the disk, and then whether you want to
  898. format another.  If this FORMAT command were command executed from a
  899. batch file, the batch processing would stop and wait for your keyboard
  900. responses to these prompts.  Here's a method to eliminate the keyboard
  901. input.
  902.      First, either with a word processor or using the DOS COPY CON:
  903. command, construct a file called RESPONSE that contains the responses:
  904.           A>COPY CON:RESPONSE
  905.  
  906.           NEWDISK
  907.           N
  908.           ^Z
  909.  
  910. The first line copied to the file is a blank line produced by just
  911. pressing Return.  This is important since it contains the Return key
  912. press needed in response to "Press any key to continue."  NEWDISK will
  913. be the VOLUME ID.  N is the response to the prompt, "Format another
  914. (Y/N)?".  ^Z is the EOF marker.
  915.      Next, place the following line in the batch file where you would
  916. normally put the FORMAT command:
  917.           TYPE RESPONSE|FORMAT A:/V
  918. The TYPE command would normally display the contents of RESPONSE on the
  919. screen, but the | symbol instead pipes the contents of RESPONSE to the
  920. inputs in the FORMAT command, so they answer the prompts that would
  921. normally be answered from the keyboard.
  922.      This same method can be used to answer the "Are you sure?" prompt,
  923. which comes up if you enter ERASE*.*.  The keyboard inputs in these DOS
  924. commands are safety features that help prevent accidental erasures by
  925. letting the operator verify the command entry before it executes, so be
  926. sure to use them with great care.
  927.      Editor's Note:  This trick works wonders automating batch files
  928. and speeding up things in general.  For instance, if you're performing
  929. lots of DISKCOPYs, you can load the disk to be copied onto a RAMdisk
  930. and then write a batch file to DISKCOPY to A: and then B: and then go
  931. back to the beginning and copy A: again - all without having to enter
  932. lots of Y's to continue.  This lets you replace copied disk with raw
  933. ones in rapid succession.  This also lets you do such nasty things as
  934. putting DEL*.* commands in places that don't need confirmations.  If
  935. you're putting together a big batch file with lots of commands that
  936. would ordinarily need responses, be sure the responses are synchronized
  937. to the questions.  If a complex batch file encounters an error, you
  938. could end up confirming some very destructive commands.  You can do
  939. serious damage, even with simple files.  For example, if you create a
  940. file called TROUBLE with just a Y and carriage return in it, then type:
  941.           TYPE TROUBLE|DEL*.*
  942. your disk suddenly turns blank.  If you do try this, you can replace
  943. all the files with DEBUG by using the L command to load in the
  944. directory and then change all the hex E5's back to their original
  945. characters.
  946.