user3148499
user3148499

Reputation: 27

How to create a folder using mkdir in Perl?

I want to create a folder in a particular path using mkdir.

Upvotes: 2

Views: 9684

Answers (3)

Richardd
Richardd

Reputation: 1012

You can also use:

my $dir = "../../folder/my_dir";
# if dir not exists create it
unless (-d "$dir") {`mkdirhier $dir`;}

Upvotes: 0

esaintpi
esaintpi

Reputation: 9

this syntax will check for directory existence and create it if needed

# here include path to new directory name
$newdir = './directory_name';
opendir(DIR, $newdir) || mkdir($newdir,0777) || die "Cannot create directory $newdir; $!";

Upvotes: -1

ikegami
ikegami

Reputation: 386361

Assuming D:/Test/Data exists

my $dir_qfn = 'D:/Test/Data/foo';
mkdir($dir_qfn)
   or $!{EEXIST}   # Don't die if $dir_qfn exists.
   or die("Can't create directory \"$dir_qfn\": $!\n");

If if might not,

use File::Path qw( make_path );

my $dir_qfn = 'D:/Test/Data/foo';
make_path($dir_qfn);

Upvotes: 5

Related Questions