The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Code not discarding the sub directory
Discuss Code not discarding the sub directory in the Perl Programming forum on Dev Shed. Code not discarding the sub directory Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 25th, 2012, 12:31 PM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 6
Time spent in forums: 1 h 17 m 24 sec
Reputation Power: 0
|
|
|
Code not discarding the sub directory
Hello,
I have following code to discard the sub- subdirectory first and then print files more than 3 KB, However, it is not discarding the sub direccotry. Any ideas?
use File::Find::Rule;
print "Enter threshold size for files to report ";
chomp( $size_req= <>);
$filename= "C:\\Unicorn";
opendir ($filename, '.') or die "Couldn't open directory, $!";
my $subdir = File::Find::Rule->directory
->name('C:\\Unicorn\\sub')
->prune
->discard;
my $rule = File::Find::Rule->new;
$rule->size( ">3*1024" );
print "\n";
my @subdirs= $rule->in ($subdir);
Thanks!
|

June 26th, 2012, 06:00 PM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
Here are a couple of different examples where I ignore subdirectory(tmp) in directory(temp) when searching for files(*.txt) with size( ">3*1024" ):
example 1:
Code:
use File::Find::Rule;
my $r1 = File::Find::Rule->directory
->name("tmp")
->prune # don't go into it
->discard; # don't report it
my $fn = File::Find::Rule->file
->name("*.txt")
->size( ">3*1024" );
my @files = File::Find::Rule->or( $r1, $fn )
->in( "/temp");
foreach my $file( @files ) {
print "$file\n";
}
example 2:
Code:
use File::Find::Rule;
my $prune_sub_dir = File::Find::Rule
->directory
->name( "tmp" )
->prune
->discard;
my $fn = File::Find::Rule->file
->name( "*.txt" )
->size( ">3*1024" );
my @files = File::Find::Rule
->any( $prune_sub_dir, $fn )
->in( "/temp" );
foreach my $file( @files ) {
print "$file\n";
}
|

June 27th, 2012, 02:45 PM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 6
Time spent in forums: 1 h 17 m 24 sec
Reputation Power: 0
|
|
|
Thanks! that worked just fine.
Quote: | Originally Posted by spacebar208 Here are a couple of different examples where I ignore subdirectory(tmp) in directory(temp) when searching for files(*.txt) with size( ">3*1024" ):
example 1:
Code:
use File::Find::Rule;
my $r1 = File::Find::Rule->directory
->name("tmp")
->prune # don't go into it
->discard; # don't report it
my $fn = File::Find::Rule->file
->name("*.txt")
->size( ">3*1024" );
my @files = File::Find::Rule->or( $r1, $fn )
->in( "/temp");
foreach my $file( @files ) {
print "$file\n";
}
example 2:
Code:
use File::Find::Rule;
my $prune_sub_dir = File::Find::Rule
->directory
->name( "tmp" )
->prune
->discard;
my $fn = File::Find::Rule->file
->name( "*.txt" )
->size( ">3*1024" );
my @files = File::Find::Rule
->any( $prune_sub_dir, $fn )
->in( "/temp" );
foreach my $file( @files ) {
print "$file\n";
}
|
that worked just fine 
|

July 3rd, 2012, 03:58 PM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 6
Time spent in forums: 1 h 17 m 24 sec
Reputation Power: 0
|
|
|
taking name value in variable
Hello,
Thanks for your reply, However, I need to take the name of the directory from a for loop as follows-
if ($line[0] eq "directories")
{ my $aa;
my $siz_two_digit;
my $sub_dir;
my $i;
my $array_size=@line;
for($i=1; $i < $array_size; )
{
$sub_dir=$line[$i];
print $sub_dir;
print "\n";
print $path;
print "\n";
my $r1 = File::Find::Rule->directory
->name('$sub_dir')
->prune # don't go into it
->discard; # don't report it
my $fn = File::Find::Rule->file
->size( $size_req );
my @files = File::Find::Rule->or( $r1, $fn )
->in( $path);
print @files;
undef @files;
print @files;
$i++;
print "\n";
print "\n";
}
}
the $sub_dir value is not appearing properly and that's why the for loop not discarding the desired directories. if anyone knows what;s wrong?
|

July 3rd, 2012, 04:48 PM
|
|
|
|
The single quotes around the var is preventing interpolation.
change:
->name('$sub_dir')
to:
->name( $sub_dir )
|

July 5th, 2012, 07:12 AM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 6
Time spent in forums: 1 h 17 m 24 sec
Reputation Power: 0
|
|
|
Thanks for the reply FishMonger. However, it does not print the desired output. If I hardcode the value of $sub_dir, I get right results.
I have another puzzle in this code. Do you know if I can enter an entrire array starting from position 1 to its last element-something like this-
my $r1 = File::Find::Rule->directory
->name($line[1] to $line[$array_size-1])
->prune # don't go into it
->discard; # don't report it
I do not want to use its 0th element.
I am trying to discard subdirectories which are greater than 3 KB in size and print them. The problem here is that when a for loop looks at one subdirectory it prints the next one which is the next value in an array to be discarded.
Thanks again for any pointers.
|

July 5th, 2012, 08:20 AM
|
|
|
Quote: | Thanks for the reply FishMonger. However, it does not print the desired output. If I hardcode the value of $sub_dir, I get right results. | Then either $sub_dir doesn't hold the value you think it holds, or you're still using single quotes around it.
Quote: | Do you know if I can enter an entrire array starting from position 1 to its last element | What did the module documentation have to say about that question? Or have you not read the documentation?
File::Find::Rule
Code:
Matching Rules
name( @patterns )
Specifies names that should match. May be globs or regular expressions.
$set->name( '*.mp3', '*.ogg' ); # mp3s or oggs
$set->name( qr/\.(mp3|ogg)$/ ); # the same as a regex
$set->name( 'foo.bar' ); # just things named foo.bar
|

July 6th, 2012, 07:55 AM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 6
Time spent in forums: 1 h 17 m 24 sec
Reputation Power: 0
|
|
|
Hello FishMonger,
Thanks for your help.
1. That needed a chomp, it was taking a blank after the value for some reason
2. $set->name( '*.mp3', '*.ogg' ); # mp3s or oggs
$set->name( qr/\.(mp3|ogg)$/ ); # the same as a regex
$set->name( 'foo.bar' ); # just things named foo.bar
Option 1- The file type will vary, so this is out of question
Option 3- not valid here
Option 2- I am still looking at this, not able to find something that fits an array.
|

July 6th, 2012, 08:18 AM
|
|
|
|
Based on the perldoc, have you tried the most obvious answer?
->name(@line[1..$#line])
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|