Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need to make code snippet recursive

Status
Not open for further replies.

tonykent

IS-IT--Management
Jun 13, 2002
251
GB
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?
 
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:
> 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.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top