home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / DELFREE / DELFREE.C next >
Text File  |  1992-10-05  |  6KB  |  162 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2.  
  3.                            D E L E T E - F R E E
  4.                    Public Domain Utility by Justin Newman
  5.                           Auanix Technoligy, INC
  6.  
  7.    The purpose of this program is to delete directories without files
  8.    (empty directories) - this file is now placed in the public domain
  9.    ------------------------------------------------------------------
  10.    If you find this program of use (and I do !), please write me some
  11.    mail (electronic or postal mail), or send me a small donation:
  12.  
  13.                  Justin Newman, Auanix Techoligy, INC
  14.                  2720 Willamette Street, Suite #106
  15.                  Eugene, OR 97405
  16.  
  17.                  or (electronicly):
  18.  
  19.                  Baddude's BBS: 503/688-2860 email user "19"
  20.                  Billboard BBS: 503/688-2056 email "Holmeboy"
  21.  
  22.                  or send me a message through fidonet/netmail.
  23.  
  24.    If you send me a disk and enough to cover postage and handling for
  25.    that disk ($1.00 should cover it), I will send you source code for
  26.    tons of utilities (by myself, and by many other authors).   If you
  27.    have any questions, please ask.  Thank you.  If you make changes &
  28.    redistribute this file (and please do), then add to the history.
  29.  
  30.                  ****** HISTORY: ******************************
  31.                   October 4th, 1992       Program created, and
  32.                     - Version 1.00        and released
  33.                  **********************************************
  34.  
  35.  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  36.  
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <dir.h>
  40.  
  41. #ifdef VERSION
  42. #undef VERSION
  43. #endif /* end of "ifdef" */
  44. #define VERSION "1.00"              /* the version of the program */
  45.  
  46. #ifdef RELEASED
  47. #undef RELEASED
  48. #endif /* end of "ifdef" */
  49. #define RELEASED "October 4th, 1992" /* the date this file was released */
  50.  
  51. #ifdef error               /* a macro to print a error message and exit */
  52. #undef error
  53. #endif /* end of "ifdef" */
  54. #define error( s ) printf( "*** \a error: %s  ***\n", s );\
  55.                   exit( 1 ) /* exit out with error number 1 */
  56.  
  57. #ifdef message          /* a macro to print some text */
  58. #undef message
  59. #endif /* end of "ifdef" */
  60. #define message( s ) printf( "%s\n", s ) /* print message */
  61.  
  62. #ifdef message_n        /* a macro to print some text with a number */
  63. #undef message_n
  64. #endif /* end of "ifdef" */
  65. #define message_n( d, s ) printf( "%d %s\n", d, s ) /* print message */
  66.  
  67. #ifdef banner              /* a macro to print our banner, or logon, etc */
  68. #undef banner           /* should show modifications, last modder, etc */
  69. #endif /* end of "ifdef" */
  70. #define banner()  printf( "DeleteFree version %s, released %s\n", VERSION, RELEASED );\
  71.                printf( "\tBy: Justin Newman, Auanix Technoligy, INC\n" );\
  72.                printf( "\tPurpose: To delete empty directories\n" );\
  73.                printf( "\tSupport: Baddude's BBS - 503/688-2860\n\n" )
  74.  
  75. int deleted_dirs = 0;            /* directories deleted */
  76.  
  77. int doit( char *path );
  78.  
  79. /* * * * start of main * * * */
  80.  
  81. int main( int argc, char *argv[] )
  82. {
  83.    char drive[68] = "\0";        /* storage for our search path */
  84.    char temp[81];                /* temp storage buffer */
  85.  
  86.    banner();         /* sign in, tell them who we are and what we do */
  87.  
  88.    if( argc >= 2 && argv[1] )    /* did they pass a valid command line? */
  89.        strcpy( drive, argv[1] );  /* yes - get path from command line */
  90.    else
  91.       {
  92.    /* tell them they need a path on the command line */
  93.       error( "use: delfree [drivename, ie., \"d:\"]" );
  94.       }
  95.  
  96.    if( toupper( drive[0] ) > ( setdisk( getdisk() ) + 'A' ) ||
  97.       drive[1] != ':' )          /* was that path valid, and does */
  98.       {                                   /* it exist? */
  99.       error( "Invalid path on command line" );    /* nope, error */
  100.       }
  101.  
  102.    message( "Searching for free directories..." );
  103.    message( "---------------------------------" );
  104.    doit( drive );      /* start searching and deleting directories */
  105.    if( deleted_dirs )
  106.       {
  107.       message_n( deleted_dirs, "dirs deleted" );
  108.       }
  109.    else  message( "...No directories free" );
  110.  
  111.    return 0;
  112. }
  113.  
  114. /* * * * start of doit * * * */
  115.  
  116. int doit( char *path )
  117. {
  118.    char search[68], new[68];  /* storage for paths */
  119.    int files_in_this_directory = 0; /* files in our directory */
  120.    int files = 0;          /* files in our child directory we called */
  121.    struct ffblk ffblk;  /* searching structure declared in <dir.h> */
  122.  
  123.    strcpy( search, path );
  124.    strcat( search, "\\*.*" );
  125.  
  126.    if( findfirst( search, &ffblk, 255 ) == 0 )  /* is there any files? */
  127.       {
  128.       do    /* yes, so search for every one, and search through the
  129.          directories */
  130.          {
  131.             /* is this file a directory? and it is not the parent directories
  132.             ie. "." or ".." */
  133.          if( (ffblk.ff_attrib & 0x10) && *ffblk.ff_name != '.' )
  134.             {
  135.                strcpy( new, path ); /* make new path */
  136.                strcat( new, "\\" );
  137.                strcat( new, ffblk.ff_name );
  138.                files = doit( new ); /* call doit for the new dir */
  139.                if( files == 0 )  /* how many files were found */
  140.                   {
  141.                   message( strlwr( new ) );
  142.                   rmdir( new );
  143.                   deleted_dirs++;
  144.                   }
  145.             }
  146.             /* if not a parent, root, etc,
  147.                 then increase file counter */
  148.           if( *ffblk.ff_name != '.' )
  149.             files_in_this_directory++; /* increase the files we found */
  150.          }
  151.       while( findnext( &ffblk ) == 0 );  /* keep on searching and filling ffblk */
  152.       }
  153.  
  154.       /* return the number of files we found to our
  155.          parent */
  156.    return files_in_this_directory;  
  157. }
  158.  
  159. /* * * * end of file * * * */
  160.  
  161. 
  162.