473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

traverse directory and count the number of files

5
Hi All,

I need to take the count of txt and bak files present in all the directory.for eg if we assume there are two directoies say(test1, test2), I need to collect the count(of txt and bak files) and print it out..

I tried the below code, but could not achieve what I need.Can someone help me on the same.

Prg:
Expand|Select|Wrap|Line Numbers
  1. ($dir, $extension) = @ARGV;
  2.    $dir = "." unless $dir;
  3.    $lextension = "txt" unless $lextension;
  4.    $dirCount = 0;
  5.    &loopDir($dir);
  6.    #$dirCount=$dircount-1;
  7.    print "Number of directories = $dirCount";
  8.  
  9.    exit;
  10.  
  11. sub loopDir {
  12.    local($dir, $margin) = @_;
  13.    chdir($dir) || die "Cannot chdir to $dir\n";
  14.    $dirCount++;
  15.    local(*DIR);
  16.    $filelCount=0;
  17.    $filetCount=0;
  18.    opendir(DIR, ".");
  19.    while ($f=readdir(DIR)) {
  20.       next if ($f eq "." || $f eq "..");
  21.  
  22.       if (-d $f) {
  23.          &loopDir($f,$margin."   ");
  24.       }elsif ($f=~/\.$lextension$/) {
  25.          print "$dir\n";
  26.          $filelCount++;
  27.          print "Number of files with .$lextension = $filelCount\n";
  28.   }
  29.        if ($f=~/\.bak$/) {
  30.         $filetCount++;
  31.         print "Number of files with .bak = $filetCount\n";
  32.       }
  33.  
  34.    }#while
  35.    closedir(DIR);
  36.    chdir("..");
  37. }
  38.  
Expected outcome:

C:\Users\ram\mycontents\test>perl count.pl
Number of directories = 2
Test1
Number of files with .txt = 1
Number of files with .bak = 1
Test2
Number of files with .txt = 1
Number of files with .bak = 2
Dec 12 '09 #1
1 4516
RonB
589 Expert Mod 512MB
Start by adding this near the top of the script.
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
Those pragmas will point out lots of problems. To begin with, you'll need to declare each of your vars with the my keyword.

It would be better to use the File::Find or File::Find::Rule module.
http://search.cpan.org/~dapm/perl-5....b/File/Find.pm
http://search.cpan.org/~rclamp/File-...e/Find/Rule.pm

That's assuming your instructor will let you use the module.
Dec 12 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python suppose you want to walk into a directory, say, to apply a string replacement to all html files. The os.path.walk() rises for the occasion. © import os ©...
4
by: Bart Plessers \(artabel\) | last post by:
Hello, I have an asp script that lists the files in a directory: CurrentPATH = "c:\temp\" Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(CurrentPATH) Set...
4
by: Baz | last post by:
I want to randomly select some files from a source directory & copy them to a target folder for testing. How can I select files from the source directory randomly? I have thought about getting a...
1
by: jj | last post by:
Hi folks: I can get the list of files in a certain directory by using Directory.GetFiles("c:\temp"). But I want to get the number of files (ex. 20) within a directory including all...
6
by: bindslind | last post by:
Hello. Pretty new to PHP(and any sort of programming), I've been trying to modify this script that was originally written to display mysql data so it will paginate images from a directory. The code...
1
by: dkmarni | last post by:
Hi, I am trying to do this perl script, but not able to complete it successfully. Here is the description what the script has to do.. Accept two and only two command line arguments. Again,...
2
by: nothing1 | last post by:
Here is my code and what it does is create a page for every directory with the contents of choice. Its been working on all letters except "u" I can't seem to figure it out. Does someone see the...
13
by: lawpoop | last post by:
Hello all - I have a two part question. First of all, I have a website under /home/user/www/. The index.php and all the other website pages are under /home/user/www/. For functions that are...
15
by: ghostxx | last post by:
Alright well i've been working at this for a ridiculously longer amount of time than i think i should have been, so i finally gave up and that's why im here. I program in C++ and hardly do any...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.