Mail Archives: djgpp/2000/04/05/18:15:38
Ben Davis wrote:
> Eli Zaretskii wrote in message <38EA1EA7 DOT 6A8E81BE AT is DOT elta DOT co DOT il>...
> >You can use the directory-traversal functions provided in the library.
>
> I've considered that, but they both check directories before they check the
> subdirectories. If I'm deleting, I need to empty a directory before I remove
> it.
I wrote a deltree program using findfirst/findnext a long time ago (before DOS
even had a DELTREE), but it's too much of a mess to make public -- I think you
could simplify things a lot with ftw, anyway. However, you don't need to make
any assumptions about the order in which the files and subdirectories are
reported. The strategy is simple, regardless of the routines used for the
traversal: after descending into the directory to be removed, if you encounter a
file, delete it; if you encounter a directory, descend into it; if the current
directory is empty, ascend back out of it and remove it. Eventually, the
program will dig its way to the very bottom, possibly deleting some files along
the way; with no more directories to enter, it must eventually start removing
files and working its way back up the tree. When you ascend out of the
specified directory, remove it and stop.
I would highly recommend testing routines like this with dummy files on a spare
floppy. It will slow down execution a bit, but at least you won't lose your
entire hard drive because of a faulty stopping criterion.
-Eric Rudd
rudd AT cyberoptics DOT com
- Raw text -