prob with Folders recursivity

O

Orion93

Hello,

My script is good (I use a grep rather than a glob) but I'm not still able
to make it function on a continuation of folders.

I know that it is necessary to use the principle of recursivity but that
exceeds me completely!

What I would like, it returned the file c:\toto \ and which it will count
the number of lines of all the files having the extension toto1 being in
the tree structure depending of toto.

Could you indicate me the principle or the function to be used.

Here my current script:

#!/usr/bin/perl
use strict;
use warnings;
use Tk;
use diagnostics;
use Tk::Entry;

my $main = MainWindow -> new;
$main -> title("Test 1");
$main -> Label(-text=>"To:")->pack();
my $toto = $main -> Entry()->pack(-padx=>5);
$main -> Label(-text=>"Extension:")->pack();
my $toto1 = $main -> Entry(-width=>4)->pack(-padx=>5);
$main -> Label(-text=>'Result on c:\result.txt')->pack(-side=>'bottom',-
pady=>1);
$main->Button(-text=>'Ok',-command=>\&recupPages)->pack(-side=>'left',
expand=>1,-pady=>5);
$main->Button(-text=>'Close',-command=>sub {exit;})->pack(-side=>'right',
expand=>1,-pady=>1);
MainLoop();

sub recupPages
{
## Recover fields
my $dir=$toto->get();
my $ext=$toto1->get();
## Created a list of files with toto1 extension
opendir(DIR,$dir) or die "$!\n";
my @files = grep { /\.$ext$/ } readdir( DIR );
closedir( DIR );
## Foreach files
foreach (@files) {
open (SORTIE,">> c:\\result.txt");
open(PL,"$dir/$_") or die "$!\n";
my @lines = <PL>;
close(PL);
print SORTIE "Folder: $dir Files: $_ ".@lines." \n";
}
}
 
G

gnari

Orion93 said:
Hello,

My script is good (I use a grep rather than a glob) but I'm not still able
to make it function on a continuation of folders.

I know that it is necessary to use the principle of recursivity but that
exceeds me completely!

What I would like, it returned the file c:\toto \ and which it will count
the number of lines of all the files having the extension toto1 being in
the tree structure depending of toto.

Could you indicate me the principle or the function to be used.

Here my current script:
[snopped Tk script]

I am not sure what you want but you should take a look at File::Find

If you want to do it yourself, a typical way is something like:

sub process_dir {
my ($dir)=@_;
opendir(DIR,$dir) or die "could not open dir '$dir': $!\n";
my @files = readdir( DIR );
closedir( DIR );
foreach my $file(@files) {
unless ($file=~ m#^.{1,2}$!#) {
if (-d "$dir/$file") {
process_dir("$dir/$file");
} else {
# whatever you want to do to "$dir/$file"
}
}
}
}

gnari
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,230
Latest member
LifeBoostCBD

Latest Threads

Top