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 warnings;
use strict;
use File::Find::Rule;
use Data::Dumper;
my @files = File::Find::Rule->file()
->name( '*.rtf', '*.ftp' )
->in( './' );
print Dumper(\@files);
Super Search where title contains all of "recurs", "direct"
: ?node_id=3989;HIT=recurs%20direct;re=N
| [reply] [d/l] |
|
|
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});
}
}
}
| [reply] [d/l] |
|
|
| [reply] |
|
|
|
|
$VAR1 = [];
| [reply] [d/l] |
|
|
|
|
|
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. | [reply] [d/l] [select] |
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);
| [reply] [d/l] |
|
|
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:
- Find all files
- Remove all file names that don't end in .rtf or .ftp
- Sort the filenames
- For each .ftp file, look if the file following it has an identical name, except for the last four letters.
| [reply] [d/l] [select] |
|
|
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('.');
| [reply] [d/l] [select] |
|
|
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
| [reply] [d/l] |
|
|
|
|
|
|
|
|
|
|
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( '.' );
| [reply] [d/l] |
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
| [reply] |