For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > January 2008 > Process Directory and subdirectories Recursively









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Process Directory and subdirectories Recursively
Mimi Cafe

2008-01-28, 7:56 am

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 /> );

}

}


}
########################################
###############

Sean Davis

2008-01-28, 6:57 pm

On Jan 28, 2008 6:56 AM, Mimi Cafe <mimicafe@googlemail.com> wrote:
> 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?


I would suggest trying to use a CPAN module for this type of thing.
There may be others, but look at File::Find:

http://search.cpan.org/search%3fmodule=File::Find


>
> ########################################
#######################
> #!/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 /> );
>
> }
>
> }
>
>
> }
> ########################################
###############
>

Mimi Cafe

2008-01-29, 3:56 am

Hi Sean,

Find::File did the magic for me.

Thanks

Mimi

On Mon, 2008-01-28 at 08:20 -0500, Sean Davis wrote:[color=darkred]
> On Jan 28, 2008 6:56 AM, Mimi Cafe <mimicafe@googlemail.com> wrote:
>
> I would suggest trying to use a CPAN module for this type of thing.
> There may be others, but look at File::Find:
>
> http://search.cpan.org/search%3fmodule=File::Find
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com