Link to home
Start Free TrialLog in
Avatar of Desire2change
Desire2change

asked on

Count of Files & subfolders

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

Avatar of ozo
ozo
Flag of United States of America image

#!/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";
Avatar of Desire2change
Desire2change

ASKER

It must listout each folders number of subfolder & files if any .

Thanks.
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

$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";
}
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 .
#!/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";
}
#!/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";
}
ASKER CERTIFIED SOLUTION
Avatar of srnar
srnar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks