Solved

Count of Files & subfolders

Posted on 2008-06-09
9
2,416 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

734 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