[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: perl question



> >From: "Mike Egglestone"<megglestone@heritage.sd57.bc.ca>
> >To: "debian-user"<debian-user@lists.debian.org>
> >Subject: perl question
> >Date: Thu, Jul 18, 2002, 4:53 AM
> >
> 
> > Hello...
> >
> > Here's one for some of the perl guys....
> >
> > I want to delete a directory that will have files in it...
> > I don't know the name of the files.... there for wildcards might
> > be needed....
> >
> > I understand that "rmdir" will wipe out an empty directory....
> > and "unlink" will wipe out files (only if I know the names of the files)
> >
> > What would be a nice command to remove a dirtory that had files in it?
> > Even better.... what would be a nice command to delete all files
> > in one directory... (leaving the directory intact)

    sub emptydir {
        my $dir = shift;

        opendir DIR,$dir or die "canna opendir $dir";
        my @d = sort grep($_ ne '.' && $_ ne '..',readdir DIR);
        closedir DIR;

        foreach my $item ( @d ) {
            my $path = "$dir/$item";
            undef $@;
            if (-d $path) {
                emptydir($path);
    ###         eval { rmdir $path; };
            } else {
                eval { unlink $path; };
            }
            warn $@ if $@;
        }
    }

but this code is untested and is likely to cause brain damage
and multiple scleroses.

or, just to zap normal files in a known directory, this might
work:

    unlink grep -f,<$dir/*>;

and then again it might not.

-- 
DEBIAN NEWBIE TIP #7 from Will Trillich <will@serensoft.com>
:
Wondering what COMMANDS you have at your disposal? Try pressing
the TAB key at the command line. For example, "apt<TAB>" will
show you all the commands that start with "apt". (This is called
"completion" if you want to look it up in your shell's manpage.)
(Different implementions have the <TAB> completion set up
differently -- you may need to press <TAB> twice.)

Also see http://newbieDoc.sourceForge.net/ ...



Reply to: