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

Hi,

Can someone help me with this perl script. Currently my perl script is reading files recursively and checking if there is any file with size zero.If so it generates a log file. But instead of just generating a text file as log file, it should first create a directory and the name of the directory should be the one it is reading files from and then generate a log file in that directory. My directory structure: Main Directory/ sub directory/ folder 1 , folder 2... So it should create a directory as "sub directory/log.txt"

#!/usr/local/bin/perl #use strict; #use warnings; use File::Find ; $search = shift || 'c:\Documents and Settings\user\Desktop\batch' ; $outfile = 'C:\Documents and Settings\user\Desktop\output'; open OUTF, ">> $outfile/log.txt" 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 ) { 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;

Replies are listed 'Best First'.
Re: Working with the log files
by roboticus (Chancellor) on Sep 20, 2010 at 19:44 UTC
      Hi,

      My problem is I need to create directory where the name of the directory should be name of the current directory it is reading files from. So I am stuck with how to do this. I know I could use mkdir but not sure how to get the dir name:

      Directory structure: Main directory/sub directory/ folder 1 , folder 2... After reading the files from sub directory it should generate a output as sub directory/log.txt right now my script is creating only log.txt.

        So, are you saying you need to get the name of the Current Working Directory (cwd)?

        Or do you just need to remember which directories you have moved through, in which case a simple array used as a stack would do?

Re: Working with the log files
by ww (Archbishop) on Sep 21, 2010 at 00:49 UTC
    So you think you want something like this?
    
    |-DIR "CWD"\
    |           |testedfile1
    |           |testedfile2
    |           |testedfile3
    |           | ....
    |           |-SUBDIR "CWD"\ 
    |                          |Logfile
    |
    |-ANOTHERDIR (which will be CWD soon?)
    |           |files...
    |           |-SUBDIR "ANOTHERDIR"\
    |                                 |Logfile
    |
    ...etc. ad nauseum
    
    
    ...where "CWD" (and "ANOTHERDIR" and each of the rest) might be ~home/lib/foo or D:\data\perl_tests ?

    And then you're going to find those logfiles by search for cases where a dir has a subdir of the same name which contains files named.....?

    Sounds as though you might profit from reviewing your plan.

Re: Working with the log files
by umasuresh (Hermit) on Sep 20, 2010 at 22:16 UTC
      It's good that you point the OP towards documentation, but please don't point him towards ancient documentation. Had you used [doc://File::Find] you would have a nice link to this, on http://perldoc.perl.org.
Re: Working with the log files
by Anonymous Monk on Sep 20, 2010 at 22:50 UTC
    Hi,

    Have a look at the Cwd package for cwd() and
    File::Path for mkpath().

    J.C.