SPUG: Yet another silly question

William Julien moonbeam at catmanor.com
Sat Jan 19 16:48:15 CST 2002


>
>Ok, I am trying to figure out the coding for checking to see if a directory
>exists and if it doesn't, create it.  I've gotten out my "Perl For Dummies"
>book and did a lot of searching and am still stumped.  Anyone have a quick
>and easy answer for me?
>
>Thanks!
>
>Susanne Bullo - Sweet Homes
>

Maybe something like this:

#!/usr/bin/perl -w
#
$dir = "some/directory/path";
$base = "/usr/people/moonbeam";

use File::Path;

if ( ! -f "$base/$dir" ) {
    if ( ! -d "$base/$dir" ) {
        mkpath("$base/$dir", 0, 0755 ) or 
            die "Cannot make $dir/$base - $!\n";
    }
}

Here is the perldoc on File::Path:

NAME
       File::Path - create or remove directory trees

SYNOPSIS
           use File::Path;

           mkpath(['/foo/bar/baz', 'blurfl/quux'], 1, 0711);
           rmtree(['foo/bar/baz', 'blurfl/quux'], 1, 1);


DESCRIPTION
       The "mkpath" function provides a convenient way to create
       directories, even if your "mkdir" kernel call won't create
       more than one level of directory at a time.  "mkpath"
       takes three arguments:

       o   the name of the path to create, or a reference to a
           list of paths to create,

       o   a boolean value, which if TRUE will cause "mkpath" to
           print the name of each directory as it is created
           (defaults to FALSE), and

       o   the numeric mode to use when creating the directories
           (defaults to 0777)

       It returns a list of all directories (including intermedi-
       ates, determined using the Unix '/' separator) created.

William

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/





More information about the spug-list mailing list