in reply to unlinking multiple files at once

Yet another way . . .

opendir( CUR, "somedir" ) or die "Can't opendir: $!\n"; my @digit_files = grep /^\d{3}/, readdir( CUR ); closedir( CUR ); foreach( @digit_files ) { unlink "somedir/$_" or warn "Can't unlink somedir/$_: $!\n"; }

Replies are listed 'Best First'.
Re: Re: unlinking multiple files at once
by $code or die (Deacon) on Oct 03, 2001 at 05:18 UTC
    In most cases, this is my preferred method because you have more control than a glob (you have the full power of Perl's Regular Expressions at your disposal). Although as boo_radley points out - this will delete all files beginning with 3 digits. The original question was regarding a specific set of three digits.

    And since you only want to delete files - not directories, you might want to change the grep:
    my $prefix = '123'; my @digit_files = grep { /^$prefix/ && -f } readdir( CUR );
    Update: fixed formatting and added why I prefer this method over globbing.

    Simon Flack ($code or die)
    $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
    =~y'_"' ';eval"die";print $_,lc substr$@,0,3;