Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check if directory exists 1

Status
Not open for further replies.

sacgirl

MIS
Nov 4, 2005
7
US
$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...
 
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. [orientalbow]

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

 
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));
 
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) }
 
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.
 
Escape the spaces with \'s like

$dir = 'c:/mike/tmp\ oops\ space/';
 
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] ($!)";
}

 
In case you were implying otherwise, Kevin: spaces are perfectly ok in *nix directory names too.
 
$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?
 
Maybe try something like:
Code:
$dir = '"c:/mike/tmp oops space/"';
 
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.
 
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.
 
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.
 
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.
 
I tried it from the command line of Win98 and it will not work for more than one level of directory creation.
 
Yeah, you're right - File::path will probably make it easier all the way around.
 
yes, finally, this works..thanks! I'll read up on the Path module..not too familiar with it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top