LinuxQuestions.org
Review your favorite Linux distribution.
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-16-2010, 01:29 PM   #1
user_28
LQ Newbie
 
Registered: Aug 2010
Posts: 27

Rep: Reputation: 0
Question 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
 
Old 08-16-2010, 02:04 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,960

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by user_28 View Post
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.
 
Old 08-16-2010, 02:51 PM   #3
user_28
LQ Newbie
 
Registered: Aug 2010
Posts: 27

Original Poster
Rep: Reputation: 0
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
 
Old 08-16-2010, 03:14 PM   #4
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
@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.
 
Old 08-16-2010, 03:34 PM   #5
user_28
LQ Newbie
 
Registered: Aug 2010
Posts: 27

Original Poster
Rep: Reputation: 0
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,
 
Old 08-16-2010, 03:45 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,960

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by user_28 View Post
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???
 
Old 08-16-2010, 03:53 PM   #7
user_28
LQ Newbie
 
Registered: Aug 2010
Posts: 27

Original Poster
Rep: Reputation: 0
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,
 
Old 08-16-2010, 03:54 PM   #8
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,

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.
 
Old 08-16-2010, 05:18 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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/
 
Old 08-17-2010, 09:23 AM   #10
user_28
LQ Newbie
 
Registered: Aug 2010
Posts: 27

Original Poster
Rep: Reputation: 0
Hi,
We both are not the same users. It was a small confusion and explained it to moderators and they again reopened this thread.
 
Old 08-17-2010, 11:03 AM   #11
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,

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.
 
Old 08-17-2010, 11:15 AM   #12
user_28
LQ Newbie
 
Registered: Aug 2010
Posts: 27

Original Poster
Rep: Reputation: 0
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
 
Old 08-17-2010, 11:21 AM   #13
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,

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.
 
Old 08-17-2010, 11:33 AM   #14
user_28
LQ Newbie
 
Registered: Aug 2010
Posts: 27

Original Poster
Rep: Reputation: 0
Hi,
I tried the hint from post #4 but the output is a huge space with no file names.
 
Old 08-17-2010, 11:49 AM   #15
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,

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 ) ;
}
 
1 members found this post helpful.
  


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 01:03 PM.

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