home *** CD-ROM | disk | FTP | other *** search
/ Cheet Sheets 1995 April/May / CHEET40.ZIP / 1ATCHTIP.TXT < prev    next >
Text File  |  1995-05-07  |  9KB  |  235 lines

  1. ~Basic Batch File Tips
  2.  
  3.  
  4. Using '%' Arguments in Batch Files  -  By Simon Burrows
  5.  
  6.  
  7.  
  8. `If you've  ever had  the job  of writing  any but the simplest of
  9. `batch files  before, you will have used arguments.  Incorporating
  10. `these into  such a  batch file  allows the  user of  the file  to
  11. `specify one or more commands as well as the name of the .BAT file
  12. `at the  Command Line.   If  you have  never  heard  of  arguments
  13. `before, or  do not know how to use them, read on.  This documents
  14. `contains a brief(ish) description of them and their uses :-
  15.  
  16.  
  17.  
  18. ~`─────────────────────────────────────────────────────────────────
  19.  
  20.  
  21.  
  22. `When using many of the utilities DOS comes with  (eg, FORMAT.COM)
  23. `you must  specify more  than just  the name  of the  file at  the
  24. `command line.   For  example, when using FORMAT.COM, you may type
  25. `this to format a disk in drive A: to 3.5" HD :-
  26.  
  27.  
  28. ~FORMAT A: /F:1.44
  29.  
  30.  
  31. `This is exactly the same for many batch files.
  32.  
  33. `For example,  many older  games available  must be installed to a
  34. `hard disk with the use of a Batch File installation program.  You
  35. `may need  to type  something like  this at  the command  line  to
  36. `install a  game on  a disk  in drive  A: to  a  directory  called
  37. `SOLITARE on drive C: :-
  38.  
  39.  
  40. ~INSTALL A: C:\SOLITARE
  41.  
  42.  
  43. `When we  do this, each part of the command is called an argument.
  44. `Whenever there is a space  (eg, in between A: and C:\SOLITARE)  a
  45. `new  argument  begins.    These  arguments  can  also  be  called
  46. `variables.
  47.  
  48. `Each argument,  or variable,  is remembered by your computer as a
  49. `'%' sign  and a  number.   So, the  separate parts of the example
  50. `above are named as follows :-
  51.  
  52.  
  53. ~`INSTALL A: C:\SOLITARE
  54. `   │     │       │
  55. `   │     │       │
  56. ~`  %0    %1      %2
  57.  
  58.  
  59. `The part  called '%1'  is the  first argument  even though  it is
  60. `really the  second, after  '%0'.  Most of the time you will never
  61. `use '%0',  although, as  you will  see later in this text, it has
  62. `some uses.
  63.  
  64. `If you are just using batch files,  you needn't worry about these
  65. `'%' sign  'abbreviations' but  they are  an important part of the
  66. `writing of a batch file.
  67.  
  68. `As an  example, imagine  you had a batch file which copies a file
  69. `from one  disk to another.  At the command line you specify where
  70. `the file  is located at the moment and then its destination.  For
  71. `example, you might type the following at the command line :-
  72.  
  73.  
  74. ~COPYIT A: C:
  75.  
  76.  
  77. `The batch  file, COPYIT.BAT, would use DOS's Copy command to copy
  78. `the program.   However,  if you listed the file, it wouldn't just
  79. `read something  like   "COPY FILE.EXE  A: C:"   because this will
  80. `only copy  the file from one set disk drive  (A:)  to another set
  81. `disk drive  (C:).  Instead, it would read something like this :-
  82.  
  83.  
  84. ~COPY FILE.EXE %1 %2
  85.  
  86.  
  87. `If you  loaded this  batch file now, stating the two arguments at
  88. `the command  line to  specify where  the file for copying is now,
  89. `and where  you want  it to go,  (eg, COPYIT A: C:), your computer
  90. `will replace  the '%1' in the batch file with the first argument,
  91. `and the '%2' with the second argument.  This means that you could
  92. `use this  same batch  file to copy the file "FILE.EXE" regardless
  93. `which drive it is on and which drive you want it to be copied to.
  94. `For example, if you typed :-
  95.  
  96.  
  97. ~COPYIT F: Y:
  98.  
  99.  
  100. `...your  computer   would  automatically   replace  the   %1   in
  101. `COPYIT.BAT's listing  with 'F:'  and the %2 with 'Y:' so it would
  102. `copy the file called 'FILE.EXE' from a disk in drive F: to a disk
  103. `in drive  Y:   (Just as long as you had these drives available on
  104. `your PC, of course!)
  105.  
  106. `You could  incorporate the use of variables and the IF command to
  107. `display a  help message  to the  user of  the batch  file.    For
  108. `example, in  our example  of "COPYIT.BAT",  you could  make it so
  109. `that if  your user  specified the  first variable  as '/?'    (by
  110. `typing "COPYIT /?")  a message would be displayed describing what
  111. `the file "COPYIT.BAT" does.
  112.  
  113. `To do this you might use the following batch file listing :-
  114.  
  115.  
  116. ~`IF "%1"=="/?" GOTO HELP
  117. ~`COPY FILE.EXE %1 %2
  118. ~`GOTO END
  119.  
  120. ~`:HELP
  121. ~`ECHO  The file "COPYIT.BAT" will copy the file called "FILE.BAT"
  122. ~`ECHO  from any drive to a destination of your choice.
  123. ~`ECHO.
  124. ~`ECHO  Usage is as follows :-ECHO.
  125. ~`ECHO  COPYIT [Location of "FILE.EXE] [Destination of "FILE.EXE]
  126.  
  127. ~`:END
  128.  
  129.  
  130. `The top  line of  this is  saying that  if the user specifies the
  131. `first argument  as '/?'   (ie,  by typing "COPYIT /?")  then your
  132. `computer should  go straight  to the  section of  the batch  file
  133. `called "HELP".  This is the part directly after the ":HELP" line,
  134. `and has a series of 'ECHO' lines to display a text message.
  135.  
  136. `However, if  the user does not specify the first argument as '/?'
  137. `and instead types something like "COPYIT A: C:", then %1 will not
  138. `be '/?'  so this first line will be ignored and it will go to the
  139. `next line.   The  next line, as described above, copies the file,
  140. `then, the  line after  that tells  your computer  to skip  to the
  141. `section called  "END" at the end of the file so it misses out the
  142. `unwanted help message.
  143.  
  144. `So, if  your user  types   "COPYIT X: Y:"  the file "FILE.EXE" is
  145. `copied from  disk X: to disk Y:, and if they type "COPYIT /?" the
  146. `following text-message is displayed :-
  147.  
  148.  
  149. ~`The file "COPYIT.BAT" will copy the file called "FILE.BAT"
  150. ~`from any drive to a destination of your choice.
  151.  
  152. ~`Usage is as follows :-
  153.  
  154. ~`COPYIT [Location of "FILE.EXE"] [Destination of "FILE.EXE"]
  155.  
  156.  
  157. `But what  happens if  your user doesn't specify any argument, and
  158. `instead loads the batch file by typing simply "COPYIT"?  Well, if
  159. `this is  the case then your computer will not know where the file
  160. `is and  where to copy it to.  In this eventuality you will need a
  161. `message displayed  which tells the user that they have loaded the
  162. `file wrongly.   This message might as well be the same as the one
  163. `displayed if  they type  "COPYIT /?" as this message  (if your is
  164. `the same  as mine,  anyway)   includes information on how to load
  165. `the file properly.
  166.  
  167. `However, there's  a problem.   The  ONLY way  this message can be
  168. `displayed is  if the  file COPYIT.BAT  is loaded  with  the  '/?'
  169. `argument.  Because of this, the only way to get the message to be
  170. `shown if no argument is specified is to make your computer reload
  171. `the batch file with the '/?' argument automatically.
  172.  
  173. `To do  this we  need use  of the  '%0' argument  mentioned at the
  174. `start of this document.  Since '%1' stands for the first argument
  175. `after the  filename, '%0'  stands for  the filename itself.  This
  176. `means that  you could  put %0  into your  batch file and the file
  177. `will reload when this figure is reached.
  178.  
  179. `So, what  we can  do is  say that  if there is no '%1' specified,
  180. `then the batch file should be reloaded by the computer, this time
  181. `with the  argument '/?', so that the message is displayed showing
  182. `the user how to use the batch file properly.
  183.  
  184. By adding  this feature to our batch file, it now reads :-
  185.  
  186.  
  187. ~`IF "%1"=="" %0 /?
  188. ~`IF "%1"=="/?" GOTO HELP
  189. ~`COPY FILE.EXE %1 %2
  190. ~`GOTO END
  191.  
  192. ~`:HELP
  193. ~`ECHO  The file "COPYIT.BAT" will copy the file called "FILE.BAT"
  194. ~`ECHO  from any drive to a destination of your choice.
  195. ~`ECHO.
  196. ~`ECHO  Usage is as follows :-
  197. ~`ECHO.
  198. ~`ECHO  COPYIT [Location of "FILE.EXE"] [Destination of "FILE.EXE"]
  199.  
  200. ~`:END
  201.  
  202.  
  203. `The first  line checks  to see  whether there  are  no  arguments
  204. `specified.   It does  this by seeing whether  '%1' equals nothing
  205. `("" means nothing)  rather than whether it exists at all. If '%1'
  206. `does equal  nothing, it  carries out  the rest  of the line  (the
  207. `"%0 /?" part)  which tells your computer to reload the batch file
  208. `with '/?' as the one and only argument.  Otherwise, it skips this
  209. `part and continues to line two.
  210.  
  211.  
  212. So with this batch file, there could be 3 outcomes :-
  213.  
  214. `1. The file is loaded with the first argument as the curent drive
  215. `   of the  file  for  copying,  and  the  second  as  the  file's
  216. `   destination.  In this case, the file will be copied correctly.
  217. `2. The  file is  loaded with  the first  and only  argument being
  218. `   '/?'.   In this  case, the  text message  is displayed quickly
  219. `   explaining the file and how to use it properly.
  220. `3. The  file is loaded with no arguments.  In this case, the same
  221. `   text message  is displayed quickly explaining the file and how
  222. `   to use it properly.
  223.  
  224.  
  225.  
  226. ~`Well, there you go then folks, have fun!..
  227.  
  228.  
  229.  
  230. `                         ───────────────
  231. ~`                          Simon Burrows
  232. `                         ───────────────
  233.  
  234.  
  235.