count number of files in a folder with perl












2















I would like to count the number of files inside a folder with perl. With the following code I can list them, but i don't know how to count them in perl. Any help?



$dir = "/home/Enric/gfs-0.5.2016061400";
opendir(DIR, "$dir");
@FILES = grep { /gfs./ } readdir(DIR);
foreach $file (@FILES) {
print $file, "n";
}
closedir(DIR);









share|improve this question


















  • 2





    As always with perl add use strict; use warnings; to the top. $#FILES is one way to have the count of files but as arrays start at 0 you need to add 1 to it

    – KeepCalmAndCarryOn
    Jun 14 '16 at 20:47








  • 1





    Possible duplicate of Find size of an array in Perl

    – Matt Jacob
    Jun 14 '16 at 21:25






  • 3





    Did you write this code yourself, or did you copy it from somewhere else? The reason I ask is because there are many things wrong with it, and if you copied it from a tutorials site or something, I would recommend avoiding that site in the future.

    – Matt Jacob
    Jun 14 '16 at 21:29











  • @FILES = grep { /gfs./ } readdir(DIR); print join "n", scalar(@FILES);

    – ssr1012
    Jun 15 '16 at 11:07








  • 1





    @KeepCalmAndCarryOn: Using $#array for anything other than getting the last index in @array is complicating things unnecessarily. Why not just use @array in scalar context?

    – Dave Cross
    Jun 15 '16 at 12:57
















2















I would like to count the number of files inside a folder with perl. With the following code I can list them, but i don't know how to count them in perl. Any help?



$dir = "/home/Enric/gfs-0.5.2016061400";
opendir(DIR, "$dir");
@FILES = grep { /gfs./ } readdir(DIR);
foreach $file (@FILES) {
print $file, "n";
}
closedir(DIR);









share|improve this question


















  • 2





    As always with perl add use strict; use warnings; to the top. $#FILES is one way to have the count of files but as arrays start at 0 you need to add 1 to it

    – KeepCalmAndCarryOn
    Jun 14 '16 at 20:47








  • 1





    Possible duplicate of Find size of an array in Perl

    – Matt Jacob
    Jun 14 '16 at 21:25






  • 3





    Did you write this code yourself, or did you copy it from somewhere else? The reason I ask is because there are many things wrong with it, and if you copied it from a tutorials site or something, I would recommend avoiding that site in the future.

    – Matt Jacob
    Jun 14 '16 at 21:29











  • @FILES = grep { /gfs./ } readdir(DIR); print join "n", scalar(@FILES);

    – ssr1012
    Jun 15 '16 at 11:07








  • 1





    @KeepCalmAndCarryOn: Using $#array for anything other than getting the last index in @array is complicating things unnecessarily. Why not just use @array in scalar context?

    – Dave Cross
    Jun 15 '16 at 12:57














2












2








2








I would like to count the number of files inside a folder with perl. With the following code I can list them, but i don't know how to count them in perl. Any help?



$dir = "/home/Enric/gfs-0.5.2016061400";
opendir(DIR, "$dir");
@FILES = grep { /gfs./ } readdir(DIR);
foreach $file (@FILES) {
print $file, "n";
}
closedir(DIR);









share|improve this question














I would like to count the number of files inside a folder with perl. With the following code I can list them, but i don't know how to count them in perl. Any help?



$dir = "/home/Enric/gfs-0.5.2016061400";
opendir(DIR, "$dir");
@FILES = grep { /gfs./ } readdir(DIR);
foreach $file (@FILES) {
print $file, "n";
}
closedir(DIR);






perl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 14 '16 at 20:32









Enric Agud PiqueEnric Agud Pique

4523923




