Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 25th, 2012, 12:31 PM
armbsu armbsu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 6 armbsu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old June 26th, 2012, 06:00 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 190 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 50 m 57 sec
Reputation Power: 41
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";
  }

Reply With Quote
  #3  
Old June 27th, 2012, 02:45 PM
armbsu armbsu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 6 armbsu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old July 3rd, 2012, 03:58 PM
armbsu armbsu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 6 armbsu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #5  
Old July 3rd, 2012, 04:48 PM
FishMonger FishMonger is online now
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,647 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 53 m 20 sec
Reputation Power: 1170
The single quotes around the var is preventing interpolation.

change:
->name('$sub_dir')

to:
->name( $sub_dir )

Reply With Quote
  #6  
Old July 5th, 2012, 07:12 AM
armbsu armbsu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 6 armbsu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old July 5th, 2012, 08:20 AM
FishMonger FishMonger is online now
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,647 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 53 m 20 sec
Reputation Power: 1170
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

Reply With Quote
  #8  
Old July 6th, 2012, 07:55 AM
armbsu armbsu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 6 armbsu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #9  
Old July 6th, 2012, 08:18 AM
FishMonger FishMonger is online now
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,647 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 53 m 20 sec
Reputation Power: 1170
Based on the perldoc, have you tried the most obvious answer?

->name(@line[1..$#line])

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Code not discarding the sub directory

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap