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

Replies are listed 'Best First'.
Re: Verifying existence of pathnames, taken up a notch...
by merlyn (Sage) on Oct 12, 2001 at 11:26 UTC