-2

I have a directory containing thousand of file.
Suppose I have 3 pdf files having same name like:

  1. sample_Q1.pdf
  2. sample_Q2.pdf
  3. sample_Q3.pdf

Now I want to find the file list having particular name started with "Sample".

I'm currently using:

#!/usr/bin/perl 
use strict;
use warnings;
my $dir = '/home/gaurav/Desktop/CSP';
opendir( DIR, $dir ) or die $!;
while ( my $file = readdir(DIR) ) {

# We only want files
    next unless ( -f "$dir/$file" );

# Use a regular expression to find files ending in .txt
    next unless ( $file =~ /\.pdf$/ );
    print "$file\n";
}

closedir(DIR);
exit 0;
serenesat
  • 4,611
  • 10
  • 37
  • 53
  • #!/usr/bin/perl use strict; use warnings; my $dir = '/home/gaurav/Desktop/CSP'; opendir(DIR, $dir) or die $!; while (my $file = readdir(DIR)) { # We only want files next unless (-f "$dir/$file"); # Use a regular expression to find files ending in .txt next unless ($file =~ /\.pdf$/); print "$file\n"; } closedir(DIR); exit 0; – Gaurav Jain Jun 29 '15 at 10:16
  • I have copied you cored from comment into main text. Please can you check it's still correct? – Sobrique Jun 29 '15 at 10:42
  • 2
    What are you having problems with? You already have code to check that the file name ends with ".pdf". It's simple to extend the logic to check that the file name starts with "Sample" as well. – Dave Cross Jun 29 '15 at 10:44
  • Actually geetring name i want to make insert in database .i got all name starting with sample that is count 3 .Now i want to use these name to make 3 insert in mysql .Where should i save it to make insert for all three. – Gaurav Jain Jun 29 '15 at 11:12
  • That is an entirely different question, and probably warrants opening a new one. – Sobrique Jun 29 '15 at 12:32

4 Answers4

3

I'd suggest that rather than readdir what you really want is glob. This latter expands a pattern in the same way as the shell does, and returns matches. The reason it's particularly useful is that readdir returns file names, where glob returns full paths. (e.g. directory too).

E.g.

#!/usr/bin/perl 
use strict;
use warnings;
my $dir = '/home/gaurav/Desktop/CSP';

foreach my $file ( glob ( "$dir/sample*.pdf" ) ) {
    print $file,"\n";
}

if you want to skip any 'non files' there's two approaches:

next unless -f $file; 

Or:

foreach my $file ( grep { -f $_ } glob ( "$dir/sample*.pdf" ) )  {

But that's probably a moot point unless you've got directories suffixed .pdf which would be a bit unusual.

Sobrique
  • 52,974
  • 7
  • 60
  • 101
2
my $DirectoryName = '......';

chdir( $DirectoryName ) or die "Can't change directory: $!\n";
my @files = glob( "sample*");
for my $file (@files) {
  print $file,"\n";
}
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
AnFi
  • 10,493
  • 3
  • 23
  • 47
0

This will give list of all files name which start with sample and have extention .pdf:

use warnings;
use strict;

opendir my $dir, "/home/gaurav/Desktop/CSP" or die "Can't open directory: $!";
my @files = grep { /sample.*\.pdf$/ } readdir $dir;
print "@files";
closedir $dir;

Use a foreach loop if you want print file names one by one:

foreach my $file (@files)
{
    print "$file\n";
}
serenesat
  • 4,611
  • 10
  • 37
  • 53
0

I will do that like below.

$file =~ /^sample.*\.pdf$/i

Here 'i' is used for case insensitive. it will search both 'sample' and 'Sample'.

while ( my $file = readdir(DIR) ) 
{

      if((-f "$dir/$file" ) && ($file =~ /^sample.*\.pdf$/i))
      {
          print "$file \n";
      }
}