LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-17-2010, 11:59 AM   #16
user_28
LQ Newbie
 
Registered: Aug 2010
Posts: 27

Original Poster
Rep: Reputation: 0

Hi,
Sorry for that. Earlier I modified my code according to the hint like this.

#!/usr/local/bin/perl

use File::Find ;


$search = shift || 'C:\Documents and Settings\user\Desktop' ;

# Get an array of all subdirectories
find sub { push @dirs, $File::Find::name if -d }, $search ;

for $dir ( @dirs ) {
opendir $dh, $dir or do { warn "Cannot open '$dir' $!" ; next ; } ;

opendir( DIR, "$dir" ) ;
@files = grep( /\./, readdir( DIR ) ) ;
closedir( DIR ) ;

foreach $file ( @files ) {

my $filesize = -s'$dir/$file';
print "$filesize\n"

}


}
 
Old 08-17-2010, 12:09 PM   #17
user_28
LQ Newbie
 
Registered: Aug 2010
Posts: 27

Original Poster
Rep: Reputation: 0
Hi,
Sorry. Below is the script which I made changes according to the hint
#!/usr/local/bin/perl

use File::Find ;

# Get the directory from the command line
# or use the default directory
$search = shift || 'p:\Documents and Settings\user\Desktop\' ;


find sub { push @dirs, $File::Find::name if -d }, $search ;

for $dir ( @dirs ) {
opendir $dh, $dir or do { warn "Cannot open '$dir' $!" ; next ; } ;

opendir( DIR, "$dir" ) ;
@files = grep( /\./, readdir( DIR ) ) ;
closedir( DIR ) ;

foreach $file ( @files ) {
my $filesize = -s'$dir/$file\n";
print -s '$filesize\n' ;


}


}

Thanks
 
Old 08-17-2010, 12:11 PM   #18
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Compare your code with the working version I posted in post #15.

All your wishes (up till now) are implemented:
- Traverses a directory structure (base can be set on the command line, otherwise a default is set),
- Looks for files with 0 length and print message,
- Excludes 2 files, which are always zero length (foor and bar in example given).
 
Old 08-17-2010, 12:22 PM   #19
user_28
LQ Newbie
 
Registered: Aug 2010
Posts: 27

Original Poster
Rep: Reputation: 0
Hi,
Thank you so much. You have so helpful for my questions. From your scripts I learned a lot.
 
Old 08-17-2010, 02:22 PM   #20
user_28
LQ Newbie
 
Registered: Aug 2010
Posts: 27

Original Poster
Rep: Reputation: 0
Hi,
After I made changes according to the script given by druuna. I further added some code to the script like creating a log file and to discard hidden folders like .,... But the problem is it is listing out folders as zero size. I double checked and my folders do have some files in them . Not sure what went wrong. please help.

#!/usr/local/bin/perl
use File::Find ;

$search = shift || 'p:\Documents and Settings\user\Desktop\' ;

find sub { push @dirs, $File::Find::name if -d }, $search ;

for $dir ( @dirs ) {
opendir $dh, $dir or do { warn "Cannot open '$dir' $!" ; next ; } ;

opendir( DIR, "$dir" ) ;
@files = grep( !/^\.\.?$/, readdir( DIR ) ) ;

closedir( DIR ) ;

foreach $file ( @files ) {
my $filesize = -s "$dir/$file" ;
if ( $file ne "foo" && $file ne "bar" ) {
print "Warning: $dir/$file has size $filesize\n"
if ( $filesize == "0" ) ;

}
}
closedir( $dh ) ;

open LOG,">>log.txt";
print LOG "Warning: $file has size ZERO\n";

close LOG;
}
 
Old 08-17-2010, 03:14 PM   #21
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Have a look at this.

Code:
#!/usr/bin/perl

use File::Find ;

$search = shift || '/data/Downloads' ;
$outfile = 'log.txt' ;

open OUTF, ">$outfile" or die print "Can't create logfile: $!" ;

find sub { push @dirs, $File::Find::name if -d }, $search ;

for $dir ( @dirs ) {
   opendir $dh, $dir or do { warn "Cannot open '$dir' $!" ; next ; } ;

   opendir( DIR, "$dir" ) ;
   @files = readdir( DIR ) ;
   closedir( DIR ) ;

   foreach $file ( @files ) {
      if ( -f "$dir/$file" ) {
         $filesize = -s "$dir/$file" ;
         if ( $file ne "foo" && $file ne "bar" ) {
            print OUTF "Warning: $dir/$file has size ZERO\n"
              if ( $filesize == "0" ) ;
         }
      }
   }
   closedir( $dh ) ;
}

close OUTF ;

exit 0 ;
BTW: I'm not seeing the 3 files in the code you posted that are always zero (mentioned in posts #1, #3 and #12).
 
1 members found this post helpful.
Old 08-17-2010, 03:35 PM   #22
user_28
LQ Newbie
 
Registered: Aug 2010
Posts: 27

Original Poster
Rep: Reputation: 0
Hi,
Thank you. It is working now and regarding the file names that are always zero. I am not still working on real data so I created two empty files and gave their names in place of foor and bar and the script is working fine now. Really thanks a lot!
 
Old 08-18-2010, 04:13 AM   #23
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@caseycolin: ????

What is the link about: Spam!

Reported!
Edit: Moderator cleaned spam message.

Last edited by druuna; 08-18-2010 at 06:36 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with perl modules declaration and perl/cgi script shifter Programming 9 02-24-2010 10:09 AM
call perl script through another perl script with arguments nanda22 Linux - Newbie 21 07-21-2009 01:18 AM
NEED HELP IN comment lines PERL Perl script adam_blackice Programming 17 11-07-2007 09:01 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 11:22 AM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 06:03 AM


All times are GMT -5. The time now is 08:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration