http://www.perlmonks.org?node_id=1052261

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

I'm new in perl trying to create a simple script. my goal is to create script to use for Nagios. trying to create a script that is count file in a specifict directory and print it out. Script working and printing out a number, only problemes i have, is in my folder i have 2 files but when script print out result i get that is 4 file in directory. What is problems in my script? you see the script here under.

use strict; use warnings; my $directory = 'My directory'; my $file_count=0; opendir (DIR, $directory) or die $!; while (my $file = readdir(DIR)) { if( $file){ $file_count++; } } closedir(DIR); print "Files: $file_count\n";
Best regard Joakim

Replies are listed 'Best First'.
Re: count file in directory
by hdb (Monsignor) on Sep 04, 2013 at 09:06 UTC

    If you just print the filenames like print "$file\n", you will see all four files!

      Yes. Im trying this perl script in windows to a directory there. when i looking in the folder only 2 file and i have put on "show all hidden files". so question is if script add 2 extra count's? if i change folder to a folder that have 12 files and 2 directory script is show 16.
        In this post you clearly have not done what was asked of you. Downvoted for that reason.

        When the program does not give you what you expect, it is certainly not the computer which is wrong, but rather, your expectations.

        If your expectations are incorrect, it is likely due to some preconceived notion which is incorrect -- so you can't use logic to get out of the trap, since you'll continue to base your logic on that same faulty assumption.

        Instead, what you need is hard, unbiased data.

        So -- you asked for help. You were advised to add a print statement. Your response was to theorize on what the print statement might accomplish.

        . . . pause . . .

        Is there something useful you were hoping to accomplish by not following the help you were given?

        :-)

        Have you really added the line I proposed to your script? Rather than trying to find hidden files or such?

Re: count file in directory
by kcott (Archbishop) on Sep 04, 2013 at 09:35 UTC

    G'day joaase,

    Welcome to the monastery.

    In addition to the two files you say you have, you've also got '.' (current directory) and '..' (parent directory). That makes four entries in total.

    You can check this by printing the directory entries. Something like (code layout tidied):

    .... while (my $file = readdir(DIR)) { print "$file\n"; if ($file) { ...

    The readdir documentation has example code for excluding '.' and '..'.

    Unrelated to your posted issue, I see another problem with:

    ... while (my $file = readdir(DIR)) { if ($file) { $file_count++; } } ...

    "if ($file) {" will always be TRUE: you'll never get to that condition if "my $file = readdir(DIR)" is FALSE. As currently written, that test is completely redundant and should be removed (i.e. replace the whole if block with just "++$file_count;".

    Perhaps you wanted a file test operator in the if condition. For instance, "if (-f $file) {" would check for plain files: '.', '..' and any other directories would not be included in the count; this would resolve your original problem.

    -- Ken

Re: count file in directory
by johngg (Canon) on Sep 04, 2013 at 09:42 UTC

    If you follow hdb's advice you will see that you have two files and two entries called '.' and '..' which are links to the current and parent directories respectively. All directories contain those two entries so you will always see two more items than you might expect.

    Cheers,

    JohnGG

      Thank JohnGG. That explain why i see 4 count insted of 2. /Joakim
Re: count file in directory
by Anonymous Monk on Sep 04, 2013 at 09:39 UTC

    When I run your script in linux it correctly gives me the number of files in the directory: hidden+visible. are there hidden files such as . (current directory) and .. in windows? My guess would be there are hidden files you are missing