×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

check if directory exists

check if directory exists

check if directory exists

(OP)
$path = "folder1\folder2\folder3";
if (system($createdir)) { print "mkdir failed\n"; };


I want to check if this directory exists before creating it. $cpath is a relative path. I want to create it only if it doesn't exist. I'm using Windows..can anyone help me?

I tried -d and -e, it doesn't work...

RE: check if directory exists

Hi,

This works on my system

WinXP Home, Perl v5.8.6

$dir = 'c:/mike/tmp/';

unless(-d $dir){
    mkdir $dir or die;
}

Mike

I am not inscrutable.

Want great answers to your Tek-Tips questions? Have a look at FAQ219-2884

RE: check if directory exists

well, you are checking if the system call is true:

if (system($createdir)) { print "mkdir failed\n"; };

so you would write it like this:

if (system($createdir)) { print "mkdir good\n"; }
else { print "mkdir failed";}

but it's easier to do it like this:

print "mkdir failed\n" unless (system($createdir));

RE: check if directory exists

use strict;
my $path = "folder1/folder2/folder3";
# need to use forward slashes for perl, even under windows.
my $createdir="mkdir -p $path";
# does mkdir -p work under windows???
if (-d $path)
  { print "$path exists\n" }
else
  { print "mkdir failed\n" unless system($createdir) }

RE: check if directory exists

(OP)
I tried Mike's code..the problem arise when I need to create a folder with space in the folder name


$dir = 'c:/mike/tmp oops space/';

unless(-d $dir){
    mkdir $dir or die;
}

This will die.

RE: check if directory exists

Escape the spaces with \'s like

$dir = 'c:/mike/tmp\ oops\ space/';

RE: check if directory exists

Or

$dir =~ s/ /\\ /g;

RE: check if directory exists

spaces are OK on a windows machine so they shouldn't be a problem. Capture the return message of die and see what it reports:


$dir = 'c:/mike/tmp oops space/';

unless(-d $dir){
    mkdir $dir or die "Couldn't create dir: [$dir] ($!)";
}

RE: check if directory exists

In case you were implying otherwise, Kevin: spaces are perfectly ok in *nix directory names too.

RE: check if directory exists

*cough* evil *cough*

RE: check if directory exists

(OP)
$dir = 'c:/mike/tmp oops space/';

unless(-d $dir){
    mkdir $dir or die "Couldn't create dir: [$dir] ($!)";
}



I ran above script..and got this error msg.

C:\PerlScripts>test.pl
Couldn't create dir: [c:/mike/
\PerlScripts\test.pl line 4.

I followed kodaff's code to replace the spaces

$dir =~ s/ /\\ /g;

and got this error message

C:\PerlScripts>test.pl
Couldn't create dir: [c:/mike/tmp\ oops\ space/] (No such file or directory) at
C:\PerlScripts\test.pl line 9.

Can anyone try it on their windows environment?

RE: check if directory exists

Maybe try something like:

CODE

$dir = '"c:/mike/tmp oops space/"';

RE: check if directory exists

it's because you are trying to create a directory and a sub directory at the same time, Windows doesn't like that. I think there is a module that will allow you to create directories with sub directories even if the OS doesn't allow that.

RE: check if directory exists

Hmmm.. from a cmd shell, I can type

CODE

mkdir "c:/mike/tmp oops space/"
without 'c:\mike' existing and it will create both directories for me.

I haven't tried it from a perl script yet, but it looks like WinXP does ok with creating a directory and subdirectory at the same time.

RE: check if directory exists

found it:

CODE

use File::Path;
my $dir = 'c:/mike/tmp oops space/';
mkpath($dir, 1);


remove the 1 as the second argument if you don't want to print the directories to the screen as they are created.

RE: check if directory exists

Could be the XP mkdir kernel supports more than one level of mkdir, seems Win98 doesn't though, which I use to test with. Either way, using File::Path should work.

RE: check if directory exists

I tried it from the command line of Win98 and it will not work for more than one level of directory creation.

RE: check if directory exists

Yeah, you're right - File::Path will probably make it easier all the way around.

RE: check if directory exists

(OP)
yes, finally, this works..thanks! I'll read up on the Path module..not too familiar with it.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close