home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FILCOUNT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  74 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  FILCOUNT.C - counts directories and /or files in a directory
  5. **
  6. **  public domain demo by Bob Stout
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "sniptype.h"
  12. #include "dirport.h"
  13.  
  14. unsigned DirCount = 0, FileCount = 0;
  15.  
  16. /*
  17. **  Arguments: 1 - directory to search
  18. **             2 - search subdirectories: True_ or False_
  19. */
  20.  
  21. void do_dir(char *path, int recurse_flag)
  22. {
  23.       char search[67], new[67];
  24.       DOSFileData ff;
  25.  
  26.       strcpy(search, path);
  27.       if ('\\' != LAST_CHAR(search))
  28.             strcat(search, "\\");
  29.       strcat(search, "*.*");
  30.       if (Success_ == FIND_FIRST(search, _A_ANY, &ff)) do
  31.       {
  32.             if ('.' == *ff_name(&ff))
  33.                   continue;
  34.             if (ff_attr(&ff) & _A_SUBDIR)
  35.             {
  36.                   DirCount++;
  37.                   if (recurse_flag)
  38.                   {
  39.                         strcpy(new, path);
  40.                         if  ('\\' != LAST_CHAR(new))
  41.                               strcat(new, "\\");
  42.                         strcat(new, ff_name(&ff));
  43.                         do_dir(new, recurse_flag);
  44.                   }
  45.             }
  46.             else  FileCount++;
  47.       } while (Success_ == FIND_NEXT(&ff));
  48. }
  49.  
  50. /*
  51. **  Simple recursive file/directory counter
  52. **
  53. **  Usage: FILCOUNT [path_name] [{Y | N}]
  54. **
  55. **  Notes: 1. If a path name isn't specified, the current directory is assumed
  56. **         2. Default recursion flag is False_
  57. **         3. Path must be specified in order to specify the recursion flag
  58. */
  59.  
  60. main(int argc, char *argv[])
  61. {
  62.       char *Dir =".";
  63.       Boolean_T recurse = False_;
  64.  
  65.       if (1 < argc)
  66.             Dir = argv[1];
  67.       if (2 < argc)
  68.             recurse = (NULL != strchr("Yy", *argv[2]));
  69.       do_dir(Dir, recurse);
  70.       printf("Counted: %d Directories and %d Files\n",
  71.             DirCount, FileCount);
  72.       return 0;
  73. }
  74.