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 :-))