home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / DOFILES.CMD < prev    next >
OS/2 REXX Batch file  |  1995-03-28  |  4KB  |  90 lines

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