home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / games / batutor.zip / BATUTOR.TXT
Text File  |  1987-01-16  |  33KB  |  700 lines

  1.  
  2.                           Intermediate .BATch Tutor
  3.                                     -by Barry Simon
  4.  
  5.  
  6.          (c) Copyright 1986 by Capital PC User Group, Bethesda, MD.
  7.  
  8. [Editors  note:   This  article was published November, 1986 in the "Monitor"
  9. published by the Capital PC User Group.  Material may be reproduced only  for
  10. internal use by other not-for-profit groups.]
  11.  
  12.  
  13.  
  14. One of the tools that most distinguishes the experienced DOS user from others
  15. is the effective use of BATch files.  In this article, I will discuss some of
  16. the  more  advanced  features of BATch files and their commands.  I call this
  17. tutorial "intermediate" because I have tried to write in a manner  accessible
  18. to those with only a little experience writing BATch files.
  19.  
  20.  
  21.                ------------[ Simple BATch files ]------------
  22.  
  23. To begin with, a BATch file is a set of DOS commands which you place together
  24. and which you can issue by just typing the  name  of  the  BATch  file.   The
  25. classic  example  is the BATch file which changes to the directory containing
  26. Lotus' 1-2-3 and then runs 1-2-3.  The same idea can be carried much further:
  27. for example, when I execute my word processor with a batch file, it
  28.  
  29.    -- loads the proper set of macros in my keyboard macro program;
  30.  
  31.    -- makes a mark using Kokkennen's MARK/RELEASE package;
  32.  
  33.    -- loads my thesaurus;
  34.  
  35.    -- loads the appropriate mouse menu program;
  36.  
  37.    -- runs the word processor, and
  38.  
  39.    --  after I am finished with the word processor, clears the macros and
  40.        runs RELEASE to boot the thesaurus and mouse menu from memory.
  41.  
  42. BATch  files must be pure ASCII text files with separate commands on distinct
  43. lines separated by carriage return/line feed pairs.   To  create  your  BATch
  44. files,  you can use EDLIN, a text editor, or any word processor that produces
  45. straight ASCII text files.
  46.  
  47. BATch programs can contain DOS commands, application program commands,  or  a
  48. variety of specialized programming features that are known as BATch commands.
  49.  
  50.  
  51.  
  52.            ------------[ BATch Commands and Features ]------------
  53.  
  54. Parameters......
  55.  
  56. Your  text  editor may allow you to specify a text file to use as a parameter
  57. on the command line loading it.  You would like to specify the file name as a
  58. parameter  on  the command line calling a BATch file to start your editor and
  59. have this file name passed on to the editor when it is loaded.  This is quite
  60. easy.   If  your editor is called EDITOR.EXE and you load it via a BATch file
  61. called foo.bat, you need only make the line calling the editor say:
  62.  
  63.                editor %1
  64.  
  65. and then call up the BATch file as
  66.  
  67.                foo filename
  68.  
  69. When the BATch processor comes to the %1, it looks for  the  first  parameter
  70. following  "foo" and replaces %1 by that parameter, in this case by filename.
  71.  
  72. To be more precise, DOS parses the command line calling the BATch  file  into
  73. distinct "words" separated by one of four delimiters:
  74.  
  75.                <space> , ;  =
  76.  
  77. That  is, it looks for strings of allowed characters separated by one or more
  78. of these special four.  Any of the 256 ASCII characters are  allowed  in  the
  79. strings  except  for  these four delimiters, the three redirection characters
  80. (<,>,|) and the ASCII nul.  The first word on the command  line  is  assigned
  81. the  value  %0, the next %l, etc.  Normally, %0 is just the name of the BATch
  82. file but since "." is not a delimiter in parsing the line but is a  delimiter
  83. in ending file names, if you have a BATch file foo.bat and type in
  84.  
  85.                foo,cpcugisgreat
  86.  
  87. foo.bat will be run and %0 will have the full 17 character string assigned to
  88. it.  Similarly,  since  DOS  knows  that  file  names  are  no  more  than  8
  89. characters, the BATch file 12345678.bat  will be run if you type in
  90.  
  91.                12345678ABCDEFGHIJ
  92.  
  93. but  %0  will  have an 18 character string.  These are admittedly curiosities
  94. but DOS curiosities have a knack of being useful sometimes.  In a real sense,
  95. DOS  assigns  as  many parameters as there are words on the command line, but
  96. initially you can only access the first ten as %0 through %9.  Any place that
  97. %1  appears in the BATch file except as %%1, it will be replaced by the first
  98. word after the filename even if that word is the empty word.  Any other  time
  99. that  the  symbol % appears in the BATch file, DOS will strip the %-sign away
  100. except that %% is replaced by a single percent  and  %%1  becomes  %1  so  if
  101. foo.bat has the single line:
  102.  
  103.                echo %%1 has the value %1
  104.  
  105. then typing
  106.  
  107.                foo junk
  108.  
  109. will issue the message
  110.  
  111.                %1 has the value junk
  112.  
  113. There  is  no  limit  on  the size of any individual parameter other than the
  114. overall limitation that the entire DOS command line can contain no more  than
  115. a total of 128 characters.
  116.  
  117. To  summarize:   any time %i (for i=0,1,...,9) occurs in the file except as a
  118. %%i, it will be replaced by the ith string in the command line.
  119.  
  120. The real limitation of BATch file parameters is that they are not variables.
  121. You cannot manipulate them by parsing them and cannot change their values.
  122.  
  123.  
  124. Labels......
  125.  
  126. As a preparation for discussing BATch file GOTO commands, I need  to  discuss
  127. labels.   Any  line  beginning  with the character ":" will be skipped by the
  128. BATch processor when it gets to it.  This means that you can use  such  lines
  129. as  remark  lines  which will not appear on the screen even if echo is on (in
  130. which case lines beginning with rem do  appear).   You  can  also  place  the
  131. redirection  characters  <,>,|  on  such  a  line  (but you cannot place such
  132. characters on a "rem" line).  And % signs are not treated specially  on  such
  133. lines.
  134.  
  135. While DOS ignores such lines, the string following the :  becomes a label for
  136. the GOTO command which I'll discuss next.  The first word or the first  eight
  137. characters  of  the  first  word become the name of that label.  You can also
  138. place comments after a label name if  you  separate  these  comments  with  a
  139. space.
  140.  
  141.  
  142. GOTO......
  143.  
  144. The  most significant way in which BATch files go beyond the DOS command line
  145. concerns two logical control structures, the GOTO and the IF  commands.   The
  146. line
  147.  
  148.                goto <label>
  149.  
  150. will  send the BAtch file to the line immediately following the line on which
  151. the label appears.  If the label appears more than once, its first appearance
  152. in  the  file  is  used.  If the label does not appear, the processing of the
  153. BATch file is abruptly ended with the message "label not found."
  154.  
  155. GOTO's can be used for either branching or some rather crude  looping.   Here
  156. is  a  simple  example  of  branching.  Suppose that you wish to send control
  157. codes to your printer easily from the DOS command line.   You  might  prepare
  158. little  files  like  ff.txt containing a formfeed (control- L), or boldon.txt
  159. with the codes for turning bold  face  on,  etc.   Then  you  could  write  a
  160. print.bat file which began:
  161.  
  162.                goto %1
  163.                :ff
  164.                copy ff.txt prn
  165.                goto end
  166.                :boldon
  167.                copy boldon.txt prn
  168.                ETC
  169.  
  170. with a label ":end" as the last line of the file.
  171.  
  172. One  problem  with the limited logic allowed by BATch files is the difficulty
  173. of error checking.  With the above, if you forget to put in  a  parameter  or
  174. put in an illegal value, you would get the rather inelegant "label not found"
  175. message.  Alternatively, you could replace the "goto %1" with separate  lines
  176. such as:
  177.  
  178.                if %1 = = ff goto ff
  179.  
  180. and then place an error message, hardly a neat solution.
  181.  
  182.  
  183. PAUSE......
  184.  
  185. The  next  BATch command that I'll discuss, PAUSE, unlike GOTO and paramaters
  186. makes sense at the DOS  command  line.   For  straight  DOS  usage,  this  is
  187. pointless  but  for  environments like CED or TallScreen which allow multiple
  188. commands on one line, it can be useful.   What  PAUSE  does  is  display  the
  189. message:
  190.  
  191.                Strike a key when ready ...
  192.  
  193. and pause the processing of the BATch file until a key is struck.  Before the
  194. message  is  displayed,  the  keyboard buffer is flushed to prevent any stray
  195. keystrokes left over from previous operations from bypassing  the  effect  of
  196. the PAUSE command.
  197.  
  198. There  are  three  rather  distinct uses of the PAUSE command.  The most well
  199. known is to allow the user to take an action like inserting a diskette into a
  200. drive.  Often, you will want to echo a message to the screen before the PAUSE
  201. command such as:
  202.  
  203.                echo Place a diskette into drive B: and ...
  204.                pause
  205.  
  206. A second use is to allow the user to see the output of  some  program  before
  207. the  BATch file continues.  Without the PAUSE, the output might disappear too
  208. quickly to absorb.  A third use that we'll see later is  an  extremely  crude
  209. device for user input.
  210.  
  211.  
  212. The IF Command......
  213.  
  214. The  second  and  last  element  of  flow  control  provided by DOS is the IF
  215. command.  Like pause, it also makes sense  at  the  DOS  command  line.   The
  216. syntax is
  217.  
  218.                IF (not) <comparison> <consequence>
  219.  
  220. The not is an optional part which we'll explain later.  Comparison is limited
  221. to three types:
  222.  
  223.    -- A test for the equality of two strings;
  224.  
  225.    -- A test for the existence of a file;  and
  226.  
  227.    -- A test of the "error level."
  228.  
  229. If the comparison is true, then the command in "consequence" will get carried
  230. out.  If the word "not" is included, then <consequence> is carried  out  only
  231. if  the  comparison  fails.  While consequence is typically a GOTO statement,
  232. any acceptable DOS or BATch command  is  allowed.   You  can  even  have  the
  233. consequence be a second if statement as in
  234.  
  235.                if not %1 = = ff if not % 1 = = boldon goto error
  236.  
  237. As  the  last line shows, the syntax for checking strings is to use two equal
  238. signs.  Strings are  sequences  of  characters  not  including  the  standard
  239. delimiters  (space,  comma,  semicolon or equal) or redirection signs.  These
  240. redirection signs but not delimiters can appear if the string or part of  the
  241. string  is  surrounded  by  single or double quotes.  If an illegal string is
  242. used, DOS will reward you with "bad command or filename." There is a standard
  243. problem faced by users first exposed to BATch files.  Often, you will want to
  244. test for an empty string, for example to give an error message  if  the  user
  245. failed  to  provide  a necessary parameter.  Empty strings are not allowed so
  246. that the line
  247.  
  248.                if %1 = = echo whoops
  249.  
  250. will yield "syntax error" if %1 is empty, and "bad command or file- name"  if
  251. %1  has  the value "echo" unless of course you have a program named "whoops."
  252. The way out of this problem is to use the fact that %1 is  replaced  wherever
  253. it is and add a dummy parameter as in
  254.  
  255.                if a %1 = = a echo whoops
  256.  
  257. The  possibility  of looking for a file provides essential error checking.  A
  258. common example of its use is to fix the deadly move BATch file:
  259.  
  260.                copy %1 %2 \ %1 erase %1
  261.  
  262. This may seem like a great way of moving a file from one directory to another
  263. so that
  264.  
  265.                move foo.txt C:  \ junk
  266.  
  267. will move foo.txt from the current directory to junk.  But if  the  directory
  268. junk  doesn't  exist,  the  first  line  will give the error message "Invalid
  269. directory" and the second line will erase the file which has NOT been copied.
  270. Thus, as a protection you should add before the "erase %1" line
  271.  
  272.               if not exist %2 \ %1 goto end
  273.  
  274. with  an  appropriate  label  and error message.  There are many other places
  275. where such error checking can be invaluable.  Unfortunately, "if exist" works
  276. with path names only in DOS 3.x and not in DOS 2.x.
  277.  
  278. The final if comparison, "if errorlevel," is useless within  the  context  of
  279. pure DOS commands since no DOS command (before DOS 3.2) sets errorlevel.  But
  280. the DOS technical reference describes how a program can  set  this  parameter
  281. and if you have a program that sets it, this comparison is useful.
  282.  
  283. But I must warn you that the comparison
  284.  
  285.                if errorlevel 2
  286.  
  287. is  TRUE  not only if the value of errorlevel is 2 but if it is 2 or greater.
  288. Beginning with DOS  3.2,  several  external  DOS  commands  such  as  BACKUP,
  289. RESTORE,  and  FORMAT set what the DOS manual calls "exit codes" which can be
  290. tested with "if errorlevel."
  291.  
  292.  
  293. Nested and Chained BATch Files......
  294.  
  295. It is possible to make rather elaborate collections of BATch files which call
  296. one  another.   The  simplest  is  the  equivalent of a GOTO.  If the name of
  297. another file appears as a line in one BATch file, the BATch file  will  begin
  298. and you will NOT return to the first BATch file when the second one ends.
  299.  
  300. Particularly  since the BATch control structures (GOTO and IF) do not include
  301. CALL/RETURN processing of subroutines, one would like to be able  to  call  a
  302. second BATch file as a subroutine and return to the first BATch file when the
  303. second one finished.  Many books, especially the older ones,  will  tell  you
  304. that  this is not possible.  However, in DOS 2.x and later, there is a method
  305. of doing precisely this kind of subroutine  call.   This  is  an  example  of
  306. something  which is not undocumented but rather so badly documented that many
  307. experts did not realize its potential until some time had passed.  The reason
  308. that an ordinary call to a second BATch file does not return you to the first
  309. is that COMMAND.COM is only able to  keep  track  of  one  BATch  file.   The
  310. solution  is  to  load  a secondary copy of COMMAND.COM to process the second
  311. BATch file.  The correct syntax is
  312.  
  313.                command/c foo
  314.  
  315. to call foo.bat from another BATch file.  When foo ends,  processing  of  the
  316. first BATch file will continue on the next line.
  317.  
  318. Several  warnings are in order concerning this command.  As you may know, DOS
  319. has both internal commands like COPY and external commands like FORMAT.  That
  320. is,  once  COMMAND.COM  is  loaded  during  initial  bootup,  COPY becomes an
  321. available command while FORMAT is a stand alone program  provided  separately
  322. on the DOS disk, which must reside in the default directory or in the path to
  323. be available in the user.  You might  think  that  COMMAND  was  an  internal
  324. command but it is external.  That is, in order for a line like
  325.  
  326.                command/c foo
  327.  
  328. to  work,  COMMAND.COM  must  be available in the default directory or in the
  329. path.  In addition if you leave an old version of  COMMAND.COM  somewhere  on
  330. your  hard  disk and that happens to be the one first found when this line is
  331. processed, you will get a message "incompatible DOS versions." Finally, there
  332. is  a  warning about the SET command discussed in the section below on global
  333. variables.
  334.  
  335.  
  336. FOR...IN...DO
  337.  
  338. FOR...IN...DO is one of DOS' most powerful and  least  appreciated  commands.
  339. It  can  be  used  at  the  DOS command line or in a BATch file with slightly
  340. different syntax.  The syntax at the DOS command line is
  341.  
  342.                for %a in (<list>) do expression (%a)
  343.  
  344. The  use  of  a  in %a is not important;  you can use any letter or number or
  345. even extended ASCII code so long as it appears  in  all  places.   The  %  is
  346. important  and since %'s get stripped from BATch files, the syntax in a BATch
  347. file must be "for %%a...".  The keyword "do" must appear or you  will  get  a
  348. "syntax  error"  message.   Within  the () can appear a list of ASCII strings
  349. separated by the standard delimiters.  Following the keyboard "do" can be any
  350. command  with  %a  as  a parameter which will be successively replaced by the
  351. values listed within ().  For example
  352.  
  353.                for %a in (file1 file2 file3) do copy %a prn
  354.  
  355. will issue three separate commands copying the three files to prn.
  356.  
  357. The power of the FOR...IN...DO construction comes  because  you  can  include
  358. wildcards  and  DOS  will  substitute  in  turn  each filename satisfying the
  359. wildcard, so for example.
  360.  
  361.                for %a in (file?) do copy %a prn
  362.  
  363. would do the same thing as the above command if there  were  no  other  files
  364. with the name file?  Therefore, the greatest use of FOR...IN...DO is to force
  365. programs which don't understand wild cards to understand them  none-the-less!
  366. For  example,  when  I  discussed  file viewers, I talked about both LIST and
  367. FILEVIEW.  LIST allows you to give wildcard filenames  so  "list*.doc"  would
  368. successively  view  all  files  with a doc extension.  FILEVIEW does not have
  369. this built in but you can do it yourself:  rename fv.exe fv@.exe and  make  a
  370. one line BATch file called fv.bat saying
  371.  
  372.                for %%a in (%1) do fv@ %%a
  373.  
  374. and suddenly FILEVIEW also has this capability.
  375.  
  376. You  can also force commands which don't treat wildcards "properly" to do so.
  377. For example, if you want to change the date/time stamp on a file foo.  txt to
  378. the current date and time, the command
  379.  
  380.                copy/b foo.txt+,,
  381.  
  382. will  do  precisely  that.  If one replaces "foo.txt" in the above command by
  383. *.*, DOS will create one file which is the combination of each  file  in  the
  384. current  directory,  probably not what you intended.  To change the date/time
  385. of all files use
  386.  
  387.                for %a in (*.*) do copy/b %a+,,
  388.  
  389. You can combine multiple commands and wildcards as in
  390.  
  391.                for %a in (*.pas*.com) do copy %a A:
  392.  
  393. If  you want to stop such a command before it has run its course, Ctrl-C will
  394. give you the opportunity to break the loop.  In addition you should be warned
  395. that  a  FOR...IN...DO  loop  is like a BATch file;  if the expression called
  396. after the keyword "do" involves a BATch file, processing will not  return  to
  397. the  FOR...IN...DO loop.  It will only be processed one time even if you have
  398. several possibilities in your list.  As  happens  for  BATch  files,  placing
  399. command/c will prevent the chain breaking so
  400.  
  401.                for %a in (*.*) do command/c foo %a
  402.  
  403. issued at the DOS command line would run the BATch file foo.bat  successively
  404. with each file in the default directory as a paramter.
  405.  
  406. The  BATch  processor  is  rather inefficient.  It reads only one line of the
  407. file into memory at a time (try including a line like "erase  foo.bat"  in  a
  408. BATch  file  to see what happens) although it keeps its place by counting the
  409. byte offset (try using your editor on foo.bat as a line in foo.bat to explore
  410. this).  By using the FOR...IN...DO command to reduce the number of lines in a
  411. BATch file, you can  speed  up  processing  by  a  noticeable,  although  not
  412. spectacular,  amount  (perhaps by 10%).  For example, if you have a large RAM
  413. disk and routinely copy several directories to it during bootup try:
  414.  
  415.             for %%a in (path 1 path 2) do copy %%a \ *.* D:  >nul
  416.  
  417. Two warnings about this procedure:  you cannot include a command with its own
  418. parameters in the list since DOS processes the list in () one word at a time.
  419. Second, for technical reasons, I strongly recommend against loading  resident
  420. programs in such a statement.
  421.  
  422.  
  423. SHIFT......
  424.  
  425. As I described above, when DOS parses the input command line to a BATch file,
  426. only the first 10 words are available as %0 to  %9.   However,  DOS  keeps  a
  427. record  of the entire line.  One of the things that the SHIFT command does is
  428. provide you with access to the other parameters.   A  line  with  the  single
  429. command  SHIFT drops %0 changes %1 to %0,..., %9 to %8 and makes what was the
  430. eleventh word on the original command line into the new %9.
  431.  
  432. Additional calls to SHIFT repeat this process.
  433.  
  434. If SHIFT were only good for accessing parameters beyond the initial ten, it
  435. would be of limited use.  I cannot find any interesting use of this aspect of
  436. the command.  Its real use lies in the possibility of having a real loop
  437. within a DOS BATch file.  Here is a sample "touch.bat" expanding the one line
  438. procedure for updating the date/time stamp of a file:
  439.  
  440.                :top
  441.                if %1x=x goto end
  442.                for %%a in (%1) do copy %%a+,,
  443.                shift
  444.                goto top
  445.                :end
  446.  
  447. With this BATch file, you can issue the command  touch  followed  by  several
  448. parameters, each with wildcards.  The basic operation is applied to each file
  449. meeting any of the filespecs listed as  parameters.   Of  course,  you  could
  450. probably do just as well with the single line.
  451.  
  452.                for %%a in (%1 %2 %3 %4 %5) do copy %%a+,,
  453.  
  454. While  I  can  develop considerable enthusiasm for the FOR...IN...DO command,
  455. SHIFT does not evoke much excitement!
  456.  
  457.  
  458. ECHO......
  459.  
  460. Next, we turn to the issue of  displaying  information  on  the  screen,  the
  461. primary  method  of  communication  between  the writer of BATch file and the
  462. user.  Even if you are writing BATch files for yourself, it  pays  to  expend
  463. some  effort  in  making attractive, instructive displays.  By default, every
  464. line in the BATch file appears on the screen.  While this has  the  potential
  465. advantage  of  showing  the  user what is going on, usually the display is so
  466. fast that it is more distracting than useful.  You can turn off the automatic
  467. display of commands to the screen with the command.
  468.  
  469.                echo off
  470.  
  471. When echo  is  off,  you  can  display  messages  by  preceding  the  desired
  472. communication  with  the  keyword ECHO.  The default for BATch file should be
  473. echo off and there are patches for COMMAND.COM to make that the  default.   I
  474. have avoided this patch because such a patch is not immediately available for
  475. new versions of DOS.  Unless you make this patch, most of  your  BATch  files
  476. should begin with
  477.  
  478.                echo off
  479.                cls
  480.  
  481. It is not necessary to turn echo on before  exiting  a  BATch  file;   it  is
  482. turned  back  on automatically.  You can suppress the prompt on a DOS command
  483. line by typing "echo off" at the DOS prompt and then "echo on"  is  necessary
  484. to restore the default.
  485.  
  486. ECHO  can  be a useful command.  Its message is sent to standard output which
  487. means that you can redirect it and so use it to send codes to  your  printer.
  488. But be warned that a CR/LF pair is always appended to an echo line so
  489.  
  490.                echo ^L>prn
  491.  
  492. (where  ^L means control-L the form feed character) will not send only a form
  493. feed to your printer, it will send an extra line feed.  Also, you will not be
  494. able  to  send  escape  codes to your screen or printer directly from the DOS
  495. command line.  DOS reacts to your <Esc> key by aborting the  current  command
  496. instead  of placing an escape character at the cursor.  With many editors you
  497. can put escape characters into a file and so write BATch files to send escape
  498. codes to the screen or printer.
  499.  
  500. One  character you may want to use is ^G (Control-G entered by depressing the
  501. control key and hitting g).  This is the "BEL" character and will  beep  when
  502. sent to the screen.  So
  503.  
  504.                echo ^GYou made an error
  505.  
  506. will catch the user's attention.  But please control yourself:  avoid putting
  507. several ^G's together.  And with most printers
  508.  
  509.                echo ^G>prn
  510.  
  511. will beep the printer.
  512.  
  513. A famous problem is how to use ECHO  to  make  blank  lines.   A  BATch  file
  514. command  line  with the single word "echo" will not work as this will get the
  515. response "echo is on" or "echo is off".  DOS 2.x  had  a  celebrated  undocu-
  516. mented  feature:   "echo<space>"  did produce a blank line by relying on this
  517. undocumented feature caused grief when DOS 3.x  gave  the  "echo  is  on/off"
  518. response.  Some vesions of DOS have had
  519.  
  520.                echo .
  521.  
  522. (note  the  period) produce blank lines but this has not always been true and
  523. had led to some rather fancy programs with installation files showing bunches
  524. of  dots  on  the screen.  There is a rather simple solution.  To get a blank
  525. line try
  526.  
  527.                echo <char 255>
  528.  
  529. where <char 255> means including the character  with  ASCII  code  255.   You
  530. can't enter this in all editors but in many (e.g.  EDLIN), you can by holding
  531. down the <Alt> key and hitting 2 5 5 on the numeric keypad.  Lest you be wary
  532. of  using  an  undocumented  feature  of DOS, this trick merely relies on the
  533. documented feature of "echo" to send any  character  even  those  with  codes
  534. above  128  to  the screen:<char 255> is a blank character.  You can also use
  535. one of these characters to anchor down a message that you don't want to start
  536. in column one.
  537.  
  538. Even  if  echo  is  off,  the  last line in the BATch file will appear on the
  539. screen if that line does not end in a carriage return /line feed  pair.   For
  540. this  reason,  you  will probably want to be sure that the last lines in your
  541. BATch files have such a CR/LF pair at the end.  The exception is if the  last
  542. line is "cls".  In this case the echo to the screen is unimportant and adding
  543. a CR/LF will sometimes place the prompt on line three instead of line two.
  544.  
  545.  
  546.                  ------------[ Using ANSI.SYS ]------------
  547.  
  548. If you are writing for a "mass market," you cannot assume that  the  user  of
  549. your  BATch  files has ANSI.SYS installed but if you are writing for yourself
  550. or for a colleague, you can be sure it is  installed.   ANSI.SYS  provides  a
  551. simple  way  of controlling colors and, to get really attractive screens, the
  552. location of messages.  You  should  consult  your  DOS  manual  (or  the  DOS
  553. technical  reference  for  DOS  3.x)  to  learn  how to move the cursor.  For
  554. example, if you want to start with echo off and  then  only  erase  the  line
  555. "echo off" without clearing the whole screen, try:
  556.  
  557.                echo off
  558.                echo <esc>[A<esc>[K
  559.  
  560.  
  561.              ------------[ Input the Inelegant Way ]------------
  562.  
  563. One  of  the weaknesses of the DOS BATch processor is its inability to accept
  564. input from the user.  This is of importance not only when writing for others.
  565. You  might  want  a BATch file to run a program and then ask if you wanted to
  566. make a backup.  Of course, if you knew the answer to this backup question  in
  567. advance  you could write the BATch file to get such input as a parameter when
  568. the BATch file was loaded but there is no simple way to get  input  once  the
  569. BATch  file  is running.  The best way to overcome this lack is to use one of
  570. the programs I'll be discussing in later articles in this miniseries.   Lack-
  571. ing  that,  I  know  of two methods to get user input just using DOS's tools.
  572. First, Control-Break (or Control-C) will stop a BAtch file (or rather give yo
  573. the  message  "Terminate  Batch  file(y/n)?") and so you can point out to the
  574. user the option that is always available any ways.  For example,  if  backing
  575. up is all that is left you could put in the lines:
  576.  
  577.                echo To prevent backup hit Control-Break;  otherwise
  578.                pause
  579.  
  580. Since you can accidentally brush a key, this is hardly an ideal solution.
  581.  
  582. Also, you can end a BATch file by displaying a  menu  with  choices  labelled
  583. "a,b,c"  and  have BATch files called a.bat, b.bat, c.bat to run depending on
  584. the user's response.
  585.  
  586.  
  587.                 ------------[ Global Variables ]------------
  588.  
  589. If  you chain or nest BATch files, it is easy to pass parameters between them
  590. by putting the parameters in the command line calling the second BATch  file.
  591. But  what  about BATch files called at different times.  For example, you may
  592. have noticed that the a.bat,...  procedure above seems  to  have  the  disad-
  593. vantage  that  you  can  only  use  it  for one menu unless you use different
  594. directories or different letter choices.  Wouldn't it be better if there were
  595. a  place  to  put  "global  variables"  so  you could set menuchoice=1 before
  596. displaying a menu and have a.bat do different things depending on  the  value
  597. of  "menuchoice".   Well,  DOS has precisely such global variables although a
  598. part of the procedure is undocumented.
  599.  
  600. When COMMAND.COM is initially loaded, it sets aside a  region  of  memory  as
  601. "the environment" which by default is 160 bytes.
  602.  
  603. The  environment  is  intended  primarly to store the location of COMMAND.COM
  604. when its transient portion needs to be  loaded,  your  path  and  prompt  and
  605. information  that  the  user  can provide various programs.  However, you can
  606. also use it to store global variables for communication  between  your  BATch
  607. files.
  608.  
  609. Placing  information  into  the environment is a documented DOS command.  You
  610. use SET.  Thus
  611.  
  612.                SET Name = string
  613.  
  614. will define a global variable "name" to have the value "string".   As  usual,
  615. "name"  doesn't distinguish between lower and upper case;  if you look at the
  616. environment by typing "SET" with no parameters, the variable will appear as
  617.  
  618.                NAME = string
  619.  
  620. Upper and lower case are distinguished in strings.  Notice also  that  spaces
  621. count so that the command
  622.  
  623.                SET name = string
  624.  
  625. will define another variable "NAME" (note the space).
  626.  
  627. Getting  the value of a global variable is easy from within a BATch file.  If
  628. a name appears between percent signs, then DOS will replace the string %name%
  629. by  its environmental value.  Thus if you issued "set foo=fun" and later have
  630. a line like
  631.  
  632.                if %foo%=fun goto success
  633.  
  634. then the BATch file will go to the label success.   If  foo  has  no  current
  635. value, %foo% is replaced by the empty string.  Several warnings are in order.
  636. First, this procedure is undocumented.  However, it has  been  constant  from
  637. DOS  2.0  through  3.2 and is so close to a documented procedure available to
  638. programs that it is as likely to remain  in  DOS  as  anything  that  is  not
  639. documented.   Second,  while  you can use the SET command to define variables
  640. whose names have spaces in them or whose name  begins  with  a  numeral,  you
  641. cannot use %name% to access such variables.
  642.  
  643. A  final  warning.   If you use a command/c to nest a BATch file, DOS makes a
  644. copy of the environment for the new shell that runs the  nested  BATch  file.
  645. Anything  in  the  original environment is in the new one so gloval variables
  646. are available in the new shell.  However, any changes made  in  the  environ-
  647. ment  by  the  nested  BATch  file  via  a  SET command are only saved in the
  648. secondary environment and will be lost when you return to the original  BATch
  649. file.
  650.  
  651. What  are  global  variables good for?  I have placed today's date in English
  652. (that is January 1, 1980 rather than 1-1-80) in my environment a  bootup  and
  653. then I have a letter.bat which makes a copy of a template with my address and
  654. my word processor's formatting command and I append to this copy via
  655.  
  656.                echo (char 255)      %date% >>%1
  657.  
  658.  
  659.                   ------------[ Key Stacking ]------------
  660.  
  661. Wouldn't you like to call up TURBO PASCAL in a BATch file which automatically
  662. answered  Y to the opening question, hits W, supplies the name of a file that
  663. you passed to the BATch file as a parameter and then  hit  E  for  edit?   Or
  664. wouldn't  you  like to have a key struck automatically in response to the IBM
  665. logo and "hit any key to continue" at the start  of  so  many  IBM  programs?
  666. BATch  files need a command that lets you prestack some convenient strokes in
  667. the keyboard buffer.
  668.  
  669. However, there is one crude way of providing keyboard input with DOS tools if
  670. you  can provide all the required input in advance.  For example, if you want
  671. an "erase*.*" command in a BATch file and  would  like  to  avoid  having  to
  672. answer  "are  you sure(Y/N)?", prepare a file, "yes.txt" with the single line
  673. "Y" (and including a CR/LF) and place the line
  674.  
  675.                erase *.* <yes.txt
  676.  
  677. in the BATch file.  Two warnings are in order  here.   Be  sure  you  have  a
  678. complete  set of responses or else your system will hang.  In response to the
  679. "are you sure," you must respond Y followed by <Enter> so  if  you  left  the
  680. CR/LF  out  of  yes.txt,  "erase"  would  patiently  wait for the <Enter> and
  681. wouldn't take it from the keyboard since you told it to only take input  from
  682. yes.txt.   Second,  you cannot redirect input and output to a BATch file as a
  683. whole but you can redirect input or output  of  individual  commands  if  the
  684. commands use standard I/O.  Thus, you cannot make a file, 2yes.txt.  with two
  685. yes  liens  and  a  foo.bat  with  two  lines  saying  "erase  *.*"  and  use
  686. "foo<2yes.txt." Foo will still insist on input from you.
  687.  
  688.  
  689.                      ------------[ Summary ]------------
  690.  
  691. BATch  commands provide the end user with a powerful set of programming tools
  692. to develop custom and innovative solutions for everyday computer needs.
  693.  
  694. In future articles, I will describe some programs that can be used to enhance
  695. the BATch commands and further extend and improve this convenient programming
  696. tool.
  697.  
  698.                               [end of article]
  699.  
  700.