Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: chosse selected files and delete rest

by jrsimmon (Hermit)
on Jul 28, 2009 at 15:02 UTC ( [id://783935]=note: print w/replies, xml ) Need Help??


in reply to chosse selected files and delete rest

Untested

#keep based on pattern match opendir(DIR, $destinationDirectory) or die $!; while(my $file = readdir(DIR)){ next if -d $file;#skip directory entries unlink unless $file =~ /$pattern/; }
#keep based on a prepopulated hash %prepopulatedHash =(FILE1 => 1, FILE2 => 1, FILEN => 1); opendir(DIR, $destinationDirectory) or die $!; while(my $file = readdir(DIR)){ next if -d $file;#skip directory entries unlink unless exists($prepopulatedKeepHash{$file}; }

Replies are listed 'Best First'.
Re^2: chosse selected files and delete rest
by Bloodnok (Vicar) on Jul 29, 2009 at 08:52 UTC
    ...or, for your pattern match match based solution, why not utilize the fact that unlink takes a list viz (also untested:-):
    unlink grep { -f $_ && ! /$pattern/ } glob "$Directory/*";

    A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://783935]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2025-02-08 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (95 votes). Check out past polls.