home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / msdos / programm / 8875 < prev    next >
Encoding:
Text File  |  1992-08-30  |  4.7 KB  |  120 lines

  1. Path: sparky!uunet!sybus.sybus.com!myrddin!tct!psycho!p473.f150.n3603.z1.FIDONET.ORG!aeldridge
  2. From: aeldridge@p473.f150.n3603.z1.FIDONET.ORG (Alan Eldridge)
  3. Sender: ufgate@psycho.fidonet.org (newsout1.24)
  4. Newsgroups: comp.os.msdos.programmer
  5. Subject: WANTED: Code to search DOS directory/subdirectories
  6. Message-ID: <25413.2A9FBE43@psycho.fidonet.org>
  7. Date: Sat, 29 Aug 92 13:38:10 EDT
  8. Organization: FidoNet node 1:3603/150.473 - Carrie's Living Roo, Largo FL
  9. Lines: 109
  10.  
  11. To: rca@csd4.csd.uwm.edu
  12.  
  13. 28 Aug 92, Robert C Allender writes to All:
  14.  
  15.  RCA> I'm trying to find a reasonable block of code that will search through a
  16.  RCA> DOS directory using Turbo C 3.0 in DOS mode.  I had what I thought was a
  17.  RCA> good source listing in the C User's Journal - February 1992 edition, but
  18.  RCA> it won't compile to save my life.  Has anyone else tried this code?  Does
  19.  RCA> anyone have a good program that does this?  All I need to do is search
  20.  RCA> every file on a drive and check the date stamp.  If it's greater than x,
  21.  RCA> set the archive bit to on.  Pretty simple stuff.
  22.  RCA>
  23.  RCA> Just so I don't get anyone upset, I *did* RTFM.  As a matter of fact, I
  24.  RCA> called tech support because the FM doesn't give all the details about the
  25.  RCA>  _dos_findfirst() and _dos_findnext() functions that are supposed to do
  26.  RCA> this kind of stuff.
  27.  RCA>
  28.  RCA> eg.
  29.  RCA>
  30.  RCA>      { ...
  31.  RCA>
  32.  RCA>        struct find_t ffblk;
  33.  RCA>        int done;
  34.  RCA>        printf("Directory of C:\\\n");
  35.  RCA>        done = _dos_findfirst("C:\\*.*",_A_SUBDIR,&ffblk);
  36.  RCA>        while(!done)
  37.  RCA>        {
  38.  RCA>           printf(" %s\n", ffblk.name);
  39.  RCA>           done = _dos_findnext(&ffblk);
  40.  RCA>        }
  41.  RCA>        return 0;
  42.  RCA>
  43.  RCA>      } ...
  44.  RCA>
  45.  RCA> the _A_SUBDIR is supposed to return only DOS subdirectory entries,
  46.  RCA> however it seems to (as do the other alternatives) also return normal
  47.  RCA> files that are  in the same directory with the directory entries. If
  48.  RCA> anyone can help, I'd really appreciate it. -Rob Allender rca@bfs.uwm.edu
  49.  
  50. No, either you misread the doc or they misrepresented the function (check with a DOS Tech Ref). The mask is the set of bits which may be on and the file still be returned. You _always_ get files with no bits on... BTW for TC/BC the function is just called findfirst(), I believe.
  51.  
  52. Here's some example code ... this makes a cheap version of the Unix rm program, complete with recursive delete. Coded for BC++ 2.0 or higher (but should work on TC++ as well). Hope this shines some light on your problem.
  53.  
  54. #define MAKE_RM_EXE    1
  55.  
  56. #if MAKE_RM_EXE
  57.  
  58. #include    <stdio.h> #include    <stdlib.h>
  59.  
  60. static int  verbose = 0; static int  recurse = 0;
  61.  
  62. #define     VPrintf if (verbose) printf
  63.  
  64. #endif
  65.  
  66. #include    <string.h>
  67.  
  68. #include    <dos.h> #include    <dir.h>
  69.  
  70. static void makefullpath(char *path, char *name, char *fullpath)
  71. {
  72.     char    dsk[ MAXDRIVE ]; char    dir[ MAXDIR ]; char    file[ MAXFILE ]; char    ext[ MAXEXT ]; char    newfile[ MAXFILE ]; char    newext[ MAXEXT ];
  73.  
  74.     fnsplit(path, dsk, dir, file, ext); fnsplit(name, NULL, NULL, newfile, newext); fnmerge(fullpath, dsk, dir, newfile, newext);
  75. }
  76.  
  77. int NukeFiles(char *path, int recurse)
  78. {
  79.     int             cnt=0; struct ffblk    ffb;
  80.  
  81.     if (!findfirst(path, &ffb, -1)) { do { char    fullpath[ MAXPATH ];
  82.  
  83.             if (!strcmp(ffb.ff_name, ".") || !strcmp(ffb.ff_name, "..")) continue;
  84.  
  85.             makefullpath(path, ffb.ff_name, fullpath);
  86.  
  87.             if (ffb.ff_attrib & FA_DIREC) { if (recurse) { char    newpath[ MAXPATH ]; strcpy(newpath, fullpath); strcat(newpath, "\\*.*"); #if MAKE_RM_EXE VPrintf("recursing to delete %s\n", newpath); #endif            cnt += NukeFiles(newpath,1); #if MAKE_RM_EXE VPrintf("removing directory %s\n", fullpath); #endif            rmdir(fullpath); } } else { #if MAKE_RM_EXE VPrintf("deleting %s\n", fullpath); #endif            unlink(fullpath); cnt++; } } while (!findnext(&ffb)); } return cnt;
  88. }
  89.  
  90. #if MAKE_RM_EXE
  91.  
  92. static void options(char *opts)
  93. {
  94.     while (*++opts) { switch (opts[ 0 ]) { case 'r': case 'R': recurse = 1; break;         case 'v': case 'V': verbose = 1; break; default: fprintf(stderr, "nuke: unknown option '%c'\n", opts[ 0 ]); break; } }
  95. }
  96.  
  97. int main(int ac, char **av)
  98. {
  99.     int cnt=0;
  100.  
  101.     if (ac > 1 && av[1][0] == '-' || av[1][0] == '/') { options(*++av); --ac; }
  102.  
  103.     if (ac == 1) { fprintf(stderr, "usage: nuke [-rRvV] path ...\n"); return 1; }
  104.  
  105.     while (--ac > 0) cnt += NukeFiles(*++av,recurse);
  106.  
  107.     VPrintf("Nuked %d files!\n", cnt); return 0;
  108. }
  109.  
  110. #endif
  111.  
  112.  
  113. Alan
  114.  
  115. --  
  116. Internet: aeldridge@p473.f150.n3603.z1.FIDONET.ORG
  117. UUCP: ...!myrddin!tct!psycho!150.473!aeldridge
  118. Note:psycho is a free gateway between Usenet & Fidonet. For info write to
  119.      root@psycho.fidonet.org.
  120.