LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help regarding a Perl script (https://www.linuxquestions.org/questions/linux-newbie-8/help-regarding-a-perl-script-826572/)

user_28 08-16-2010 12:29 PM

Help regarding a Perl script
 
Hi,
I am new to Perl scripting. I need some ideas regarding a requirement.
That is I need to read file names and their size from folders and sub folders and if the size of the file is "0/ZERO" print out an error message saying "file size is zero with their file names".

Except I constantly have three files which are always zero so it should avoid these files and check the rest of them.

Thanks

TB0ne 08-16-2010 01:04 PM

Quote:

Originally Posted by user_28 (Post 4067955)
Hi,
I am new to Perl scripting. I need some ideas regarding a requirement.
That is I need to read file names and their size from folders and sub folders and if the size of the file is "0/ZERO" print out an error message saying "file size is zero with their file names".

Except I constantly have three files which are always zero so it should avoid these files and check the rest of them.

Thanks

This sounds very much like homework.

And we'll be glad to HELP you with this...but we're not going to write it for you. Post what you've written, and where you're getting stuck. Otherwise, there are many Perl tutorials you can find via Google, that can tell you how to check file sizes.

user_28 08-16-2010 01:51 PM

Hi,
Sure I am posting the script I am working on right now . I am reading the file names recursively. Next step would be to get the file size and if it is zero print an error message. I think we can use file size = -s "file" but not sure how to use it correctly and also I need to avoid three files from my directory reading because their size is by default zero.

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

$search = shift || 'p::\ documents\new folder' ;

# 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 ) {
print "$dir/$file\n" ;


}
}

Thanks

druuna 08-16-2010 02:14 PM

@TB0ne: Sure looks like traversing a directory is the question of the day. I just answered a very similar question: Problem with a Perl script.

The code in post #3 looks very much like the one I posted in post #14 in the above posted thread :)

@user_28: Hint:
my $filesize = -s 'filename' ;
print $filesize . "\n" if ( $filesize != "0" ) ;


Hope this helps.

user_28 08-16-2010 02:34 PM

Hi,
The above code is a patch work from here and there as I am new to Perl but need to complete a task immediately. I tired the code above by declaring $file after s because it is the variable which is holding my file names but no luck the size is not being retrieved.

Thanks,

TB0ne 08-16-2010 02:45 PM

Quote:

Originally Posted by user_28 (Post 4068070)
Hi,
The above code is a patch work from here and there as I am new to Perl but need to complete a task immediately. I tired the code above by declaring $file after s because it is the variable which is holding my file names but no luck the size is not being retrieved.

Thanks,

So how about posting the code that's not working, and what it's returning? Or do you expect us to fully test and debug YOUR program for you???

user_28 08-16-2010 02:53 PM

Hi,
Sorry I did not mean that way. What I said was the script to traverse is working, in that script I added the code which was posted by "druuna" by making changes accordingly and it did not work. So I requested for help thinking I am not using the code exactly.

Thanks,

druuna 08-16-2010 02:54 PM

Hi,

The code you posted in #3 doesn't work. This is not correct: grep( /\/, readdir( DIR ). Have a look here: perl grep

Quote:

I tired the code above by declaring $file after s because it is the variable which is holding my file names
That's only half correct. It only holds the file name, not the directory structure, that is stored in $dir.

For you to figure out how to implement the message that needs to be printed.

Hope this helps.

Tinkster 08-16-2010 04:18 PM

Please post your thread in only one forum. Posting a single thread
in the most relevant forum will make it easier for members to help
you and will keep the discussion in one place. This thread is being
closed because it is a duplicate.

It would be much more acceptable (and honest) if you didn't use
two user accounts, either. I respectfully ask that you cease
using two accounts to saturate the forums with duplicate threads,
whatever the motivation behind it is.




http://www.linuxquestions.org/questi...script-826534/

user_28 08-17-2010 08:23 AM

Hi,
We both are not the same users. It was a small confusion and explained it to moderators and they again reopened this thread.

druuna 08-17-2010 10:03 AM

Hi,

The thread is re-opened, but you do not mention if you fixed your problem(s).

If not: Post what you have this far and tell us what the problem might be.

user_28 08-17-2010 10:15 AM

Hi,
Below is the script till now. To get the file size I declared -s and declaring my variable after that which holds my location and file names at the print statement, but no output is being generated. Next thing is I have three basic files which will always be zero so it has to check the rest of the files leaving this three for file size.

#!/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 ) {
print -s '$dir/$file\n' ;


}


}

Thanks

druuna 08-17-2010 10:21 AM

Hi,

Have another look at the hint I gave in post #4. The only -s I see is in this statement: print -s '$dir/$file\n' ;, which doesn't work.

Hope this helps.

user_28 08-17-2010 10:33 AM

Hi,
I tried the hint from post #4 but the output is a huge space with no file names.

druuna 08-17-2010 10:49 AM

Hi,

You are still not posting what you actually tried! How do we know what you did/didn't do correct without it?

Ok, enough with all this. Here you are:
Code:

#!/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 ) ;
}



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