home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / c / clone1_1.zip / SLASH-IF.FOR < prev   
Text File  |  1992-12-21  |  6KB  |  107 lines

  1. SLASH-IF.FOR
  2. Some properties of the use of a slash within a FOR or IF statement.
  3.  
  4. Introduction.
  5.  
  6. Within batch files replaceable batch file parameters (%0..%9) and other batch
  7. variables (%%A..%%Z in FOR commands) are always processed as is, i.e.
  8. literally, except for the wildcard characters ? and * within a FOR command,
  9. ment to be replaced by a list of existing corresponding file names.
  10. Furthermore there are characters that DOS interprets as delimiters and that
  11. can never be processed by themselves or as "part" of a parameter (the parameter
  12. gets split in such a case). These are: space TAB , ; =
  13.  
  14. There is, however, a character that DOS appears to treat in a special way,
  15. which is not documented anywhere (at least I have not seen it anywhere, but
  16. I have experienced the special treatment). That character is the slash. It
  17. will not be treated specially if it only is being compared in a comparative
  18. IF command (IF 'text'=='some other text' ...), whether or not it is a part
  19. of a replaceable parameter or not, but it gets treated specially if it
  20. is part of a text that refers to file names with the IF or FOR command, as in
  21. IF EXIST filename ...
  22. FOR %%F IN (filename_set) DO ...
  23.  
  24. Slash processing.
  25.  
  26. Generally a filename (text that refers to a file name) or set of one or more
  27. filenames within an IF or a FOR, that involves a slash is separated into more
  28. filenames, the first one consisting of all (eventually occurring) characters
  29. up to the slash, the second one consisting of the one literal character (not
  30. being a delimiter) following the slash, while removing the slash (the one
  31. character may be another slash or a * or ?, interpreted as a wildcard character
  32. within an IF, but NOT within a FOR) and the third one consisting of the
  33. (eventually) remaining characters. Thus for example the text 'abc/defg' is
  34. being processed into three separate texts: 'abc', 'd' and 'efg', the texts
  35. 'abc/?efg' or 'abc/*efg' evaluate into 'abc', literal '?' or '*' and 'efg,
  36. while 'abc//efg' is split into 'abc', '/' and 'efg'.
  37.  
  38. In an IF EXIST command only the first evaluated part is processed and the rest
  39. gets ignored, while in a FOR command all parts are contained in the set to be
  40. processed. If specifying 'FOR %%B IN (%1) DO .....' if the first character of
  41. %1 is a '/': it is being removed (within the set-part of the FOR) and the first
  42. (or only) part of the set will be the second character of %1 (and the next part
  43. the eventually remaining characters). If %1 does not have a second character
  44. (it only consists of the slash) then it will be removed, evaluating into
  45. nothing. (If there would be a non-empty following set specification (%1 %2)
  46. then that one would be processed as if being directly preceed by the only
  47. slash, thus disregarding the delimiter between %1 and %2). If %1 has more
  48. embedded slashes these delimit sub-parts and determine a one-character and
  49. remaining-characters next parts. So for example the set specification
  50. '/abc/,def/g' evaluates into 'a', 'bc', 'd', 'ef' and 'g'.
  51.  
  52. This feature (?) can be used to investigate explicitely only the first
  53. character of a replaceable parameter by preceeding it with a slash in a FOR
  54. command. Consider the following DOS batch commands:
  55.   IF "%1"=="" GOTO EMPTY
  56.   FOR %%B IN (/%1) DO IF %%B==/ GOTO SLASH
  57.   FOR %%B IN (/%1) DO IF %%B==~ GOTO TILDE
  58.   REM If %1 itself starts with a slash (followed by zero or more characters)
  59.   REM then the IF test on the first part of %1, being a slash, evaluates
  60.   REM positive and the jump actually is being made before the second remaining
  61.   REM part could be investigated.
  62. Within the FOR the initial (literal) '/' is removed and the rest (%1) is
  63. processed as follows:
  64. - if %1 is only one character long it is the only remaining part of the set
  65.   (and may be checked further, see 1. below).
  66. - if %1 is longer then one character it is split into 2 separate parts of a set:
  67.    1. the first or only character: if it is a '*' or '?' it is _NOT_
  68.       treated as a wildcard specification, but literally left as it is;
  69.    2. the remaining characters (if not including a '/' themselves), which
  70.       may have wildcard characters among them, substituting to file names;
  71.       if these include a '/' the '/' acts as a part delimiter _AND_
  72.       influences the processing of the characters thereafter similarly;
  73. So when calling bath files with arguments one could discriminate between
  74. replaceable parameters starting with a slash (or any specific first character)
  75. and those not starting with a slash (or character specified).
  76.  
  77. Even if 'IF EXIST /%1 ...' is specified not the whole filename (or wildcard)
  78. is used for evaluation, but only the filename consisting of the first
  79. character. If that one is a '*' or '?' it will be treated as a wildcard.
  80. If the slash is within the filename (not the first character) then only the
  81. characters up to the slash as regarded as the filename (eventually including
  82. processed wildcard characters), the rest being ignored. But this seems a
  83. useless application.
  84.  
  85. This also implies that the batch commands of for instance the form
  86.   FOR %%A IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO
  87.                                                  IF "%1"=="%%A:\" GOTO ROOTDIR
  88.   FOR %%A IN (a b c d e f g h i j k l m n o p q r s t u v w x y z) DO
  89.                                                  IF "%1"=="%%A:\" GOTO ROOTDIR
  90. may be replaced by the much shorter
  91.   FOR %%A IN (/%1) DO IF "%%A:\"=="%1" GOTO ROOTDIR
  92. assuming only letters are specified as the first character of %1.
  93.  
  94. This is as far as I know undocumented DOS, but has been tested successfully on
  95. many systems and many DOS versions (3.1, 3.2, 3.3, 5.0). One may check these
  96. properties oneself with the command 'FOR %A IN (test-set) DO ECHO %A'.
  97.  
  98. This is only valid for real batch files, not for compiled ones (BAT2EXEC 1.5);
  99. compiled programs pass the slash (as well as the =char.) as any other character.
  100.  
  101. Centrum voor Medische Informatica TNO       <Email>              |  |  |\/|
  102. TNO Center for Medical Informatics | GROENEVELD@CMI.TNO.NL  |  \_/  |  |  |
  103. ( CMI-TNO )    | Y. Groeneveld     | GROENEVELD@CMIHP1.UUCP | Jim Groeneveld
  104. P.O.Box 124    | Wassenaarseweg 56 | GROENEVELD@TNO.NL      | Schoolweg 14
  105. 2300 AC Leiden | 2333 AL Leiden    | (21 December 1992)     | 8071 BC Nunspeet
  106. Nederland.     | (+31|0)71-181810  | Fax (+31|0)71-176382   | 03412-60413
  107.