x
Solved

# Count of Files & subfolders

Posted on 2008-06-09
Medium Priority
2,522 Views
1 Endorsement
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";
``````
1
Question by:Desire2change
• 4
• 3
• 2

LVL 85

Expert Comment

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

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

Thanks.
0

LVL 8

Expert Comment

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";
``````
0

LVL 85

Expert Comment

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

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 85

Expert Comment

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 85

Expert Comment

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

srnar earned 2000 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";
``````
0

Author Closing Comment

ID: 31465638
thanks
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.