home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / filerx11.zip / wildcard.cmd < prev   
OS/2 REXX Batch file  |  1995-02-11  |  5KB  |  120 lines

  1. /******************************************************************
  2.  * WILDCARD.CMD
  3.  *
  4.  * This program calls the various REXX external functions provided in the FILEREXX DLL.
  5.  *******************************************************************/
  6.  
  7.  
  8. /* The FileLoadFuncs loads all the rest of the REXX functions in the FILEREXX DLL. */
  9. /* So, we don't have to make calls to RxFuncAdd to add each one of those functions. Of */
  10. /* course, in order to call FileLoadFuncs, we have to add that one. */
  11. CALL RxFuncAdd 'FileLoadFuncs', 'FILEREXX', 'FileLoadFuncs'
  12. CALL FileLoadFuncs
  13.  
  14. /* Let's assume that this script reads in some existing file, manipulates it, and writes
  15. out the new changes to a file.    The script wants the user to type the name of the file to
  16. read in, as an arg on the command line when this script is run.  This must be an existing
  17. file.  Furthermore, the user can optionally supply the name of the file to write out the new
  18. changes to.  If he doesn't supply this, then it defaults to the same name as the existing
  19. file (ie, overwrites it).  The script wants to allow the user to use wildcards for the source
  20. and destination filenames.  For example, if he supplied the following args&colon.
  21.  
  22. *.c  c:\temp\*.bak
  23.  
  24. then this should read in each file (in the current directory) that ends with a :hp9..c:ehp9.
  25. extension, perform an operation upon it, and write it out in the C&colon.\temp directory,
  26. changing the file's extension to :hp9..bak:ehp9..  Here's how to implement the script.
  27. */
  28.  
  29. /* Get the fromname and toname that the user typed as args on the command line. */
  30. arg fromname toname
  31.  
  32. /* Separate fromname into separate pieces. Set the flag to check whether it's a file or
  33. directory. This script only wants to perform work upon files.
  34. */
  35. err = FileGetPath('Source', fromname, 'CHK')
  36. IF err = 0 THEN DO
  37.  
  38.     /* Now remake the fromname from its pieces. This will ensure that, if the user specified
  39.     a dir for the source, our fromname will end with a '\' character, which ensures that
  40.     FileMatchFile enumerates all of the files within that directory */
  41.     fromname = Source.0||Source.1||Source.2||Source.3
  42.  
  43.     /* If toname was not supplied, default to the same as fromname.
  44.     */
  45.     IF toname = '' THEN DO
  46.     toname = fromname
  47.     END
  48.  
  49.     ELSE DO
  50.     /* Let's also chop up the toname into pieces, checking if it's a directory, and remake
  51.     the toname from its pieces. This will ensure that, if the user specified a dir for the
  52.     toname, our toname will end with a '\' character, which ensures that FileEditName
  53.     enumerates all of the matching files to that directory */
  54.     err = FileGetPath('File', toname, 'CHK')
  55.     IF err = 0 THEN toname = File.0||File.1||File.2||File.3
  56.  
  57.     /* Error */
  58.     ELSE DO
  59.         SAY 'ERROR checking "' toname '":' err
  60.         SIGNAL OutOfHere
  61.     END
  62.     END
  63.  
  64.     /* Since the fromname can contain wildcards, we'll pass it to FileMatchFile so that we
  65.     can enumerate each and every match. If fromname doesn't contain any wildcards
  66.     (ie, it's simply the name of a specific file), this will only loop once anyway on that
  67.     one fromname.  Note that we limit the search to read/write files, readonly files,
  68.     and files with the archive bit set.  We ignore subdirectories, system, and hidden
  69.     files since this script doesn't care about those.
  70.     */
  71.  
  72.     /* Initially set handle to 0 */
  73.     Handle = 0
  74.  
  75.     DO UNTIL Handle = 0
  76.  
  77.     /* Locate the next match to fromname */
  78.     err = FileMatchFile('File', 'Handle', fromname, 'RDO|ARC')
  79.  
  80.     /* No error? (ie, another match) */
  81.     IF err = 0 THEN DO
  82.  
  83.         /* Since the toname can contain wildcards, we need to apply it as a template to the matched
  84.         source (ie, minus the drive and directory of source, and using the drive and dir of the toname).
  85.         */
  86.         NewFile = FileEditName(File, toname, 'SKIPDIR')
  87.         IF NewFile <> '' THEN DO
  88.  
  89.         /* Add the original source drive and directory to the matched source */
  90.         File = Source.0||Source.1||File
  91.  
  92.         /* Here, we would read in the file (name is in File), operate upon it, and write it back out using the
  93.             filename in NewFile.  Just for testing purposes, we'll just write out these 2 filenames.
  94.         */
  95.         SAY '============================================='
  96.         SAY 'Source =' File
  97.         SAY 'New file =' NewFile
  98.         END
  99.  
  100.         /* An error occurred during creating the NewFile name */
  101.         ELSE SAY 'ERROR making new filename.'
  102.  
  103.     END
  104.  
  105.     /* An error occurred during matching the source */
  106.     ELSE IF err <> 18 THEN SAY 'ERROR locating "' fromname '":' err
  107.  
  108.     END
  109. END
  110.  
  111. /* Error */
  112. ELSE SAY 'ERROR checking "' fromname '":' err
  113.  
  114. OutOfHere:
  115.  
  116. /* =============================================================================== */
  117. /* FileDropFuncs: This unloads all of the functions in the FILEREXX DLL. This is not necessary, and
  118.     we could otherwise leave it open for some other REXX script */
  119. CALL FileDropFuncs
  120.