Solved

Count of Files & subfolders

Posted on 2008-06-09
9
2,392 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Studying Angular for employability 5 88
Perl Write to Specific line in a file 15 95
Convert MSI to MSM 1 62
change the windows script file to BAT 10 43
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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.

832 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