🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.msdos.programmer
Article #1061

WANTED: Code to search DOS directory/subdirectories

#1061
From: Alan Eldridge
Date: Sat, 29 Aug 1992 17:38
109 lines
4887 bytes
To: rca@csd4.csd.uwm.edu

28 Aug 92, Robert C Allender writes to All:

 RCA> I'm trying to find a reasonable block of code that will search through a
 RCA> DOS directory using Turbo C 3.0 in DOS mode.  I had what I thought was a
 RCA> good source listing in the C User's Journal - February 1992 edition, but
 RCA> it won't compile to save my life.  Has anyone else tried this code?  Does
 RCA> anyone have a good program that does this?  All I need to do is search
 RCA> every file on a drive and check the date stamp.  If it's greater than x,
 RCA> set the archive bit to on.  Pretty simple stuff.
 RCA>
 RCA> Just so I don't get anyone upset, I *did* RTFM.  As a matter of fact, I
 RCA> called tech support because the FM doesn't give all the details about the
 RCA>  _dos_findfirst() and _dos_findnext() functions that are supposed to do
 RCA> this kind of stuff.
 RCA>
 RCA> eg.
 RCA>
 RCA>      { ...
 RCA>
 RCA>        struct find_t ffblk;
 RCA>        int done;
 RCA>        printf("Directory of C:\\\n");
 RCA>        done = _dos_findfirst("C:\\*.*",_A_SUBDIR,&ffblk);
 RCA>        while(!done)
 RCA>        {
 RCA>           printf(" %s\n", ffblk.name);
 RCA>           done = _dos_findnext(&ffblk);
 RCA>        }
 RCA>        return 0;
 RCA>
 RCA>      } ...
 RCA>
 RCA> the _A_SUBDIR is supposed to return only DOS subdirectory entries,
 RCA> however it seems to (as do the other alternatives) also return normal
 RCA> files that are  in the same directory with the directory entries. If
 RCA> anyone can help, I'd really appreciate it. -Rob Allender rca@bfs.uwm.edu

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.

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.

#define MAKE_RM_EXE    1

#if MAKE_RM_EXE

#include    <stdio.h> #include    <stdlib.h>

static int  verbose = 0; static int  recurse = 0;

#define     VPrintf if (verbose) printf

#endif

#include    <string.h>

#include    <dos.h> #include    <dir.h>

static void makefullpath(char *path, char *name, char *fullpath)
{
    char    dsk[ MAXDRIVE ]; char    dir[ MAXDIR ]; char    file[ MAXFILE ]; char    ext[ MAXEXT ]; char    newfile[ MAXFILE ]; char    newext[ MAXEXT ];

    fnsplit(path, dsk, dir, file, ext); fnsplit(name, NULL, NULL, newfile, newext); fnmerge(fullpath, dsk, dir, newfile, newext);
}

int NukeFiles(char *path, int recurse)
{
    int             cnt=0; struct ffblk    ffb;

    if (!findfirst(path, &ffb, -1)) { do { char    fullpath[ MAXPATH ];

            if (!strcmp(ffb.ff_name, ".") || !strcmp(ffb.ff_name, "..")) continue;

            makefullpath(path, ffb.ff_name, fullpath);

            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;
}

#if MAKE_RM_EXE

static void options(char *opts)
{
    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; } }
}

int main(int ac, char **av)
{
    int cnt=0;

    if (ac > 1 && av[1][0] == '-' || av[1][0] == '/') { options(*++av); --ac; }

    if (ac == 1) { fprintf(stderr, "usage: nuke [-rRvV] path ...\n"); return 1; }

    while (--ac > 0) cnt += NukeFiles(*++av,recurse);

    VPrintf("Nuked %d files!\n", cnt); return 0;
}

#endif


Alan

--  
Internet: aeldridge@p473.f150.n3603.z1.FIDONET.ORG
UUCP: ...!myrddin!tct!psycho!150.473!aeldridge
Note:psycho is a free gateway between Usenet & Fidonet. For info write to
     root@psycho.fidonet.org.
Technical Details
Message-ID: <25413.2A9FBE43@psycho.fidonet.org>
Path: arc1-ca.pugleaf.net!sparky!uunet!sybus.sybus.com!myrddin!tct!psycho!p473.f150.n3603.z1.FIDONET.ORG!aeldridge

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!sybus.sybus.com!myrddin!tct!psycho!p473.f150.n3603.z1.FIDONET.ORG!aeldridge
From: aeldridge@p473.f150.n3603.z1.FIDONET.ORG (Alan Eldridge)
Sender: ufgate@psycho.fidonet.org (newsout1.24)
Newsgroups: comp.os.msdos.programmer
Subject: WANTED: Code to search DOS directory/subdirectories
Message-ID: <25413.2A9FBE43@psycho.fidonet.org>
Date: Sat, 29 Aug 92 13:38:10 EDT
Organization: FidoNet node 1:3603/150.473 - Carrie's Living Roo, Largo FL
Lines: 109