×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

need to make code snippet recursive

need to make code snippet recursive

need to make code snippet recursive

(OP)
Can anyone help me make this script recursive? It's defeating my limited scripting ability so far.

I'm trying to get a directory listing out of an informix database (note, this is not an actual file structure so I can't just glob the files or do a 'dir' etc).

The following code does give me what I want for the top level directory:

CODE

my ($name,$version,$instance,$type,%hash);
my $topdir='databases~4';
my @dircontent = `ccm query "is_child_of(\'$topdir:dir:1\', 'project~mine_2.5.2')"  -u -f "%name---%version---%instance---%type"`;
foreach my $entry(@dircontent) {
    chomp($entry);
    ($name,$version,$instance,$type)=split(/---/,$entry);
    $hash{$topdir}{$instance}=$type;
    if ($hash{$topdir}{$instance}=~m/dir/) {
        print "$topdir\n\t$name,$version,$instance,$type\n";
    } else {
        print "\t$entry\n";
    }
}

This nicely outputs the contents of the top level folder 'databases~4':

CODE

databases~4
        DB_UTILITIES,5,1,dir
databases~4
        CLIENT_DATA_SETUP,3,1,dir
databases~4
        INITIAL_SETUP,1,1,dir
databases~4
        MIGRATION,8,1,dir
        wrapper.sql---11---1---SQL

However, what I need to do is where an entry is of type 'dir', such as DB_UTILITIES or CLIENT_DATA_SETUP or INITIAL_SETUP or MIGRATION above, I need to list the contents of that dir (and the contents of any dir that might be within them etc).

I need to make the above code recursive where $hash{$topdir}{$instance} = dir.

Can anyone assist with some pseudo or actual code to do this?
 

RE: need to make code snippet recursive

See if this helps get you started. It's a simple example on a windows file system, but hopefully it will point you in the right direction. Also, I'm assuming that the utility you run to get the directory listings can list the contents of any folder without making that the active directory.

CODE

my $topdir = 'c:/test';
&dir_list($topdir);

sub dir_list {
    my $cur_path = shift;
    # Replace the system call in the next line with your
    # code to list the contents of the directory
    chomp(my @dir_content = `ls $cur_path`);
    my @sub_dirs;
    
    print "> $cur_path\n";
    foreach my $entry (@dir_content) {
        print "\t", $entry;
        if (-d "$cur_path/$entry") {
            # In OP code, you'd check $type instead of using -d
            # Print / Process Directories
            print '*';
            push @sub_dirs, $entry;
        } else {
            # Print / Process regular files
        }
        print "\n";
    }
    
    foreach my $dir (@sub_dirs) {
        &dir_list("$cur_path/$dir");
    }
}

CODE --> OUTPUT

> c:/test
        dir1*
        dir2*
        file0-1.txt
        file0-2.txt
        file0-3.txt
> c:/test/dir1
        file1-1.txt
        file1-2.txt
        file1-3.txt
        file1-4.txt
> c:/test/dir2
        dir3*
        file2-1.txt
        file2-2.txt
> c:/test/dir2/dir3
        file3-1.txt
A note, this might not deal with with links that point to somewhere higher up the path list (could end up in an endless loop.) I'm not sure if that's an issue with your file system, but just be aware of it.

RE: need to make code snippet recursive

(OP)
Thanks rharsh, that has certainly got me on my way. The listing is now printing out the contents of the subdirectories, I just need to work on the formatting a bit.

CODE

my $topdir='databases~4:dir:1';
&dir_list($topdir);

sub dir_list {
    my $objectname = shift;
    my @dircontent = `ccm query "is_child_of(\'$objectname\', 'project~mine_4.5.2')"  -u -f "%objectname"`;
    foreach my $entry(@dircontent) {
        chomp($entry);
        print "\t$entry\n";
        if ($entry=~m/dir/) {
            push(@alldirs,$entry);
        }
    }
}

foreach my $sub(@alldirs) {
    print "$sub\n";
    &dir_list("$sub");
}

I hope you and all the perl regulars have a propserous New Year.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close