Lixin,
 
Perl's mkdir function does not do parent directory creation like the shell command "mkdir -p somedir".  I wrote a little function to get around this.
 
# make a directory and it's parent if necessary
sub mkdirp {
    my $dir = shift;

    my $check;
 
    # if something with this path already exists and is not a
    # directory then return an error
    return (1) if (-e $dir && !-d $dir);

    # if this directory already exists then return the
    # directory name to show success
    return ($dir) if (-d $dir);
 
    # get the parent directory path
    my ($pdir) = $item =~ m%^(.+)/%;
 
    # make the parent directory first
    $check = &mkdirp ($pdir);

    # check to make sure parent was created correctly
    if ($check ne $pdir) {
        # return an error if there was a problem with
        # the parent
        return 1;
    } else {
        # make the directory
        $check = mkdir($dir, 0775);
        return 1 if ($check != 1);
    }
 
    # return success with the directory name
    return $dir;
}
 
Matthew Schneider
System Admin / Programmer
SKLD Information Services, LLC
303-820-0863
 


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Ella Cai
Sent: Monday, November 15, 2004 7:22 AM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: [Perl-unix-users] mkdir questions on unix platform

My codes is like this:
**********************************************************************
my $new_data_dir = "/var/tmp/sct_1.61/data";
mkdir ($new_data_dir, 0744) unless (-d $new_data_dir);
**********************************************************************
 
/var/tmp exists, but /sct_1.61/data does not exist under /var/tmp. It looks like mkdir function does not work well. It does not create /sct_1.61/data. Could you please let me know why? and how to solve it?
 
Thanks
 
Lixin
 

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to