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

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:


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) {
    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':



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.


my $topdir = 'c:/test';

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) {


> c:/test
> c:/test/dir1
> c:/test/dir2
> c:/test/dir2/dir3
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

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.


my $topdir='databases~4:dir:1';

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) {
        print "\t$entry\n";
        if ($entry=~m/dir/) {

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

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