in reply to Is this directory a subdirectory of another directory?
This script will build a directory tree using Tree::DAG_Node.#!/usr/bin/perl -w use strict; use File::Find; use Tree::DAG_Node; my $count =0; my $rootname="/home/perl/"; # or your favourite dir my $dirname= "testtree"; my $root = new Tree::DAG_Node; $root->name($dirname); my $node = $root; find(\&{ sub { if (-d "$File::Find::dir/$_"){ my $dir = $File::Find::dir; $dir .= "/$_" if $count++; $dir =~ s/^$rootname//; my $ancestor = $node; while ($ancestor) { my $name = $ancestor->name; if ((length($dir) > length($name)) && $dir =~ /^$name/) +{ my $nnode = new Tree::DAG_Node; $ancestor->add_daughter($nnode); $nnode->name($dir); $node = $nnode; last; } $ancestor = $ancestor->mother; } } } }, "$rootname$dirname"); sub is_below { my $node = shift; my $dir = shift; return scalar grep {$dir eq $_->name} $node->ancestors; } sub is_above { my $node = shift; my $dir = shift; return scalar grep {$dir eq $_->name} $node->self_and_descendants; } sub find_node { my $node = shift; my $dir = shift; my ($descendant) = grep {$dir eq $_->name} $node->self_and_descendants; return $descendant; } my $source = "testtree/a/k"; my $dest = "testtree/a"; my $n = find_node($root,$source) or die "can't find dir\n"; if (is_below($n,$dest)) { print "[$source] is below [$dest]\n"; } elsif (is_above($n,$dest)) { print "[$source] is above [$dest]\n"; } print "\nthe tree\n\n"; print $root->dump_names,"\n"; print "the same, fancier\n"; print map "$_\n", @{$root->draw_ascii_tree}; __END__ output: [testtree/a/k] is below [testtree/a] the tree 'testtree' 'testtree/b' 'testtree/b/y' 'testtree/b/x' 'testtree/a' 'testtree/a/k' 'testtree/a/k/m' 'testtree/a/k/l' 'testtree/a/j' the same, fancier (simplified, for posting purposes) | <testtree> /-----------------------\ | | <b> <a> /---------\ /-------\ | | | | <b/y> <b/x> <a/k> <a/j> /--------\ | | <a/k/m> <a/k/l>
_ _ _ _ (_|| | |(_|>< _|
|
|---|