This script takes a pathname, and checks whether each directory in the path exists, and, if any of them don't exist, creates them. I've only tested it on windows, but it should theoretically work on *nix systems...
sub vfdir { my $dir = shift; $dir =~ s/\\/\//g; # convert \'s (if any) to /'s $dir =~ s/\/$//; # remove trailing / @dirs = split(/\//,$dir); # split directories into array if($dirs[0] =~ /^[a-z]\:/i) { # there's a drive letter, this must +be windows! foreach my $i (1..$#dirs) { # remove spaces & illegal chars $dirs[$i] =~ s/(\s|\;|\:|\*|\?|\"|\<|\>|\|)//g; } $build = ''; my $drive = "$dirs[0]\\"; print "cd: ", chdir($drive) or return 0; foreach $i (1..$#dirs) { $build .= "$dirs[$i]".'\\'; unless(-e "$dirs[0]\\$build") { # remember, this is window +s, use \'s, not /'s chdir("$dirs[0]\\$build") or return 0; mkdir("$dirs[0]\\$build") or return 0; } } } else { # there's no drive letter, assume it's (li|u)n(u|i)x! my $build = ''; foreach my $i (0..$#dirs) { $build .= "$dirs[$i]/"; unless(-e "/$build") { chdir("/$build") or return 0; mkdir("/$build") or return 0; } } } return 1; } =head1 NAME C<vfdir(PATHNAME)> - Verifies that all directories in B<PATHNAME> exis +t, and creates any that don't exist. Returns 1 on success, 0 on fail +ure. =head1 SYNOPSIS vfdir("c:\\program files\\dir\\subdir"); # if "dir" doesn't, exist, it creates # it, then creates "subdir" =head1 NOTES I<Should> work on both win32 & unix systems, but only tested on win32. =head1 AUTHOR Evan Kaufman E<lt>evank@scriptionsoft.comE<gt> =cut

In reply to Verifying existence of pathnames, taken up a notch... by EvanK

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.