home *** CD-ROM | disk | FTP | other *** search
/ Big Blue Disk 11 / bbd11.zip / BITS&PCS.TXT < prev    next >
Text File  |  1987-06-11  |  8KB  |  146 lines

  1. |D╔══════════════════╗════════════════════════════════════════════════════════════
  2. |D║ |5The Happy Hacker |D║════════════════════════════════════════════════════════════
  3. |D╚══════════════════╝════════════════════════════════════════════════════════════
  4.  
  5. ^C|0▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
  6. ^C|0█|E~0Bits 'N PC's ^0|0█
  7. ^C|0█|F~0      by     ^0|0█ 
  8. ^C|0█|F~0Daniel Tobias^0|0█
  9. ^C|0▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
  10.  
  11.    This month's topic is batch files.  Through the use of these, you can make 
  12. your PC do many repetitive tasks automatically.
  13.  
  14.    A batch file is basically a list of commands for DOS to execute.  The name 
  15. "batch" derives from the old days of computing, when all commands to computers 
  16. had to be given on stacks of punchcards.  This was known as "batch-processing" 
  17. mode, since the system operators gave the computer its tasks in batches, which 
  18. it churned through and ultimately returned its result on printouts.
  19.  
  20.    In modern times, the interactive mode has become more popular.  In this 
  21. mode, you give commands to the computer via keyboards, mice, light pens, 
  22. joysticks, etc., and they are executed right away.  This is much more convenient
  23. for the user in most cases.  However, there are still times when you have a 
  24. predefined task that must be done frequently, and you'd like to just tell the 
  25. computer to start it up, without the user needing to intervene and enter every 
  26. single required command in the proper sequence.
  27.  
  28.    That is why the designers of MS-DOS provided the capability of "Batch 
  29. Files."  Like the old stacks of punchcards, a batch file contains a sequence of 
  30. commands to be executed.  However, MS-DOS also provides such advanced features 
  31. as branching and conditional expressions.  In fact, MS-DOS batch files can be 
  32. considered to be a programming language in their own right, and you can do many 
  33. powerful things with them.
  34.  
  35.    A batch file is an ordinary ASCII text file, given a filename with the 
  36. extension ".BAT".  You can use any text editor (such as BlueLine) to create 
  37. batch files.  Each line of a batch file, terminated by a carriage return, 
  38. contains one DOS command.  Any legal DOS command, or invocation of any program,
  39. can be used in a batch file.  In addition, several special commands have been 
  40. created specifically for batch file use.  Here are some of them:
  41.  
  42.    ^1REM^0 indicates a comment line that is not executed.  Such lines are ignored.
  43.  
  44.    ^1PAUSE^0 makes the PC stop and wait for the user to press a key.  The prompt
  45. "Strike a key when ready..." is displayed.
  46.  
  47.    ^1CLS^0 clears the screen.
  48.  
  49.    ^1ECHO^0 has several functions.  ^1ECHO OFF^0 causes DOS to stop showing the commands
  50. as it executes them, allowing the batch file to proceed invisibly.  (This just 
  51. stops output of the command lines executed; any output produced by the commands 
  52. and programs invoked by the batch file is still shown.)  ^1ECHO ON^0 "undoes" ^1ECHO
  53. ^1OFF^0, restoring output of each line executed.  ^1ECHO^0 followed by any other
  54. text causes that text to be output.
  55.  
  56.    ^1GOTO^0 causes a branch to a different part of a batch file.  Define labels to
  57. show DOS where to go by entering a line whose first character is a colon (:), 
  58. followed by a label name (such as "line1" or "loop").  ^1GOTO LINE1^0 would cause
  59. execution of the batch file to continue at LINE1 instead of sequentially.  Use 
  60. GOTO along with the IF command (described below) for conditional branching.
  61.  
  62.    ^1IF^0 is the most powerful batch command.  Its syntax is as follows:
  63. ^1IF <condition> <command>^0.  <Command> is any command allowed in a batch file.
  64. It is executed only if <condition> is true.  <Condition> is one of the following
  65. three things: 
  66.  
  67. ^1EXIST <filename>^0: This is true if a file exists by the name of <filename>. 
  68.  
  69. ^1<string1> == <string2>^0: This is true if the two strings are identical.  This
  70. becomes useful when you use parameters, described below.
  71.  
  72. ^1errorlevel <number>^0: This checks the errorlevel returned by a command or 
  73. program that was just executed, and is true if this level (a number from 0 to 
  74. 255) is greater than or equal to the given <number>.  Some programming 
  75. languages allow you to have a program terminate with a particular errorlevel.  
  76. For example, in Turbo Pascal, you can end a program with ^1halt(32)^0 to cause an
  77. errorlevel of 32 to be sent to DOS.  This can be used to control execution of a 
  78. batch file.
  79.  
  80. In addition, there is the form ^1IF NOT <condition> <command>^0 which executes 
  81. <command> only if <condition> is NOT true.
  82.  
  83.    ^1FOR^0 allows commands to be repeated.  It is another powerful, complex 
  84. command.  Its syntax is ^1FOR <dummy variable> IN <set> DO <command>^0.  The dummy
  85. variable consists of two percent signs followed by a letter, like "%%x".  <Set> 
  86. is a list of parameters (usually filenames) surrounded by parentheses and 
  87. separated by spaces, like ^1(go.bat present.chn pasrun.com return.chn)^0.  Finally,
  88. <command> is any DOS command.  If you include the dummy variable name anywhere 
  89. in the <command>, it will be replaced in turn with each element of the <set>.  
  90. Thus, ^1FOR %%A IN (THIS.TXT THAT.TXT ANOTHER.TXT) DO COPY %%A B:^0 will copy the
  91. files THIS.TXT, THAT.TXT, and ANOTHER.TXT to drive B.
  92.  
  93.    A powerful feature of batch files is the ability to use parameters.  When 
  94. you invoke a batch file by typing its name (with or without the .BAT extension),
  95. you can follow it with a list of parameters separated by spaces.  They can be 
  96. inserted into any command within the batch file by using a percent sign (%) 
  97. followed by the number of the parameter:  %1 for the first parameter, %2 for 
  98. the second, and so on.  Hence, if you invoke JUNK.BAT with the command:
  99. ^1JUNK 1 2 3 FOUR^0, then the sequence %1 anywhere in the file will be replaced 
  100. by the string "1", and similarly %2 is replaced with "2", %3 with "3", and %4 
  101. with "FOUR".  %0 represents the name of the batch file itself.  Since the % 
  102. sign is a special character in batch files, if you actually want "%" to be used 
  103. in a command, it must be typed twice, as "%%".  This is why %% is used in dummy 
  104. variable names in the FOR command; if you type a FOR command in direct mode 
  105. rather than a batch file, you would just use one percent sign. 
  106.  
  107.    The ^1SHIFT^0 command causes parameters to be "shifted" over one.  After SHIFT
  108. is executed, the old parameter %1 becomes %0, while %2 becomes %1, and so on.  
  109. This allows more than nine parameters to be used; the tenth parameter will be 
  110. shifted to %9.  Doing repeated SHIFTs will bring more parameters to accessible 
  111. numbers.  The original value of %0 is lost after SHIFT is executed.
  112.  
  113.    Here's an example of a batch file using some of these features:
  114.  
  115. ^1   ECHO OFF
  116. ^1   IF EXIST %1 GOTO OK
  117. ^1   ECHO That file is not found.
  118. ^1   GOTO END
  119. ^1   :OK
  120. ^1   IF %2 == D DEL %1
  121. ^1   IF %2 == C COPY %1 B:
  122. ^1   FOR %%A IN (%3.TXT %3.DOC %3.HLP) DO DEL %%A
  123. ^1   ECHO Done!
  124. ^1   :END
  125. ^1   REM End of batch file.
  126.  
  127.   This batch file takes three parameters.  The first is the name of a file.  If 
  128. no such file exists, the batch file terminates, giving a message that the file 
  129. was not found.  Otherwise, the second parameter is examined; if it is the 
  130. letter D, then the file is deleted.  If it is the letter C, then it is copied 
  131. to drive B.  After this, the FOR command is used to delete all files with names 
  132. beginning with the third parameter, and ending with the .TXT, .DOC, and .HLP 
  133. extensions.  Then a message "Done!" is output, and the file ends.
  134.  
  135.    So, if the above file was named STUFF.BAT, then typing:
  136.  
  137. ^C^1STUFF JUNK.TXT D STUFF^0
  138.  
  139. will cause JUNK.TXT, STUFF.TXT, STUFF.DOC, and STUFF.HLP to be deleted (assuming
  140. all these files exist to begin with.)
  141.  
  142.    Examine DO.BAT, a batch file on this disk, for a good example of things you 
  143. can do with a batch file.  You might also check our GO.BAT (used to start up 
  144. BIG BLUE DISK), and any other batch files you may encounter, for more examples 
  145. of batch file usage. 
  146.