in reply to Re: log file in perl.
in thread log file in perl.

Hi,

Thank you and its working now. If possible can I know how to tweak my script to get filenames, date and time in addition to counts.

#!/usr/local/bin/perl my $dir= ('c:\My Projects\Perl Scripts\New Folder') ; $directory_count = 0; $file_count=0; $outfile = 'log.txt'; open my $OUTF, "> $outfile" or die print " can't create logfile; $!"; opendir( DIR, "$dir" ) ; my @files = readdir( DIR ) ; foreach $file ( @files ) { if ( -f "$dir/$file") { $directory_count++; } else { $file_count++; } } print {$OUTF) "Directories: $directory_count |"; print ($OUTF) "Files: $file_count\n"; closedir( DIR ) ; } close OUTF ;

Replies are listed 'Best First'.
Re^3: log file in perl.
by kennethk (Abbot) on Aug 27, 2010 at 21:00 UTC
    What's stopping you? The answer to your question lies in you trying to understand the code you have. You already have the file name in your $file variable. You can get information on files using the -X family of operators, but you should know that already since your code contains -f. stat might also be helpful.

    I find it odd that you say it's working now, as I took your posted code, attempted to run it, and obtained the errors:

    String found where operator expected at fluff.pl line 35, near ") "Dir +ectories: $directory_count |"" (Missing operator before "Directories: $directory_count |"?) print (...) interpreted as function at fluff.pl line 36. String found where operator expected at fluff.pl line 36, near ") "Fil +es: $file_count\n"" (Missing operator before "Files: $file_count\n"?) syntax error at fluff.pl line 35, near "$OUTF) " syntax error at fluff.pl line 36, near ") "Files: $file_count\n"" Execution of fluff.pl aborted due to compilation errors.

    I can also spot a number of other issues that would be caught by strict and warnings, as you claim to have implemented Re^2: Perl Script to count files and directories is not working.. As an internet programming forum, PerlMonks is ridiculously respectful and helpful to the neophyte. We do not have patience for people who show no effort. I highly recommend you read How To Ask Questions The Smart Way and meditate on it before posting again.

      Hi,

      I am sorry I did not mean to say its working for a wrong code. It was pasted from a wrong file which was not working earlier.This happened in a rush. I am pasting the code which is working fine now for me and I am also tweaking it as per the suggestions given stat and -x.

      #!/usr/local/bin/perl use warnings; use strict; my $dir= 'c:\My documents\perl scripts\new folder'; my $directory_count=0; my $file_count=0; my $outfile = 'log.txt'; open my $OUTF, '>', $outfile or die "can't create logfile;$!"; opendir (DIR, $dir); my @files = readdir(DIR); foreach my $files(@files) { print {$OUTF}"$files |";} foreach my $file(@files) { if (-d "$dir/$file") { $directory_count++; } else { $file_count++; } } print { $OUTF} "$directory_count |"; #print { $OUTF} "$file_count \n";
Re^3: log file in perl.
by vek (Prior) on Aug 27, 2010 at 20:57 UTC

    You've already got the filenames in your foreach loop. Each iteration of $file has the filename.

    For dates/times of files, have a peek at stat