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

Hi, Can anyone advise on how to print all the files in a given directory(recursively) that have both ".rtf" and ".ftp" extensions?

  • Comment on Printing all the files that have both ".rtf" and ".ftp" by searching recursively in a given directory

Replies are listed 'Best First'.
Re: Printing all the files that have both ".rtf" and ".ftp" by searching recursively in a given directory
by toolic (Bishop) on Apr 12, 2011 at 03:00 UTC
      use Filesys::Tree qw/tree/; my $sysTree = tree({ 'full' => 1,'max-depth' => 3,'pattern' => qr/\.rt +f|\.ftp$/ } ,'.'); files($sysTree); sub files { my %treeree = %{+shift}; for (keys %tree) { if ($tree{$_}->{type} eq 'f'){ print $_,"\n" }elsif ($tree{$_}->{type} eq 'd') { files($tree{$_}->{contents}); } } }

        I only want to print the names of files

        When I run the above script on a directory I get the following output even there are files with same name but with both extensions .rtf and .ftp

        $VAR1 = [];

Re: Printing all the files that have both ".rtf" and ".ftp" by searching recursively in a given directory
by ww (Archbishop) on Apr 12, 2011 at 11:37 UTC
    Quite aside from the absence of code, - - for lack of effort on the title and question.

    The title can, quite plausibly, be read to mean that you want to actually print the contents of files which -- within their bodies -- contain both (.rtf and .ftp). Your actual question makes it seem likely that you want merely to print file_names where those files have .rtf OR .ftp as their extensions.

    But what your question actually asks for is a means to print the files (names or content remains logically ambiguous) which have both extensions -- e.g., filename.rtf.ftp.

Printing files that has two different extensions in a given directory
by Anonymous Monk on Apr 12, 2011 at 20:29 UTC

    Seems like my earlier query was not clear,explain better here.

    For example ,I have files foo.rtf and foo.ftp in a given directory.I want to print such files(print either of them).Basically any two different files with same name but different extension namely .rtf and .ftp needs to be printed.I tried the following but its not printing anything?Can anyone advise?

    use warnings; use strict; use File::Find::Rule; use Data::Dumper; my @files = File::Find::Rule->file() ->name( qr/\.(rtf&ftp)$/ ) ->in( './' ); print Dumper(\@files);

      What is the following code supposed to do?

      qr/\.(rtf&ftp)$/

      Where in perlre is that way of working documented?

      Personally, I wouldn't try to cram the whole logic into File::Find::Rule. I would partition the work as follows:

      1. Find all files
      2. Remove all file names that don't end in .rtf or .ftp
      3. Sort the filenames
      4. For each .ftp file, look if the file following it has an identical name, except for the last four letters.

      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('.');

        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

      So: if a file has the .rtf extension or if it has the .ftp extension, prints its name.

      my @files = File::Find::Rule ->file() ->name( qr/\.(?:rtf|ftp)\z/ ) ->in( '.' );
Re: Printing all the files that have both ".rtf" and ".ftp" by searching recursively in a given directory
by GotToBTru (Prior) on Apr 12, 2011 at 15:43 UTC
    ls -R | grep \.rtf | grep \.ftp