home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 035 / cenvi29.zip / ALLFILES.CMD < prev    next >
OS/2 REXX Batch file  |  1994-03-08  |  4KB  |  86 lines

  1. EXTPROC CEnvi
  2. /************************************************************************
  3.  *** AllFiles.cmd - Perform the given command on all files that match ***
  4.  *** ver.1          the input command spec, where the special command ***
  5.  ***                fields $DIR, $ROOT, $EXT, and $FILE are replaced  ***
  6.  ***                by the directory, root, extension, and full       ***
  7.  ***                filename of the matching files found.             ***
  8.  ************************************************************************/
  9.  
  10. main(argc,argv)
  11. {
  12.    if ( argc < 3  ||  !strcmp(argv[1],"/?")  ||  !stricmp(argv[1],"/help") ) {
  13.       Instructions();
  14.    } else {
  15.       FileSpec = argv[1];
  16.       // build output command, which is all the other input args
  17.       Command = argv[2];
  18.       for ( i = 3; i < argc; i++ ) {
  19.          strcat(strcat(Command," "),argv[i]);
  20.       }
  21.       // build list of matching files
  22.       FileList = Directory(FileSpec,FALSE,FATTR_RDONLY|FATTR_HIDDEN|FATTR_SYSTEM|FATTR_ARCHIVE)
  23.       if ( NULL != FileList ) {
  24.          for( i = 0; i <= GetArraySpan(FileList); i++ ) {
  25.             PerformCommandOnFile(Command,FileList[i].name);
  26.          }
  27.       }
  28.    }
  29. }
  30.  
  31. SubstituteNamesInCommand(Command,Find,Replace)
  32. {
  33.    FindLen = strlen(Find);
  34.    ReplaceLen = strlen(Replace);
  35.    for ( c = Command; NULL != (c = strchr(c,'$')); c++ ) {
  36.       if ( !strnicmp(c+1,Find,FindLen) ) {
  37.          // replace the Find string with the Replace string
  38.          strcpy(c+ReplaceLen,c+1+FindLen);
  39.          memcpy(c,Replace,ReplaceLen);
  40.          c += ReplaceLen - 1;
  41.       }
  42.    }
  43. }
  44.  
  45. PerformCommandOnFile(VirginCommand,File)
  46. {
  47.    NameParts = SplitFileName(File);
  48.    // substitute all the $DIR, $ROOT, $EXT, and $FILE commands for that part in NameParts
  49.    strcpy(NewCommand,VirginCommand);
  50.    SubstituteNamesInCommand(NewCommand,"DIR",NameParts.dir);
  51.    SubstituteNamesInCommand(NewCommand,"ROOT",NameParts.name);
  52.    SubstituteNamesInCommand(NewCommand,"EXT",NameParts.ext);
  53.    SubstituteNamesInCommand(NewCommand,"FILE",File);
  54.    // set the environment variables to the new values
  55.    $DIR = NameParts.dir
  56.    $ROOT = NameParts.name
  57.    $EXT = NameParts.ext
  58.    $FILE = File
  59.    // print the command, and send it to the system
  60.    printf("%s\n",NewCommand);
  61.    system(NewCommand);
  62. }
  63.  
  64. Instructions()
  65. {
  66.    printf("\n")
  67.    printf("AllFiles.cmd - Execute a command on all files meeting the input criteria.  The\n")
  68.    printf("               command may include these strings:\n")
  69.    printf("                $DIR    - directory portion of matching file\n")
  70.    printf("                $ROOT   - root name of the matching file\n")
  71.    printf("                $EXT    - extension name of the matching file\n")
  72.    printf("                $FILE   - full name of the matching file\n")
  73.    printf("               These names, $DIR, $ROOT, $EXT, and $FILE will also be set as\n")
  74.    printf("               environment variables when the command is executed.\n")
  75.    printf("\n")
  76.    printf("SYNTAX: AllFiles FileSpec [Command]\n")
  77.    printf("Where:  FileSpec  - any file specification ith wildcards included\n")
  78.    printf("        Command   - any command line specification\n")
  79.    printf("\n")
  80.    printf("EXAMPLE: To copy all *.TXT files to *.bak files in the C:\\Temp directory. but\n")
  81.    printf("         only if the C:\\Temp\\*.bak file does not already exist:\n")
  82.    printf("     ALLFILES *.TXT IF NOT EXIST C:\\Temp\\$ROOT.bak copy $FILE C:\\Temp\\$ROOT.bak\n")
  83.    printf("\n")
  84. }
  85.  
  86.