How to determine if a directory is a subdirectory of another directory?


I ran into a problem with copying files and it turns out to be due to
the fact
that I was copying all files in a partcular directory into another
directory that
was a subdirectory of the original one, like:

Directory contents:

foreach f [glob *] {
file copy $f work

which led to an almost endless loop (the path became too long, that is
why it stopped)

So I wonder how I can best avoid this situation. For instance: if I
have the strings "example"
and "../example/work" and I know that the directory "example" exists
(but not necessarily the
other one), how can I tell that the second is a subdirectory of the

Is normalizing both and then examine the result via [file split] the
most robust way?
Something along these lines:

set dir1 [file normalize "example"]
set dir2 [file normalize "../example/work"]

foreach p1 [file split $dir1] p2 [file split $dir2] {
if { $p1 != $p2 && $p1 != "" && $p2 != "" } {
puts "Subdirectories!"

(Well, the above does not quite work, but I hope my question is clear
enough. It could probably
serve as a basis)