4523923








  • 2





    As always with perl add use strict; use warnings; to the top. $#FILES is one way to have the count of files but as arrays start at 0 you need to add 1 to it

    – KeepCalmAndCarryOn
    Jun 14 '16 at 20:47








  • 1





    Possible duplicate of Find size of an array in Perl

    – Matt Jacob
    Jun 14 '16 at 21:25






  • 3





    Did you write this code yourself, or did you copy it from somewhere else? The reason I ask is because there are many things wrong with it, and if you copied it from a tutorials site or something, I would recommend avoiding that site in the future.

    – Matt Jacob
    Jun 14 '16 at 21:29











  • @FILES = grep { /gfs./ } readdir(DIR); print join "n", scalar(@FILES);

    – ssr1012
    Jun 15 '16 at 11:07








  • 1





    @KeepCalmAndCarryOn: Using $#array for anything other than getting the last index in @array is complicating things unnecessarily. Why not just use @array in scalar context?

    – Dave Cross
    Jun 15 '16 at 12:57














  • 2





    As always with perl add use strict; use warnings; to the top. $#FILES is one way to have the count of files but as arrays start at 0 you need to add 1 to it

    – KeepCalmAndCarryOn
    Jun 14 '16 at 20:47








  • 1





    Possible duplicate of Find size of an array in Perl

    – Matt Jacob
    Jun 14 '16 at 21:25






  • 3





    Did you write this code yourself, or did you copy it from somewhere else? The reason I ask is because there are many things wrong with it, and if you copied it from a tutorials site or something, I would recommend avoiding that site in the future.

    – Matt Jacob
    Jun 14 '16 at 21:29











  • @FILES = grep { /gfs./ } readdir(DIR); print join "n", scalar(@FILES);

    – ssr1012
    Jun 15 '16 at 11:07








  • 1





    @KeepCalmAndCarryOn: Using $#array for anything other than getting the last index in @array is complicating things unnecessarily. Why not just use @array in scalar context?

    – Dave Cross
    Jun 15 '16 at 12:57








2




2





As always with perl add use strict; use warnings; to the top. $#FILES is one way to have the count of files but as arrays start at 0 you need to add 1 to it

– KeepCalmAndCarryOn
Jun 14 '16 at 20:47







As always with perl add use strict; use warnings; to the top. $#FILES is one way to have the count of files but as arrays start at 0 you need to add 1 to it

– KeepCalmAndCarryOn
Jun 14 '16 at 20:47






1




1





Possible duplicate of Find size of an array in Perl

– Matt Jacob
Jun 14 '16 at 21:25





Possible duplicate of Find size of an array in Perl

– Matt Jacob
Jun 14 '16 at 21:25




3




3





Did you write this code yourself, or did you copy it from somewhere else? The reason I ask is because there are many things wrong with it, and if you copied it from a tutorials site or something, I would recommend avoiding that site in the future.

– Matt Jacob
Jun 14 '16 at 21:29





Did you write this code yourself, or did you copy it from somewhere else? The reason I ask is because there are many things wrong with it, and if you copied it from a tutorials site or something, I would recommend avoiding that site in the future.

– Matt Jacob
Jun 14 '16 at 21:29













@FILES = grep { /gfs./ } readdir(DIR); print join "n", scalar(@FILES);

– ssr1012
Jun 15 '16 at 11:07







@FILES = grep { /gfs./ } readdir(DIR); print join "n", scalar(@FILES);

– ssr1012
Jun 15 '16 at 11:07






1




1





@KeepCalmAndCarryOn: Using $#array for anything other than getting the last index in @array is complicating things unnecessarily. Why not just use @array in scalar context?

– Dave Cross
Jun 15 '16 at 12:57





@KeepCalmAndCarryOn: Using $#array for anything other than getting the last index in @array is complicating things unnecessarily. Why not just use @array in scalar context?

– Dave Cross
Jun 15 '16 at 12:57












4 Answers
4






active

oldest

votes


















5














If you want to just count them, once you have a directory open for reading you can manipulate context on readdir so that it returns the list of all entries, and then assign that to a scalar. This gives you the length of the list, ie. the number of elements



opendir my $dh, $dir;
my $num_entries = () = readdir($dh);


The construct = () = goes under the name of goatse operator, see it in perlsecret. There are clearer ways, of course, as below.



If you want to count certain kinds of files, pass the file list through grep first, like you do. Since grep imposes the list context on its input readdir returns the list of all files, and after filtering grep itself returns a list. When you assign that to a scalar you get the length of the list (number of elements), ie. your count. For example, for all regular files and /gfs./ files



use warnings;
use strict;

my $dir = '/home/Enric/gfs-0.5.2016061400';
opendir my $dh, $dir or die "Can't open $dir: $!";

my $num_files = grep { -f "$dir/$_" } readdir($dh);
rewinddir($dh); # so that it can read the dir again
my $num_gfs = grep { /gfs./ } readdir($dh);


(This is only an example, with rewinddir so that it works as it stands. To really get two kinds of files from a directory better read all files into an array and then process that)



