home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / os / msdos / programm / 9118 < prev    next >
Encoding:
Internet Message Format  |  1992-09-08  |  5.2 KB

  1. Path: sparky!uunet!wupost!waikato.ac.nz!canterbury.ac.nz!cctr132
  2. Newsgroups: comp.os.msdos.programmer
  3. Subject: Re: Another MsDos batch programming challenge
  4. Message-ID: <1992Sep7.211129.702@csc.canterbury.ac.nz>
  5. From: cctr132@csc.canterbury.ac.nz (Nick FitzGerald, CSC, Uni. of Canterbury, NZ)
  6. Date: 7 Sep 92 21:11:28 +1200
  7. References: <israel.715477510@unixg.ubc.ca> <1992Sep3.214401.5000@uwasa.fi> 
  8.  <1992Sep4.032836.7299@cs.ubc.ca> <1992Sep4.213842.14734@mits.mdata.fi>
  9. Organization: University of Canterbury, Christchurch, New Zealand
  10. Lines: 126
  11.  
  12. In article <1992Sep4.213842.14734@mits.mdata.fi>, kennu@mits.mdata.fi
  13. (Kenneth Falck) writes:
  14. > In article <1992Sep4.032836.7299@cs.ubc.ca> yogi@cs.ubc.ca (Yossi Gil)
  15. writes:
  16. >>Yes, I tested it. It works. It relies on the way DOS does batch file:
  17. >>    
  18. >>    1. Open file.
  19. >>    2. Seek in file to current statement. 
  20. >>    3. Interpret statement. 
  21. >>    4. Close file.
  22. >>    5. Execute statement
  23. >>    6. Goto 1 if not done.
  24. >>
  25. >>This is why Batch Programming is so inefficient.
  26.  
  27. I missed Yossi's original post on this, so apologies if he covered some
  28. of the following.
  29.  
  30. The actual DOS batch processor is a tad more involved than Yossi's
  31. outline suggests.  Whilst the following may not seem much different from
  32. what Yossi suggests, some subtle implications of batch processor
  33. operations are more clearly apparent from it.  To the best of my ability
  34. to tell, the following is a more accurate outline:
  35.  
  36.  1.  Open file
  37.  2.  Read first line
  38.  3.  Note number of -chars- read thus far
  39.  4.  Parse line (for redirection, etc)
  40.  5.  Close file
  41.  6.  Execute command
  42.  7.  Open file
  43.  8.  Read to number of chars recorded in 4.
  44.  9.  Read to next EOL or EOF
  45. 10.  If command is null and EOF goto 12.
  46. 11.  Loop to 3.
  47. 12.  Close file
  48.  
  49. (NB:  Steps 3, 4 and 5 are arbitrarily ordered here -- I can't think of
  50. any way to test this.  I could roll them into one step I suppose.  8-) )
  51.  
  52. This implies that one line and null batches will be opened twice --
  53. anyone with an interrupt tracer care to test this out?  If null batches
  54. aren't opened twice, I'll have to revise Steps 2 and 3.
  55.  
  56. Some other implications of this are easily tested.  In LAN environments
  57. batch files whose last act is to remove the drive mapping for the drive
  58. they are running from will generate an error message from DOS about an
  59. invalid command or bad filename (depending on DOS version) as the batch
  60. processor fails to find the file, to reopen it (if the EOF testing was
  61. part of the "read command" logic, this wouldn't happen).
  62.  
  63. > By the way, does MSDOS store the current statement of
  64. > a batch file as an absolute byte location from the
  65. > beginning of the file, or perhaps as the line number
  66. > (lines separated by CRLF's)?
  67.  
  68. According to the above, byte offset, but don't just take my word for it,
  69. try the following:
  70.  
  71. Copy the following batch files to a suitable directory.  Make a backup
  72. copy of BATTEST.BAT (say BATTEST1.BAT ).
  73.  
  74.  
  75. -----------------------  BATTEST.BAT  -----------------------
  76. echo off
  77. echo Batch test 1
  78. copy battest2.bat battest.bat
  79. echo Batch test 1
  80.  
  81. -----------------------  BATTEST2.BAT  -----------------------
  82. echo off
  83. echo Batch test 2
  84. copy battest2.bat battest.bat  dir
  85. echo Batch test 2
  86.  
  87. NOTE:  The "dir" on line 3 of BATTEST2.BAT is preceded by -TWO- spaces. 
  88. It is most important that both are included (but more won't hurt  8-) ).
  89.  
  90. Change to the directory where these batches are, and execute BATTEST. 
  91. Your output should look something like:
  92.  
  93. *************************************************
  94. Batch test 1
  95.         1 file(s) copied
  96.  
  97.  Volume in drive C has no label
  98.  Volume Serial Number is 0E5C-07CE
  99.  Directory of C:\BATTEST
  100.  
  101. .            <DIR>     23-08-92  12:06p
  102. ..           <DIR>     23-08-92  12:06p
  103. BATTEST  BAT        85 07-09-92   1:27p
  104. BATTEST1 BAT        80 07-09-92   1:27p
  105. BATTEST2 BAT        85 07-09-92   1:27p
  106.        12 file(s)       1804 bytes
  107.                     13506560 bytes free
  108. Batch test 2
  109. *************************************************
  110.  
  111. Note that the "dir" command on line 3 of BATTEST2 executed -- if the
  112. batch processor were simply keeping a line count this wouldn't occur. 
  113. Now it should be obvious why there need to be two spaces before the
  114. "dir" on line 3 -- remember, DOS uses CR/LF for end-of-line!
  115.  
  116. Take out one of the two spaces before the "dir" in BATTEST2.BAT and what
  117. happens? -- You get a "bad command or filename" error, as DOS tries to
  118. execute the command "ir" (unless you have an "IR.BAT", .EXE or .COM in
  119. your path or current directory).
  120.  
  121. Make sure SHARE is loaded and run the original test again.  You will
  122. -NOT- get a sharing violation on BATTEST.BAT while the "copy" command
  123. on line 3 executes, supporting my contention that the batch file is
  124. closed between reads.
  125.  
  126. I have only tested this systematically under MS-DOS 5.0, but from much
  127. batch experience under MS-DOS 3.3 and 4.0, feel it (mostly ?) holds for
  128. those versions as well.  Would someone with DR-DOS 5.0 and/or 6.0 like
  129. to test this out and send me the results?  Also, anyone with an "old"
  130. version of MS-DOS (say, pre-3.2) ?
  131.  
  132. I hope you found this weeks installment of the DOS batch file saga
  133. interesting!
  134.  
  135. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  136.  Nick FitzGerald, PC Applications Consultant, CSC, Uni of Canterbury, N.Z.
  137.  n.fitzgerald@csc.canterbury.ac.nz  (64)(3)  364-2337 Voice,  364-2332 FAX
  138.