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

  1. @echo OFF
  2. REM *****************************************************************
  3. REM *** CDfind - Use CEnvi to search this disk, or all disks, for ***
  4. REM *** ver.3    directory matching any given file spec.  Then    ***
  5. REM ***          change to that directory                         ***
  6. REM *****************************************************************
  7.  
  8. SET CDF_DRV=
  9. SET CDF_DIR=
  10. CALL CEnviSet %0.cmd %1 %2 %3 %4 %5 %6 %7 %8 %9
  11. GOTO CENVI_EXIT
  12.  
  13. main(argc,argv)
  14. {
  15.    if ( 2 != argc  ||  !strcmp("/?",argv[1])  ||  !stricmp("/help",argv[1])  ||  0 == argv[1][0] ) {
  16.       Instructions();
  17.    } else {
  18.       DirCount = FindDirs(argv[1],Dirs);
  19.       if ( !DirCount ) {
  20.          printf("No directory matching \"%s\"!\a\n",argv[1]);
  21.          exit(EXIT_FAILURE);
  22.       }
  23.       if ( 1 == DirCount ) {
  24.          printf("1 directory found.\n");
  25.          ChangeToDirectory(Dirs[0]);
  26.       } else {
  27.          printf("%d directories found.\n",DirCount);
  28.          for ( choice = 0; choice < DirCount; choice++ ) {
  29.             printf("cd %s (Y/N) ? ",Dirs[choice]);
  30.             GetResponse:
  31.             Response = toupper(getch());
  32.             if ( !strchr("YN",Response) ) {
  33.                   printf("\a");
  34.                   goto GetResponse;
  35.             }
  36.             printf("%c\n",Response);
  37.             if ( 'Y' == Response ) {
  38.                ChangeToDirectory(Dirs[choice]);
  39.                break;
  40.             }
  41.          }
  42.       }
  43.    }
  44. }
  45.  
  46. FindDirs(pSpec,pDirList) // find a directories matching spec and return
  47. {
  48.    // determine spec searching for to see if search all drives, all of one
  49.    // drive, or specified subbdirectories
  50.    NameParts = SplitFileName(pSpec);
  51.    if ( NameParts.dir[0] == 0 ) {
  52.       lDirCount = SearchAllDrives(pSpec,pDirList);
  53.    } else if ( 2 == strlen(NameParts.dir)  &&  NameParts.dir[1] == ':' )
  54.       lDirCount = SearchOneDrive(NameParts.dir[0],pSpec+2,pDirList,0);
  55.    else {
  56.       lDirCount = SearchSubdir(pSpec,pDirList,0);
  57.    }
  58.    return lDirCount;
  59. }
  60.  
  61. SearchSubdir(pSpec,pDirList,pDirCount) // search from SearchSpec for all matching files
  62. {
  63.    lDirCount = pDirCount;
  64.    List = Directory(pSpec,TRUE,FATTR_RDONLY|FATTR_SUBDIR|FATTR_ARCHIVE,FATTR_SUBDIR);
  65.    if ( NULL != List ) {
  66.       lCount = 1 + GetArraySpan(List);
  67.       for ( i = 0; i < lCount; i++ )
  68.          pDirList[lDirCount++] = List[i].name;
  69.    }
  70.    return lDirCount;
  71. }
  72.  
  73. SearchOneDrive(DriveLetter,SearchSpec,pDirList,pDirCount)
  74. {
  75.    sprintf(FullSearchSpec,"%c:\\%s",DriveLetter,SearchSpec);
  76.    return SearchSubDir(FullSearchSpec,pDirList,pDirCount);
  77. }
  78.  
  79. SearchAllDrives(pSearchSpec,pDirList) // look on all valid drives
  80. {
  81.    lDirCount = 0;
  82.    for ( DriveLetter = 'C'; DriveLetter <= 'Z'; DriveLetter++ ) {
  83.       sprintf(DriveCurdir,"%c:.",DriveLetter);
  84.       if ( NULL != FullPath(DriveCurdir) ) {
  85.          lDirCount = SearchOneDrive(DriveLetter,pSearchSpec,pDirList,lDirCount);
  86.       }
  87.    }
  88.    return lDirCount;
  89. }
  90.  
  91. ChangeToDirectory(pDirname)
  92. {
  93.    strcpy(CDF_DIR,pDirname);
  94.    if ( ':' == pDirName[1] )
  95.       sprintf(CDF_DRV,"%c:",pDirname[0]);
  96. }
  97.  
  98. Instructions()
  99. {
  100.    printf("\a\n")
  101.    printf("CDfind.cmd - Find directory and change to that directory.\n")
  102.    printf("\n")
  103.    printf("SYNTAX: CDfind <DirSpec>\n")
  104.    printf("\n")
  105.    printf("Where:  DirSpec - Directory specification to match in finding file\n")
  106.    printf("\n")
  107.    printf("Examples: CDfind C:MDOS       Find a MDOS directory on C:\n")
  108.    printf("          CDfind E:UT*        Find directory starting with UT on E:\n")
  109.    printf("          CDfind TOOLS        Find TOOLS directory on any drive\n")
  110.    printf("          CDfind D:\\UTL\\BOZ?  Find BOZ? directory below D:\\UTL\n")
  111. }
  112.  
  113. :CENVI_EXIT
  114. IF "%CDF_DIR%" == "" GOTO ALL_DONE
  115. cd %CDF_DIR%
  116. IF "%CDF_DRV%" == "" GOTO ALL_DONE
  117. %CDF_DRV%
  118. :ALL_DONE
  119. SET CDF_DRV=
  120. SET CDF_DIR=
  121.