How to determine if a directory is a subdirectory of another directory?
- From: Arjen Markus <arjen.markus@xxxxxxxxxx>
- Date: Tue, 25 Nov 2008 21:27:56 -0800 (PST)
Hello,
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:
..
...
work/
example.inp
Script:
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
first?
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)
Regards,
Arjen
.
- Follow-Ups:
- Re: How to determine if a directory is a subdirectory of another directory?
- From: Glenn Jackman
- Re: How to determine if a directory is a subdirectory of another directory?
- From: Andreas Leitgeb
- Re: How to determine if a directory is a subdirectory of another directory?
- From: Donald Arseneau
- Re: How to determine if a directory is a subdirectory of another directory?
- Prev by Date: Re: What happened to Tcl and Expect
- Next by Date: Re: How to determine if a directory is a subdirectory of another directory?
- Previous by thread: What happened to Tcl and Expect
- Next by thread: Re: How to determine if a directory is a subdirectory of another directory?
- Index(es):