home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / filerx11.zip / FILE.CMD < prev    next >
OS/2 REXX Batch file  |  1995-02-06  |  10KB  |  246 lines

  1. /******************************************************************
  2.  * FILE.CMD
  3.  *
  4.  * This program calls the various REXX external functions provided in the FILEREXX DLL.
  5.  *
  6.  * Each function is called once to illustrate how it is used.
  7.  *******************************************************************/
  8.  
  9.  
  10. /* The FileLoadFuncs loads all the rest of the REXX functions in the FILEREXX DLL. */
  11. /* So, we don't have to make calls to RxFuncAdd to add each one of those functions. Of */
  12. /* course, in order to call FileLoadFuncs, we have to add that one. */
  13. CALL RxFuncAdd 'FileLoadFuncs', 'FILEREXX', 'FileLoadFuncs'
  14. CALL FileLoadFuncs
  15.  
  16.  
  17.  
  18. /* ================================ FileWrite ================================= */
  19. /*** Demonstrate FileOpen by opening 'c:\blort' for writing. This should return non-0. Note the
  20.      flags is 'on' to overwrite any existing file with the same name, or create a new file if it doesn't exist.
  21.      The mode is 'rws' for Read/Write with Sharing. Note that we could do only Write with Sharing (ie, 'ws')
  22.       since we aren't going to simultaneously read/write using this handle. That would be the wiser thing
  23.       in case someone else has prevented read sharing. But, this is for demonstration. The attributes is
  24.       0 for normal file, although we don't need to supply it as that's the default.
  25. ***/
  26. handle = FileOpen('c:\blort', 'rws', 'on', 0)
  27. SAY  handle"=FileOpen('c:\blort', 'rws', 'on', 0)"
  28.  
  29. /*** Demonstrate FileWrite by writing 10 characters to 'c:\blort'. This should return 10 in this case ***/
  30. err = FileWrite(handle, '0123456789', 10)
  31. SAY  err"=FileWrite(handle, '0123456789', 10)"
  32.  
  33. /* Incidentally, the count for FileWrite defaults to a 1. So, if you're writing 1 character, you can omit the count arg. */
  34. err = FileWrite(handle, ".")
  35. SAY  err"=FileWrite(handle, '.')"
  36.  
  37. /*** Demonstrate FileClose. This should return 0. ***/
  38. err = FileClose(handle)
  39. SAY err"=FileClose(handle)"
  40.  
  41.  
  42.  
  43.  
  44. /* ================================ FileRead ================================= */
  45. /*** Demonstrate FileOpen by opening 'c:\blort' for reading. This should return non-0, Note omit last arg ***/
  46. handle = FileOpen('c:\blort', 'r', 'e')
  47. SAY  handle"=FileOpen('c:\blort', 'r', 'e')"
  48.  
  49. /*** Demonstrate FileRead by reading 10 characters from 'c:\blort'. This should return '0123456789' ***/
  50. contents = FileRead(handle, 10)
  51. SAY  contents"=FileRead(handle, 10)"
  52.  
  53. /* Incidentally, the count for FileRead defaults to a 1. So, if you're reading 1 character, you can omit the count arg. */
  54. contents = FileRead(handle)
  55. SAY "one more character read ="contents
  56.  
  57. /*** Demonstrate FileClose. This should return 0. ***/
  58. err = FileClose(handle)
  59. SAY err"=FileClose(handle)"
  60.  
  61.  
  62.  
  63.  
  64. /* ============================= FileOpen sharing ============================== */
  65. /** OK. Let's open the same file twice to demonstrate that FileOpen shares access if we allow it. For
  66.       Sharing, we're going to add the + or - signs after the 's' in order to specifically ask for READ sharing
  67.       but no WRITE sharing (ie, 's+-').  If we don't specify these extra chars, it defaults to 's++' **/
  68. handle = FileOpen('c:\blort', 'rs+-', 'e')
  69. SAY  handle"=FileOpen('c:\blort', 'rs+-', 'e')  <- first handle"
  70. handle2 = FileOpen('c:\blort', 'rs+-', 'e')
  71. SAY  handle2"=FileOpen('c:\blort', 'rs+-', 'e')  <- second handle"
  72.  
  73. /** OK. Let's alternately read 1 byte from the file using both handles. You'll note that both
  74.       handles have their own "current" position **/
  75. DO i=1 TO 10
  76.   SAY FileRead(handle)"=read from first handle"
  77.   SAY FileRead(handle2)"=read from second handle"
  78. END
  79.  
  80. /** Close the 2 handles */
  81. err = FileClose(handle)
  82. SAY err"=FileClose(handle)"
  83. err = FileClose(handle2)
  84. SAY err"=FileClose(handle2)"
  85.  
  86.  
  87.  
  88.  
  89. /* ================================ FilePuts ================================= */
  90. /** Use FilePuts to write 3 lines of text. To this example, we'll add error checking of return values.
  91.        Note that we overwrite any existing file, or allow creation of the file if it doesn't exist. Also note
  92.     that we DON'T allow write sharing on this file while its open. If someone else has already done
  93.        likewise to us, then this open will fail.
  94.  **/
  95. handle = FileOpen('c:\blort', 'w', 'on')
  96. IF handle = 0 THEN SAY "Error opening file"
  97.  
  98. line.1 = "This is the first line"
  99. line.2 = "This is the second line"
  100. line.3 = "This is the third     and last    line"
  101.  
  102. DO i=1 TO 3
  103.     err = FilePuts(handle, line.i)
  104.     IF err = 0 THEN SAY "Error writing to file"
  105. END
  106.  
  107. /* Incidentally, if you supply a null line for FilePuts, it defaults to simply closing the current line. So, if you're using
  108.     FileWrite() to write the characters of a line separately, as you might with CHAROUT, and you wish to close the
  109.     line, simply do: */
  110. err = FilePuts(handle);
  111. IF err = 0 THEN SAY "Error upon writing the end of the line"
  112.  
  113. err = FileClose(handle)
  114. IF err <> 0 THEN SAY "Error closing file"
  115.  
  116.  
  117.  
  118.  
  119. /* ================================ FileGets ================================= */
  120. /** Use FileGets to read those lines of text we just wrote above. Note: the default for FileOpen is
  121.       READ with READ sharing (no WRITE share), 'e' flag, normal file. That's what I want here, so the
  122.       FileOpen is really easy
  123. **/
  124. handle = FileOpen('c:\blort')
  125. IF handle = 0 THEN SAY "Error opening file"
  126.  
  127. i=1
  128. DO FOREVER
  129.     myline = FileGets(handle)
  130.     IF FILEERR = 0 THEN LEAVE
  131.     IF FILEERR <> 0 THEN SAY "line "i"="myline
  132.     i=i+1
  133. END
  134.  
  135. /* Now before we close the file, seek to 5 bytes after beginning and read the first line again. We should
  136.     skip the "This ". Note that we could omit the 3rd arg if it was a 1 (ie, seek from current position) */
  137. err = FileSeek(handle, 5, 0)
  138. IF err = "" THEN SAY "Error seeking within file"
  139. IF err <> "" THEN SAY "New position within file ="err
  140. myline = FileGets(handle)
  141. IF FILEERR <> 0 THEN SAY "seek'ed line="myline
  142.  
  143. err = FileClose(handle)
  144. IF err <> 0 THEN SAY "Error closing file"
  145.  
  146.  
  147.  
  148.  
  149. /* ============================= FileGetInfo ================================ */
  150. /** Use FileGetInfo to retrieve info about this REXX script (ie, assumed to be in current dir).
  151. **/
  152. handle = FileOpen('file.cmd')
  153. IF handle \= 0 THEN DO
  154.     err = FileGetInfo(handle, 'Info', 'SIZE|DATE|EASIZE')
  155.     IF err = 0 THEN DO
  156.     SAY "Size of file is" Info.0 "bytes"
  157.     SAY "Size of file's EAs are" Info.3 "bytes"
  158.     SAY "Last Write Date of file is" Info.1 "(month/day/year hour/minute/sec)"
  159.     END
  160.     err = FileClose(handle)
  161. END
  162.  
  163.  
  164.  
  165.  
  166. /* ============================= FileGets with parsing ============================== */
  167. /** Use FileGets to read those same lines of text, but parse each line into separate args using a space as
  168.       the separator character. So, for the first call to FileGets, it should return 5 args, "This", "is", "the",
  169.       "first", and "line", stored in the variables FileArg.1, FileArg.2, FileArg.3, FileArg.4, and FileArg.5. FileArg.0
  170.       should be set to 5. For the next call to FileGets, it should return 5 args also, "This", "is", "the",
  171.       "second", and "line", stored in the variables FileArg.1, FileArg.2, FileArg.3, FileArg.4, and FileArg.5. FileArg.0
  172.       should be set to 5. For the next call to FileGets, it should return 7 args, "This", "is", "the",
  173.       "third", "and", "last, and "line", stored in the variables FileArg.1, FileArg.2, FileArg.3, FileArg.4, FileArg.5,
  174.       FileArg.6, and FileArg.7. FileArg.0 should be set to 7. For the next call to FileGets, FileArg.0 should be
  175.       set to 1, and FileArg.1 is a null string, since we wrote out an empty line in the above FilePuts example.
  176.       For the next call to FileGets, FileArg.0 should be set to 0 since there aren't any more lines in the file.
  177.        Note that the separator arg can be many characters, for example '=,;' would separate args at any of the
  178.       characters '=', ',', or ';'. If you want to include a ' or " in the separator string, enclose the ' within "" and
  179.       the " within ''. For example, here's how to specify both quote characters are separators:  '"'"'". Yeah,
  180.       it's ugly, but that's REXX.
  181.          Also note that for flags, we ask to trim leading and trailing spaces off of each arg. If we didn't
  182.       want any flags enabled, we'd specify a null string (ie, "" or '').
  183. **/
  184. handle = FileOpen('c:\blort')
  185.  
  186. linenum=1
  187. DO FOREVER
  188.     FileGets(handle, 't', ' ')
  189.     IF FILEARG.0 = 0 THEN LEAVE
  190.     DO i = 1 to FileArg.0
  191.     SAY "line #"linenum": Arg #"i"="FileArg.i
  192.     END
  193.     linenum = linenum+1
  194. END
  195.  
  196. err = FileClose(handle)
  197.  
  198.  
  199.  
  200.  
  201. /* ============================== FileWriteValue ============================ */
  202. handle = FileOpen('c:\blort', 'w', 'on')
  203.  
  204. /*** Demonstrate FileWriteValue by writing 400000 LONG, 20000 SHORT, 6 CHAR, and -369000 LONG ***/
  205. err = FileWriteValue(handle, 400000, 4)
  206. SAY  "Writing 400000 ULONG"
  207. err = FileWriteValue(handle, 20000, 2)
  208. SAY  "Writing 20000 USHORT"
  209. err = FileWriteValue(handle, 6, 1)
  210. SAY  "Writing 6 UCHAR"
  211. err = FileWriteValue(handle, -369000, 4)
  212. SAY  "Writing -369000 LONG"
  213.  
  214. err = FileClose(handle)
  215.  
  216.  
  217.  
  218. /* ============================== FileReadValue ============================ */
  219. handle = FileOpen('c:\blort', 'r', 'e')
  220.  
  221. /*** Demonstrate FileReadValue by reading 400000 LONG, 20000 SHORT, 6 CHAR, and -369000 LONG ***/
  222. contents = FileReadValue(handle, 4)
  223. SAY  "Reading "contents" ULONG"
  224. contents = FileReadValue(handle, 2)
  225. SAY  "Reading "contents" USHORT"
  226. contents = FileReadValue(handle, 1)
  227. SAY  "Reading "contents" UCHAR"
  228. contents = FileReadValue(handle, 4, '-')
  229. SAY  "Reading "contents" LONG"
  230.  
  231. err = FileClose(handle)
  232.  
  233.  
  234.  
  235. /* ============================ FileDeleteFile ============================ */
  236. /* Delete that file that we created here */
  237. err = FileDeleteFile('c:\blort')
  238. IF err = 0 THEN SAY 'Temp file is deleted.'
  239.  
  240.  
  241.  
  242. /* =============================================================================== */
  243. /* FileDropFuncs: This unloads all of the functions in the FILEREXX DLL. This is not necessary, and
  244.     we could otherwise leave it open for some other REXX script */
  245. CALL FileDropFuncs
  246.