Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am using the below perl script to recursively read directories and sub directories and find out files which are of size zero and generate a log file for them. But instead of simply generating a log file I want to create e directories which it is reading and generate the log file in them. For example My script is reading the directory structure as
Directory1 -> Sub directory1 ->Folder1 -> files " " ->Folder2 -> files " " ->Folder3 -> files

Directory2 -> Sub directory2 ->Folder1 ->files " " ->Folder2 ->files

#!/usr/local/bin/perl use File::Find ; use File::Basename; $search = shift || 'c:\Documents and Settings\user\Desktop\newbatch2' +; $outfile = 'c:\Documents and Settings\user\Desktop\newbatch\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 ) { print " $dir\$file\n"; if ( -f "$dir/$file") { $filesize = -s "$dir/$file" ; if ( $file ne "empty.txt" && $file ne "sample.txt" ) { print OUTF "Warning: $dir/$file has size ZERO\n" if ( $filesize == "0" ) ; } } } closedir( $dh ) ; } close OUTF ; exit 0;
Thanks,

Replies are listed 'Best First'.
Re: Perl Log File Help
by Anonymous Monk on Dec 14, 2010 at 05:01 UTC
    It's very cold, and my head hurts, so I can't quite parse the purpose of the program. But I can help you make it much simpler and shorter, and more correct (with strictures).

    To follow your general program logic, but with the help of File::Find::Rule and File::DosGlob, the latter of which is a core module:

    #!perl use strict; use warnings use autodie; # errors with open, chdir, etc are now fatal use File::Find::Rule; use File::DosGlob 'glob'; my $search = shift || 'c:/Documents and Settings/user/Desktop/newbatch +2' ; my $outfile = 'c:/Documents and Settings/user/Desktop/newbatch/log.txt +'; open my $outf, '>', $outfile; for my $dir (File::Find::Rule->dir->in($search)) { for my $file ( <*>) { my $path = "$dir\\$_"; print " $path\n"; if (-f $path) { if ($file ne 'empty.txt' and $file ne 'sample.txt') { unless (-s $path) { print $outf "Warning: $path has size ZERO\n"; } } } } }

    OR, to be more concise:

    #!perl use strict; use warnings; use autodie; use File::Find::Rule; use File::DosGlob 'glob'; my ($search) = @ARGV or die "need a directory to search"; sub found { my ($name, $path, $full) = @_; warn "Warning: $full has size ZERO" # outputs to STDERR unless $name ~~ ['empty.txt', 'sample.txt']; # Perl v5.10+ # unless $name eq 'empty.txt' or $name eq 'sample.txt'; } File::Find::Rule->file->empty->exec(\&found)->in($search);
Re: Perl Log File Help
by Marshall (Canon) on Dec 14, 2010 at 04:54 UTC
    I seem to remember seeing this question or something very, very similar to it in the recent past. I can't be 100% sure, but if you are the same anon Monk, then please pay attention when you get replies.

    1)Always use strict; and use warnings;. Even on Windows, #!/usr/bin/perl -w is equivalent to use warnings; although the path to Perl is ignored.
    2)File::Find will visit all files and directories underneath the starting directory. There is no need to save the directories and then re-open them only to read the files names yet again. All of those file names and the directory have already been visited!
    3)If what you need to do to the file is not complicated, do it right in the File::Find subroutine - No need to save it's path and come back.
    4) Please pay more attention to the indenting of your code - it is important.
    5) I think the code below is essentially equivalent to what your code does, reports zero length files that are found.
    6) I was not able to understand your question about what you want to do other than this. "But instead of simply generating a log file..." Please try a different explanation.

    #!/usr/bin/perl -w use strict; use File::Find; use File::Basename; my $start_dir = "C:/temp"; my $log_file = 'C:/temp/logfile.txt'; open (my $log, '>', $log_file) or die "cannot open $log_file for write +"; find (\&process_file, $start_dir); sub process_file { return unless -f; # do not process directories, # only "real files" my $full_path_name = $File::Find::name; print "$full_path_name\n"; #other ways of writing the "if", but extending your code if ( -s == 0 and ($_ ne 'empty.txt') and ($_ ne 'sample.txt') and ($full_path_name ne $log_file) ) { print $log "Warning: $full_path_name has size ZERO\n"; } }