Solved

Count of Files & subfolders

Posted on 2008-06-09
9
2,438 Views
1 Endorsement
Last Modified: 2013-12-25
I am beginner in perl .My goal is to get path name of a folder from user & count total number of files and sub-directories in it using recursive function .

example : "Main" is main folder name which contains 2 subfolders SUB1 & SUB2 and 2 files mainfile1.txt,mainfile2.txt . Under each sub folder 1-file is there such as subfile1.txt & subfile2.txt in their respective folders.



-------
My snippet heer does to count number of subfolders -

#!/usr/bin/perl

use File::Find;

$dir = '.';

finddepth(\&ren_dir, $dir);

$count=0;
sub ren_dir {
next unless (-d);
$tot=$count++ ;
print "we are now inside :";
print($_ );
print "\n";
}
print "Total no of subdirectories under folder $dir= $tot";

------

Required o/p -

Main folder contains 2 files & 2 subfolders .
SUB1 folder contains 1 file
SUB2 folder contains 1 file

Totally Main folder contains 4 files & 2 subnfolders.

i don't know how to seperate the files & directories inside the folder .. my assumation of (-d) is to check whether the value is directory type..but still not clear . Please help me at the earliest .




Thanks.

My snippet heer does to count number of subfolders -
 
#!/usr/bin/perl 
 
use File::Find;
 
$dir = '.'; 
 
finddepth(\&ren_dir, $dir);
 
$count=0;
sub ren_dir {
next unless (-d);
$tot=$count++ ;
print "we are now inside :";
print($_ );
print "\n";
}
print "Total no of subdirectories under folder $dir= $tot";

Open in new window

1
Comment
Question by:Desire2change
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 21749478
#!/usr/bin/perl

use File::Find;

$dir = '.';
$file=0;
$count=0;
finddepth(\&ren_dir, $dir);
$count=0;
sub ren_dir {
$file++ if -f;
next unless (-d);
$tot=$count++ ;
print "we are now inside :";
print($_ );
print "\n";
}
print "Total no of subdirectories under folder $dir= $tot\n";
print "Total no of files under folder $dir= $file\n";
0
 

Author Comment

by:Desire2change
ID: 21749811
It must listout each folders number of subfolder & files if any .

Thanks.
0
 
LVL 8

Expert Comment

by:srnar
ID: 21749827
Here you have script for traversing directory and counting number of subdirectories and files within.
#!/usr/bin/perl 
 
use strict;
use warnings;
 
 
my $dirCount=0;
my $fileCount=0;
 
my $dir='.';
 
countDir($dir);
 
sub countDir 
{
  my ($root) = @_;
  my @files = <$root/*>;
 
  foreach my $file (@files) 
  {
    print "found: $file\n";
    if (-d $file)
    {
      $dirCount++;
      countDir($file);
    } else
    {
      $fileCount++;
    }
      
  }
 
}
 
print "Total No of subdirectories under folder $dir= $dirCount with $fileCount of files";

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 21749921
$dir = '.';
%file=();
$count=0;
finddepth(\&ren_dir, $dir);
$count=0;
sub ren_dir {
$file{$File::Find::dir}++ if -f;
next unless (-d);
$tot=$count++ ;
print "we are now inside :";
print($_ );
print "\n";
}
print "Total no of subdirectories under folder $dir= $tot\n";
for( sort keys %file ){
   print "$file{$_} files in $_\n";
}
0
 

Author Comment

by:Desire2change
ID: 21750027
thanks for your efforts , but still i want to goto each sub-folder & count the files & subfolders in it & so on till end . Please help me .
0
 
LVL 84

Expert Comment

by:ozo
ID: 21750109
#!/usr/bin/perl
 
use File::Find;
 
$dir = '.';
 
finddepth(\&ren_dir, $dir);
 
$count=0;
sub ren_dir {
next unless (-d);
$tot=$count++ ;
print "we are now inside :";
print($_ );
print "\n";
}
print "Total no of subdirectories under folder $dir= $tot";

$dir = '.';
%file=();
%dir=();
$count=0;
finddepth(\&ren_dir, $dir);
$count=0;
sub ren_dir {
$file{$File::Find::dir}++ if -f;
next unless (-d);
$dir{$File::Find::dir}++ ;
print "we are now inside :";
print($_ );
print "\n";
}
for( sort keys %file ){
   print "$file{$_} files in $_\n";
}
for( sort keys %dir ){
   print "$dir{$_} folders in $_\n";
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 21750123
#!/usr/bin/perl
 
use File::Find;
 
$dir = '.';
 
finddepth(\&ren_dir, $dir);
 
$count=0;

$dir = '.';
%file=();
%dir=();
$count=0;
finddepth(\&ren_dir, $dir);
$count=0;
sub ren_dir {
$file{$File::Find::dir}++ if -f;
next unless (-d);
$dir{$File::Find::dir}++ ;
print "we are now inside :";
print($_ );
print "\n";
}
for( sort keys %file ){
   print "$file{$_} files in $_\n";
}
for( sort keys %dir ){
   print "$dir{$_} folders in $_\n";
}
0
 
LVL 8

Accepted Solution

by:
srnar earned 500 total points
ID: 21793230
Modified version of my 1st traversing script. Writes number of subdirs, files by directory and also overall total.
#!/usr/bin/perl 
 
use strict;
use warnings;
 
 
my $dirCount=0;
my $fileCount=0;
 
my $dir='.';
 
countDir($dir);
 
sub countDir 
{
  my ($root) = @_;
  my @diskOjbs = <$root/*>;
  my $dirCountLoc=0;
  my $fileCountLoc=0;
 
  foreach my $item (@diskOjbs) 
  {   
    if (-d $item)
    {
      $dirCount++;
      $dirCountLoc++;
      countDir($item);
    } else
    {
      $fileCount++;
      $fileCountLoc++;
    }      
  }
  print "$dirCountLoc subdirectory(s) under folder '$root' with $fileCountLoc file(s)\n";
 
}
 
print "Total directory(s) $dirCount with file(s) $fileCount";

Open in new window

0
 

Author Closing Comment

by:Desire2change
ID: 31465638
thanks
0

Featured Post

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question