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 |
Quote:
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. |
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 |
@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. |
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, |
Quote:
|
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, |
Hi,
The code you posted in #3 doesn't work. This is not correct: grep( /\/, readdir( DIR ). Have a look here: perl grep Quote:
For you to figure out how to implement the message that needs to be printed. Hope this helps. |
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/ |
Hi,
We both are not the same users. It was a small confusion and explained it to moderators and they again reopened this thread. |
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. |
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 |
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. |
Hi,
I tried the hint from post #4 but the output is a huge space with no file names. |
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 |
All times are GMT -5. The time now is 08:49 AM. |