Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Only files starting with xxxx

by Cockneyphil (Acolyte)
on May 11, 2002 at 13:52 UTC ( [id://165875]=perlquestion: print w/replies, xml ) Need Help??

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

This is part of a script which (currently) lists all files in a directory.
How can I alter this script to make it include ONLY the filenames which start with xxxxx ?
opendir THEDIR, "$basepath$ARGV[0]" or die "Unable to open directory: +$!"; my @allfiles = readdir THEDIR; closedir THEDIR; for my $file ( sort { $b <=> $a } @allfiles ) { ..... }

Replies are listed 'Best First'.
Re: Only files starting with xxxx
by TheHobbit (Pilgrim) on May 11, 2002 at 14:13 UTC

    Hi,
    Simple enough... Use grep.Replace the line

    @allfiles = readdir THEDIR;
    with
    @allfiles = grep /^xxxxx/,readdir THEDIR;
    And you're done!

    Cheers


    Leo TheHobbit
Re: Only files starting with xxxx
by choocroot (Friar) on May 11, 2002 at 14:17 UTC
    use the grep function :

    @allfiles = grep { m/^xxxx/ } readdir THEDIR;

    And you'll need to modify your $numfiles calculation ...

Re: Only files starting with xxxx
by fundflow (Chaplain) on May 13, 2002 at 13:33 UTC
    This is probably the simplest:
    @thefiles = <xxxx*>;
Re: Only files starting with xxxx
by dsheroh (Monsignor) on May 11, 2002 at 14:11 UTC
    my @allfiles = readdir THEDIR; closedir THEDIR; my @filtered_files = grep /^xxxxx/, @allfiles; foreach my $file...

    Originally posted as a Categorized Answer.

Re: Only files starting with xxxx
by amitbhosale (Acolyte) on Feb 14, 2008 at 09:04 UTC
    Hi,
    Below mentioned script list's the files starting with "xxx"
    #!/usr/bin/perl -w use strict; sub dodir { opendir(DIR,$_[0]) or die "Couldn't open $_[0] directory $!"; my $dir=$_[0]; if ( $dir !~ /\/$/ ) { $dir .= "/"; } my @List=readdir(DIR); closedir(DIR); splice(@List,0,2); foreach my $file (@List) { $file = $dir.$file; if( -d $file) { dodir($file); } elsif ($file =~ /xxx*/ ) { print "\n File $file is present in $di +r"; #print "\n File type: ",stat($file)->mode; } } } my $dir_name="/home"; if ( -d $dir_name) { dodir($dir_name); }
    ========o/p======
    File /home/myprog/xxxabc is present in /home/myprog/ File /home/myprog/xxxpqr is present in /home/myprog/

    Originally posted as a Categorized Answer.

Re: Only files starting with xxxx
by Cockneyphil (Acolyte) on May 11, 2002 at 14:57 UTC
    Thanx everyone --ooo0---"(_)"---0ooo---

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found