RE: Process Directory and subdirectories Recursively



Hi Mimi,

I could think of a replacement of "ls -R" command of unix in windows. It's "dir /S".

Recursive function call is a one way to do the same.

You can try File::Find as well: http://search.cpan.org/~lbrocard/perl5.005_04/lib/File/Find.pm

Thanks,
Ajay

-----Original Message-----
From: Mimi Cafe [mailto:mimicafe@xxxxxxxxxxxxxx]
Sent: Monday, January 28, 2008 5:25 PM
To: beginners@xxxxxxxx
Subject: Process Directory and subdirectories Recursively


Hi,

I am trying to process a directory and all subdirectory recursively and
generate a list as the Unix command "ls -R" will display, but this seems not
to behave as it should.
My script only goes as far as processing the directory and the 1 step below
not more.

I have some like, but the script goes as far as 1_child and 2_child but no
further:
/parent/1_child/1_grant_child/././
/parent/2_child/2_grant_child/././

The script will finally be used on Windows OS, Unix system command cannot
be used.

Can anyone help?

Regards

Mimi

###############################################################
#!/usr/bin/perl -w


# Recursively read content of a folder and dynamically create a list on an
html page.

use CGI qw(:standard);
#use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
#use warnings;
use strict;



print header(),
start_html("Documents list");
print "<h1> Documents List</h1><br>\n";

my $source = "/depot/my_directory";

opendir (SOURCE, $source) or die "Cannot open the source folder for reading:
$!\n";

my $file;
while (defined($file = readdir(SOURCE))){
next if $file =~ /^\.{1,2}$/;
my $full_name = "$source/$file";
if (-d $full_name){
#my $full_name = "$source/$file";
print qq(<b>$file</b><br>);
process($full_name);
}else{
print qq(<a href="$full_name>$file</a><br>);
}
#next;
}
closedir (SOURCE);


print end_html();



# Subroutine to process subdirectory and list content.

sub process{

my ($path, $file, @files, $file_full_path);
$path = shift;
opendir (SUBDIR, $path) or die "Cannot open the subfolder for reading:
$!\n";

@files = sort grep {!/^\.{1,2}$/} readdir(SUBDIR);
closedir (SUBDIR);

for (@files){
$file_full_path = "$path/$_";

if (-d $_){
print qq(<b>$_</b><br />);
process($_);


}else {

print qq(<a href="$file_full_path">$_</a><br />);

}

}


}
#######################################################
.



Relevant Pages

  • Process Directory and subdirectories Recursively
    ... generate a list as the Unix command "ls -R" will display, ... My script only goes as far as processing the directory and the 1 step below ... or die "Cannot open the source folder for reading: ... closedir; ...
    (perl.beginners)
  • Re: G.M.T. Monday 10th April 2006
    ... obviously haven't been reading the script - that sort of thing is just not ... Health is merely the slowest possible rate at which one can die. ...
    (uk.people.silversurfers)
  • Extract lines by name
    ... But I actually use a script with 24 names and I would like to find a way where I have to open and close the infile only once. ...
    (comp.lang.perl.misc)
  • Where did the prophet really live?
    ... > under "North Arabian" is Classical Arabic and idioms that, ... explains why Old Arabic and ANA where written in the Sabaic script. ... Sabaic was the prestige script of the region. ... One doesn't need to dissociate the great reading from the prophet to ...
    (soc.religion.islam)
  • Re: No such file or directory
    ... Still cannot see why Perl complains that Could not open file for reading. ... I have modified my script and now using Getopt::Long module instead of the ... foreach $list { ...
    (perl.beginners)