Note that readdir returns the bare filename, without any path. So for most of what is normally done with files we need to prepend it with the path (unless you first chdir to that directory). This is what is done in the grep block above so that the -f file test (-X) has the correct filename.



If you need to use the file list itself, get that into an array and then assign it to a scalar



# Get the file list, then its length
my @files_gfs = map { "$dir/$_" } grep { /gfs./ } readdir($dh);
my $num_gfs = @files_gfs;


Here map builds the full path for each file. If you don't need the path drop map { }. Note that there is normally no need for the formal use of scalar on the array to get the count, like



my $num_gfs = scalar @files_gfs;


Simply assign an array to a scalar instead, it's an idiom (to say the least).



If you are processing files as you read, count as you go



my $cnt_gfs = 0;
while (my $filename = readdir($dh)) {
$cnt_gfs++ if $filename =~ /gfs./;
# Process $dir/$filename as needed
}


Here readdir is in the scalar context (since its output is assigned to a scalar), and it iterates through the directory entries, returning one at a time.






share|improve this answer

































    4














    You have the list of files in @FILES. So your question becomes "how do I get the length of an array?" And that's simple, you simply evaluate the array in scalar context.



    my $number_of_files = @FILES;
    print $number_of_files;


    Or you can eliminate the unnecessary scalar variable by using the scalar() function.



    print scalar @FILES;





    share|improve this answer
























    • Thank you for the comments @Dave Cross

      – Enric Agud Pique
      Jun 15 '16 at 15:38



















    1














    Try this code for starters (this is on Windows and will include . , .. and folders. Those can be filtered out if you want only files):



    #!/usr/bin/perl -w

    my $dirname = "C:/Perl_Code";
    my $filecnt = 0;

    opendir (DIR, $dirname) || die "Error while opening dir $dirname: $!n";
    while(my $filename = readdir(DIR)){
    print("$filenamen");
    $filecnt++;
    }
    closedir(DIR);
    print "Files in $dirname : $filecntn";
    exit;





    share|improve this answer































      0














      I know this isn't in Perl, but if you ever need a quick way, just type this into bash command line:



       ls -1 | wc -l 


      ls -1 gives you a list of the files in the directory, and wc -l gives you the line count. Combined, they'll give you the number of files in your directory.



      Alternatively, you can call bash from Perl (although you probably shouldn't), using



      system("ls -1 | wc -l"); 





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37821571%2fcount-number-of-files-in-a-folder-with-perl%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        5














        If you want to just count them, once you have a directory open for reading you can manipulate context on readdir so that it returns the list of all entries, and then assign that to a scalar. This gives you the length of the list, ie. the number of elements



        opendir my $dh, $dir;
        my $num_entries = () = readdir($dh);


        The construct = () = goes under the name of goatse operator, see it in perlsecret. There are clearer ways, of course, as below.



        If you want to count certain kinds of files, pass the file list through grep first, like you do. Since grep imposes the list context on its input readdir returns the list of all files, and after filtering grep itself returns a list. When you assign that to a scalar you get the length of the list (number of elements), ie. your count. For example, for all regular files and /gfs./ files



        use warnings;
        use strict;

        my $dir = '/home/Enric/gfs-0.5.2016061400';
        opendir my $dh, $dir or die "Can't open $dir: $!";

        my $num_files = grep { -f "$dir/$_" } readdir($dh);
        rewinddir($dh); # so that it can read the dir again
        my $num_gfs = grep { /gfs./ } readdir($dh);


        (This is only an example, with rewinddir so that it works as it stands. To really get two kinds of files from a directory better read all files into an array and then process that)



        Note that readdir returns the bare filename, without any path. So for most of what is normally done with files we need to prepend it with the path (unless you first chdir to that directory). This is what is done in the grep block above so that the -f file test (-X) has the correct filename.



        If you need to use the file list itself, get that into an array and then assign it to a scalar



        # Get the file list, then its length
        my @files_gfs = map { "$dir/$_" } grep { /gfs./ } readdir($dh);
        my $num_gfs = @files_gfs;


        Here map builds the full path for each file. If you don't need the path drop map { }. Note that there is normally no need for the formal use of scalar on the array to get the count, like



        my $num_gfs = scalar @files_gfs;


        Simply assign an array to a scalar instead, it's an idiom (to say the least).



        If you are processing files as you read, count as you go



        my $cnt_gfs = 0;
        while (my $filename = readdir($dh)) {
        $cnt_gfs++ if $filename =~ /gfs./;
        # Process $dir/$filename as needed
        }


        Here readdir is in the scalar context (since its output is assigned to a scalar), and it iterates through the directory entries, returning one at a time.






        share|improve this answer






























          5














          If you want to just count them, once you have a directory open for reading you can manipulate context on readdir so that it returns the list of all entries, and then assign that to a scalar. This gives you the length of the list, ie. the number of elements



          opendir my $dh, $dir;
          my $num_entries = () = readdir($dh);


          The construct = () = goes under the name of goatse operator, see it in perlsecret. There are clearer ways, of course, as below.



          If you want to count certain kinds of files, pass the file list through grep first, like you do. Since grep imposes the list context on its input readdir returns the list of all files, and after filtering grep itself returns a list. When you assign that to a scalar you get the length of the list (number of elements), ie. your count. For example, for all regular files and /gfs./ files



          use warnings;
          use strict;

          my $dir = '/home/Enric/gfs-0.5.2016061400';
          opendir my $dh, $dir or die "Can't open $dir: $!";

          my $num_files = grep { -f "$dir/$_" } readdir($dh);
          rewinddir($dh); # so that it can read the dir again
          my $num_gfs = grep { /gfs./ } readdir($dh);


          (This is only an example, with rewinddir so that it works as it stands. To really get two kinds of files from a directory better read all files into an array and then process that)



          Note that readdir returns the bare filename, without any path. So for most of what is normally done with files we need to prepend it with the path (unless you first chdir to that directory). This is what is done in the grep block above so that the -f file test (-X) has the correct filename.



          If you need to use the file list itself, get that into an array and then assign it to a scalar



          # Get the file list, then its length
          my @files_gfs = map { "$dir/$_" } grep { /gfs./ } readdir($dh);
          my $num_gfs = @files_gfs;


          Here map builds the full path for each file. If you don't need the path drop map { }. Note that there is normally no need for the formal use of scalar on the array to get the count, like



          my $num_gfs = scalar @files_gfs;


          Simply assign an array to a scalar instead, it's an idiom (to say the least).



          If you are processing files as you read, count as you go



          my $cnt_gfs = 0;
          while (my $filename = readdir($dh)) {
          $cnt_gfs++ if $filename =~ /gfs./;
          # Process $dir/$filename as needed
          }


          Here readdir is in the scalar context (since its output is assigned to a scalar), and it iterates through the directory entries, returning one at a time.






          share|improve this answer




























            5












            5








            5







            If you want to just count them, once you have a directory open for reading you can manipulate context on readdir so that it returns the list of all entries, and then assign that to a scalar. This gives you the length of the list, ie. the number of elements



            opendir my $dh, $dir;
            my $num_entries = () = readdir($dh);


            The construct = () = goes under the name of goatse operator, see it in perlsecret. There are clearer ways, of course, as below.



            If you want to count certain kinds of files, pass the file list through grep first, like you do. Since grep imposes the list context on its input readdir returns the list of all files, and after filtering grep itself returns a list. When you assign that to a scalar you get the length of the list (number of elements), ie. your count. For example, for all regular files and /gfs./ files



            use warnings;
            use strict;

            my $dir = '/home/Enric/gfs-0.5.2016061400';
            opendir my $dh, $dir or die "Can't open $dir: $!";

            my $num_files = grep { -f "$dir/$_" } readdir($dh);
            rewinddir($dh); # so that it can read the dir again
            my $num_gfs = grep { /gfs./ } readdir($dh);


            (This is only an example, with rewinddir so that it works as it stands. To really get two kinds of files from a directory better read all files into an array and then process that)



            Note that readdir returns the bare filename, without any path. So for most of what is normally done with files we need to prepend it with the path (unless you first chdir to that directory). This is what is done in the grep block above so that the -f file test (-X) has the correct filename.



            If you need to use the file list itself, get that into an array and then assign it to a scalar



            # Get the file list, then its length
            my @files_gfs = map { "$dir/$_" } grep { /gfs./ } readdir($dh);
            my $num_gfs = @files_gfs;


            Here map builds the full path for each file. If you don't need the path drop map { }. Note that there is normally no need for the formal use of scalar on the array to get the count, like



            my $num_gfs = scalar @files_gfs;


            Simply assign an array to a scalar instead, it's an idiom (to say the least).



            If you are processing files as you read, count as you go



            my $cnt_gfs = 0;
            while (my $filename = readdir($dh)) {
            $cnt_gfs++ if $filename =~ /gfs./;
            # Process $dir/$filename as needed
            }


            Here readdir is in the scalar context (since its output is assigned to a scalar), and it iterates through the directory entries, returning one at a time.






            share|improve this answer















            If you want to just count them, once you have a directory open for reading you can manipulate context on readdir so that it returns the list of all entries, and then assign that to a scalar. This gives you the length of the list, ie. the number of elements



            opendir my $dh, $dir;
            my $num_entries = () = readdir($dh);


            The construct = () = goes under the name of goatse operator, see it in perlsecret. There are clearer ways, of course, as below.



            If you want to count certain kinds of files, pass the file list through grep first, like you do. Since grep imposes the list context on its input readdir returns the list of all files, and after filtering grep itself returns a list. When you assign that to a scalar you get the length of the list (number of elements), ie. your count. For example, for all regular files and /gfs./ files



            use warnings;
            use strict;

            my $dir = '/home/Enric/gfs-0.5.2016061400';
            opendir my $dh, $dir or die "Can't open $dir: $!";

            my $num_files = grep { -f "$dir/$_" } readdir($dh);
            rewinddir($dh); # so that it can read the dir again
            my $num_gfs = grep { /gfs./ } readdir($dh);


            (This is only an example, with rewinddir so that it works as it stands. To really get two kinds of files from a directory better read all files into an array and then process that)



            Note that readdir returns the bare filename, without any path. So for most of what is normally done with files we need to prepend it with the path (unless you first chdir to that directory). This is what is done in the grep block above so that the -f file test (-X) has the correct filename.



            If you need to use the file list itself, get that into an array and then assign it to a scalar



            # Get the file list, then its length
            my @files_gfs = map { "$dir/$_" } grep { /gfs./ } readdir($dh);
            my $num_gfs = @files_gfs;


            Here map builds the full path for each file. If you don't need the path drop map { }. Note that there is normally no need for the formal use of scalar on the array to get the count, like



            my $num_gfs = scalar @files_gfs;


            Simply assign an array to a scalar instead, it's an idiom (to say the least).



            If you are processing files as you read, count as you go



            my $cnt_gfs = 0;
            while (my $filename = readdir($dh)) {
            $cnt_gfs++ if $filename =~ /gfs./;
            # Process $dir/$filename as needed
            }


            Here readdir is in the scalar context (since its output is assigned to a scalar), and it iterates through the directory entries, returning one at a time.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 0:47

























            answered Jun 14 '16 at 23:05









            zdimzdim

            33.2k32142




            33.2k32142

























                4














                You have the list of files in @FILES. So your question becomes "how do I get the length of an array?" And that's simple, you simply evaluate the array in scalar context.



                my $number_of_files = @FILES;
                print $number_of_files;


                Or you can eliminate the unnecessary scalar variable by using the scalar() function.



                print scalar @FILES;





                share|improve this answer
























                • Thank you for the comments @Dave Cross

                  – Enric Agud Pique
                  Jun 15 '16 at 15:38
















                4














                You have the list of files in @FILES. So your question becomes "how do I get the length of an array?" And that's simple, you simply evaluate the array in scalar context.



                my $number_of_files = @FILES;
                print $number_of_files;


                Or you can eliminate the unnecessary scalar variable by using the scalar() function.



                print scalar @FILES;





                share|improve this answer
























                • Thank you for the comments @Dave Cross

                  – Enric Agud Pique
                  Jun 15 '16 at 15:38














                4












                4








                4







                You have the list of files in @FILES. So your question becomes "how do I get the length of an array?" And that's simple, you simply evaluate the array in scalar context.



                my $number_of_files = @FILES;
                print $number_of_files;


                Or you can eliminate the unnecessary scalar variable by using the scalar() function.



                print scalar @FILES;





                share|improve this answer













                You have the list of files in @FILES. So your question becomes "how do I get the length of an array?" And that's simple, you simply evaluate the array in scalar context.



                my $number_of_files = @FILES;
                print $number_of_files;


                Or you can eliminate the unnecessary scalar variable by using the scalar() function.



                print scalar @FILES;






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 15 '16 at 9:59









                Dave CrossDave Cross

                47.9k34079




                47.9k34079













                • Thank you for the comments @Dave Cross

                  – Enric Agud Pique
                  Jun 15 '16 at 15:38



















                • Thank you for the comments @Dave Cross

                  – Enric Agud Pique
                  Jun 15 '16 at 15:38

















                Thank you for the comments @Dave Cross

                – Enric Agud Pique
                Jun 15 '16 at 15:38





                Thank you for the comments @Dave Cross

                – Enric Agud Pique
                Jun 15 '16 at 15:38











                1














                Try this code for starters (this is on Windows and will include . , .. and folders. Those can be filtered out if you want only files):



                #!/usr/bin/perl -w

                my $dirname = "C:/Perl_Code";
                my $filecnt = 0;

                opendir (DIR, $dirname) || die "Error while opening dir $dirname: $!n";
                while(my $filename = readdir(DIR)){
                print("$filenamen");
                $filecnt++;
                }
                closedir(DIR);
                print "Files in $dirname : $filecntn";
                exit;





                share|improve this answer




























                  1














                  Try this code for starters (this is on Windows and will include . , .. and folders. Those can be filtered out if you want only files):



                  #!/usr/bin/perl -w

                  my $dirname = "C:/Perl_Code";
                  my $filecnt = 0;

                  opendir (DIR, $dirname) || die "Error while opening dir $dirname: $!n";
                  while(my $filename = readdir(DIR)){
                  print("$filenamen");
                  $filecnt++;
                  }
                  closedir(DIR);
                  print "Files in $dirname : $filecntn";
                  exit;





                  share|improve this answer


























                    1












                    1








                    1







                    Try this code for starters (this is on Windows and will include . , .. and folders. Those can be filtered out if you want only files):



                    #!/usr/bin/perl -w

                    my $dirname = "C:/Perl_Code";
                    my $filecnt = 0;

                    opendir (DIR, $dirname) || die "Error while opening dir $dirname: $!n";
                    while(my $filename = readdir(DIR)){
                    print("$filenamen");
                    $filecnt++;
                    }
                    closedir(DIR);
                    print "Files in $dirname : $filecntn";
                    exit;





                    share|improve this answer













                    Try this code for starters (this is on Windows and will include . , .. and folders. Those can be filtered out if you want only files):



                    #!/usr/bin/perl -w

                    my $dirname = "C:/Perl_Code";
                    my $filecnt = 0;

                    opendir (DIR, $dirname) || die "Error while opening dir $dirname: $!n";
                    while(my $filename = readdir(DIR)){
                    print("$filenamen");
                    $filecnt++;
                    }
                    closedir(DIR);
                    print "Files in $dirname : $filecntn";
                    exit;






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jun 14 '16 at 21:54









                    tale852150tale852150

                    1,37321420




                    1,37321420























                        0














                        I know this isn't in Perl, but if you ever need a quick way, just type this into bash command line:



                         ls -1 | wc -l 


                        ls -1 gives you a list of the files in the directory, and wc -l gives you the line count. Combined, they'll give you the number of files in your directory.



                        Alternatively, you can call bash from Perl (although you probably shouldn't), using



                        system("ls -1 | wc -l"); 





                        share|improve this answer




























                          0














                          I know this isn't in Perl, but if you ever need a quick way, just type this into bash command line:



                           ls -1 | wc -l 


                          ls -1 gives you a list of the files in the directory, and wc -l gives you the line count. Combined, they'll give you the number of files in your directory.



                          Alternatively, you can call bash from Perl (although you probably shouldn't), using



                          system("ls -1 | wc -l"); 





                          share|improve this answer


























                            0












                            0








                            0







                            I know this isn't in Perl, but if you ever need a quick way, just type this into bash command line:



                             ls -1 | wc -l 


                            ls -1 gives you a list of the files in the directory, and wc -l gives you the line count. Combined, they'll give you the number of files in your directory.



                            Alternatively, you can call bash from Perl (although you probably shouldn't), using



                            system("ls -1 | wc -l"); 





                            share|improve this answer













                            I know this isn't in Perl, but if you ever need a quick way, just type this into bash command line:



                             ls -1 | wc -l 


                            ls -1 gives you a list of the files in the directory, and wc -l gives you the line count. Combined, they'll give you the number of files in your directory.



                            Alternatively, you can call bash from Perl (although you probably shouldn't), using



                            system("ls -1 | wc -l"); 






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jun 15 '16 at 21:23









                            JDYJDY

                            7719




                            7719






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37821571%2fcount-number-of-files-in-a-folder-with-perl%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Bressuire

                                Vorschmack

                                Quarantine