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

Re: perl question



On Wednesday 17 July 2002 03:53 pm, Mike Egglestone wrote:
> 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)

#! /usr/bin/perl -w
 
$dir = "/path/to/dir";

opendir(DIR, $dir) or die "can't opendir $dir: $!";
 
while ( defined ($file = readdir DIR) ) {
    next if $file =~ /^\.\.?$/;     # skip . and ..
    unlink $file;
}

Quick and dirty, but I think it will delete all files in one directory.  
Won't handle subdirs.  Then you could probably just

close(DIR);
rmdir $dir;

to get rid of the directory.


-- 
Bud Rogers <budr@sirinet.net>   http://www.sirinet.net/~budr
All things in moderation.  And not too much moderation either.



Reply to: