in reply to Printing files that has two different extensions in a given directory
in thread Printing all the files that have both ".rtf" and ".ftp" by searching recursively in a given directory

You're still very unclear, but I'm pretty sure you want the following:

For each file matching *.rtf, print its name if that directory also has similarly named file with the .ftp extension.

my @dirs = File::Find::Rule ->directory() ->in('.'); for my $dir (@dirs) { my @rtfs = File::Find::Rule ->maxdepth(1) ->file() ->in($dir); for my $rtf (@rtfs) { ( my $ftp = $rtf ) =~ s/\.rtf\z/.ftp/; if (-e $ftp) { print("$rtf\n"); } } }

Update: Condensed:

my @files = File::Find::Rule ->file() ->name('*.rtf') ->exec(sub{ my $rtf = $_; ( my $ftp = $rtf ) =~ s/\.rtf\z/.ftp/; -e $ftp; }) ->in('.');

Replies are listed 'Best First'.
Re^2: Printing files that has two different extensions in a given directory
by Anonymous Monk on Apr 12, 2011 at 21:02 UTC

    Thanks for the reply ikegami.I would like to make one correction,the corresponding .ftp file not necessarily should be in the same directory,it can be in any other directory.

    For example,if the directory structure is like below c:\tests\foo.rtf c:\tests\data\foo.ftp if we run the script from c:\tests,it should print foo.rtf

      use strict; use warnings; use File::Find::Rule qw( ); use File::Basename qw( basename ); my %files; for ( File::Find::Rule ->file() ->name('*.rtf', '*.ftp') ->in('.') ) { my ($name, $ext) = basename($_) =~ /^(.*)\.(.*)\z/; ++$files{$name}{$ext}; } for my $name (%files) { print("$name\n") if $files{$name}{rtf} && $files{$name}{ftp}; }

      Update: Tested. Fixed.

        Without repeating the extension:
        use strict; use warnings; use File::Find::Rule qw( ); my %files; for ( File::Find::Rule ->file() ->name('*.rtf', '*.ftp') ->in('.') ) { my ($name) = /^(.*)\./; ++$files_by_ext{$name}; } say for grep {$files{$_} == 2} keys %files;

        Thanks,this is exactly what i want,but the thing is I want to print the absolute path ,instead of just the file names.Can you please advise? </p?