Solved

Count of Files & subfolders

Posted on 2008-06-09
9
2,371 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
Comment Utility
#!/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
Comment Utility
It must listout each folders number of subfolder & files if any .

Thanks.
0
 
LVL 8

Expert Comment

by:srnar
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
$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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Desire2change
Comment Utility
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
Comment Utility
#!/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
Comment Utility
#!/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
Comment Utility
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
Comment Utility
thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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 …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now