home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / FILCOUNT.C < prev    next >
C/C++ Source or Header  |  1991-12-25  |  2KB  |  94 lines

  1. /*
  2. **  FILCOUNT.C - counts directories and /or files in a directory
  3. **
  4. **  public domain demo by Bob Stout
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9.  
  10. #undef TRUE
  11. #undef FALSE
  12. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  13.  
  14. #ifdef __ZTC__
  15.  #include <dos.h>
  16. #elif defined(__TURBOC__)
  17.  #include <dir.h>
  18.  #include <dos.h>
  19.  #define _dos_findfirst(f,a,b) findfirst(f,b,a)
  20.  #define _dos_findnext(b) findnext(b)
  21.  #define find_t ffblk
  22.  #define _A_SUBDIR FA_DIREC
  23.  #define attrib ff_attrib
  24.  #define name ff_name
  25. #else                   /* assume MSC/QC                                */
  26.  #include <dos.h>
  27.  #include <errno.h>
  28. #endif
  29.  
  30. #undef SUCCESS
  31. #define SUCCESS 0
  32.  
  33. #define LAST_CHAR(str) (str)[strlen(str) - 1]
  34.  
  35. unsigned DirCount = 0, FileCount = 0;
  36.  
  37. /*
  38. **  Arguments: 1 - directory to search
  39. **             2 - search subdirectories: TRUE or FALSE
  40. */
  41.  
  42. void do_dir(char *path, int recurse_flag)
  43. {
  44.       char search[67], new[67];
  45.       struct find_t ff;
  46.  
  47.       strcpy(search, path);
  48.       if ('\\' != LAST_CHAR(search))
  49.             strcat(search, "\\");
  50.       strcat(search, "*.*");
  51.       if (SUCCESS == _dos_findfirst(search, 0xff, &ff)) do
  52.       {
  53.             if ('.' == *ff.name)
  54.                   continue;
  55.             if (ff.attrib & _A_SUBDIR)
  56.             {
  57.                   DirCount++;
  58.                   if (recurse_flag)
  59.                   {
  60.                         strcpy(new, path);
  61.                         if  ('\\' != LAST_CHAR(new))
  62.                               strcat(new, "\\");
  63.                         strcat(new, ff.name);
  64.                         do_dir(new, recurse_flag);
  65.                   }
  66.             }
  67.             else  FileCount++;
  68.       } while (SUCCESS == _dos_findnext(&ff));
  69. }
  70.  
  71. /*
  72. **  Simple resursive file/directory counter
  73. **
  74. **  Usage: FILCOUNT [path_name] [{Y | N}]
  75. **
  76. **  Notes: 1. If a path name isn't specified, the current directory is assumed
  77. **         2. Default recursion flag is FALSE
  78. **         3. Path must be specified in order to specify the recursion flag
  79. */
  80.  
  81. void main(int argc, char *argv[])
  82. {
  83.       char *Dir =".";
  84.       LOGICAL recurse = FALSE;
  85.  
  86.       if (1 < argc)
  87.             Dir = argv[1];
  88.       if (2 < argc)
  89.             recurse = (NULL != strchr("Yy", *argv[2]));
  90.       do_dir(Dir, recurse);
  91.       printf("Counted: %d Directories and %d Files\n",
  92.               DirCount, FileCount);
  93. }
  94.