> PC>  chdir "/u131/tmp" or die $!;
> PC>  opendir(HERE, '.');
> PC>  @AllFiles = readdir(HERE);
> PC>  foreach $Name (@AllFiles) {
> PC>    if (-f $Name) {next}
> PC>    if ((-d $Name) and ($Name =~ /^[A-Z]+$/)) {rmdir $Name}
> PC>    if ((-d $Name)) {print "$Name\n"}
> PC>    }                          

> It's working great.  Thank you.  But one more thing,
> it's removing all the empty directories but what about
> directories with files in them.  It errors out when it hits
> a directory with files in it.  I have just files in this directory
> that I don't want to delete.  I only want to delete the
> directories with Capital letters and the files in them.
> Thank you for any help or ideas.

You have to go to another level of programming,
and use recursion. You may find this easy or it
may wrap your brain in knots, it depends on the
person.

Basically, you have to write a sub that does the
following sort of thing (ignoring your wrinkle that
you don't want to delete all the files in the initial
directory):

    emptyoutdir
        foreach direntry
            file: delete it
            dir:
                cd direntry
                emptyoutdir
                cd ..
                rm direntry

Writing that is easy once you understand that
whatever variable you use to contain the current
direntry needs to be local to its own level of call;
otherwise, the 'rm direntry' is going to be acting
on the 'direntry' value last set by the emptyoutdir
called just before it (or the emptyoutdir that that
called, or... and so on, down the depths of stacked
emptyoutdir calls).

Go on, have a go, then post your best shot.

Initially, just try to print the files/dirs, don't try to
delete them. Remember, you are playing with
fire here; something that will delete entire trees
of files/dirs, and anyway, you don't want to keep
deleting your test dir structure. To make it a bit
easier to see what's going on, add an indent
variable in to the sub like so:

    emptyoutdir
        my indent = indent;
        indent++;
        foreach direntry
            file: print " " x indent . file
            dir:
                cd direntry
                emptyoutdir
                cd ..
                print " " x indent . direntry

have fun...

Reply via email to