sito has asked for the wisdom of the Perl Monks concerning the following question:

I've been using the following code to recursively view, and then delete files matching a certain pattern.
@files = <$input$dir>; foreach $file (@files) { if(-f $file && ($file =~ /\.DS_Store/i)) { print "$file\n"; unlink $file; } }
However, this doesn't work, as no files starting in `.` are displayed. If it makes any difference, I'm on perl 5.10, on Windows XP Pro. Does anyone have a solution for this, without completely rewriting? Thanks,
~Sito

Replies are listed 'Best First'.
Re: View hidden files?
by ikegami (Patriarch) on Dec 14, 2008 at 16:33 UTC
    glob doesn't list "." files unless you ask it to.
    my @files = <$dir/.* $dir/*>;

    You'll have fewer problems if you use bsd_glob directly.

    use File::Glob qw( bsd_glob ); my @files = ( bsd_glob("$dir/.*"), bsd_glob("$dir/*") );

      Doesn't glob just call File::Glob::bsd_glob (by default) anyway?

      my @files = glob "$dir/{,.}*";
      Works fine for me :-)

      Update: I should finish reading the docs... apparently, glob still splits on space, though I'm not sure why.

      Thanks, you first code worked perfectly.
        Only as long as you don't try to list a dir that has a space in its name. The second executes the same code, but doesn't treat spaces specially.
Re: View hidden files?
by jwkrahn (Abbot) on Dec 14, 2008 at 16:55 UTC

    Use opendir and readdir to read all file names from a directory:

    opendir my $DH, $dir or die "Cannot open '$dir' $!"; while ( my $file = readdir $DH ) { if ( -f "$dir/$file" && $file =~ /^\.DS_Store/i ) { print "$file\n"; unlink "$dir/$file" or warn "Cannot delete '$dir/$file' $!"; } }
Re: View hidden files?
by poolpi (Hermit) on Dec 15, 2008 at 08:56 UTC
    #!/usr/bin/perl use strict; use warnings; use File::Find::Rule; my $rule = File::Find::Rule->new; my $pattern = qr/^\.DS_Store/i; $rule->file; $rule->name($pattern); $rule->exec( sub { my ( $shortname, $path, $fullname ) = @_; print $fullname, "\n"; unlink $fullname or warn "Can't delete $fullname\n"; } ); my @files = $rule->in($dir);


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb