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"; } }
In reply to Re: Perl Log File Help
by Marshall
in thread Perl Log File Help
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |