Okay, I believe that this is a weird question. Not the question itself, but how I'm going to explain it! I think the easiest way to start is to give an example of a directory structure (I'm using Win32, so I'll use Win32 paths):

Now, say that the string "C:/Apache/www-root/images/bgs" is passed to the CGI script and stored in $input{'directory'}. Let's say that $input{'directory'} is the name of the directory I want to copy to another directory somewhere on the system, specified as $input{'copy_to'}. Here's the code that accomplishes this:

use File::NCopy; File::NCopy::copy (\1, "$input{'directory'}", "$input{'copy_to'}");

But there is a small problem with this code! If $input{'directory'} is "C:/Apache/www-root/images" and $input{'copy_to'} is "C:/Apache/www-root/images/bgs", the copy does a horrible thing. It continually, over and over again, copies the C:/Apache/www-root/images directory deeper and deeper into directory C:/Apache/www-root/images/bgs, so that one gets a filesystem such as this:

And that pattern continues on for whoever knows how many times. So in the end I have a directory C:/Apache/www-root/images/bgs/ images/bgs/ images/bgs/ images/bgs/ images/bgs/ images/bgs/ images/bgs/ images/bgs/ images/bgs/ images/bgs/images (minus the spaces) and that is DEFINITELY not what I want to ever happen. So to finally get to the actual question: How can I make sure that $input{'copy_to'} is not a subdirectory of $input{'directory'}?? Here is some more code. I believe that I know where the missing code has to go. It is colored in red. This is what I use to display a list of options to the web user of which directories s/he can copy to:

print qq|<select name="blah">\n|;

find(\&{ sub {
     if (-d "$File::Find::dir/$_"){
          if ($count++ == 0) {
               push @dirs, $File::Find::dir;
          } else {
               push @dirs, $File::Find::dir . "/" . $_;
          }
     }

   }
}, "C:/Apache/www-root");

foreach (sort { lc($a) cmp lc($b) } @dirs) {
if (something needs to go here) { print qq|<option>$_\n|; }
}

print qq|</select>|;

So what code has to go in that red if statement (or maybe somewhere in that find sub) to make sure that $input{'copy_to'} is not a subdirectory of $input{'directory'}??

Let me sum this up in one sentence:
"How do I make sure that a directory is not copied to a subdirectory of the directory I'm copying?"

Sorry for all the head scratching and confusion with this question. That was hard to describe...

Edit Petruchio Fri Mar 15 06:46:11 UTC 2002 - Fixed typo in title on author's behalf

Edit by tye as long path string made page render very wide in some browsers


In reply to Is this directory a subdirectory of another directory? by mt2k

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.