Gossamer Forum
Home : General : Perl Programming :

Create directory only if it does not exist.

Quote Reply
Create directory only if it does not exist.
Perl Beginer/Dummie Question:

How do I use perl to check if a directory exists, and if not, then create it?

Example directory: "$CFG->{build_root_url}/editors/profiles/"



Thanks in advance.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Create directory only if it does not exist. In reply to
Have a look at nph-build.cgi (Links SQL), sub _build_dir

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Create directory only if it does not exist. In reply to
unless (-e "$CFG->{build_root_path}$path") {

make the diretory here

}

That seems to be the gist of it in nph_build.

I'll try that, thankSmile

I think I might start looking though more of this file..... seems like there is some great stuff in here!


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Create directory only if it does not exist. In reply to
You might want -d instead of -e

-e is exists, -d is directory.

Last edited by:

Paul: May 31, 2002, 2:42 PM
Quote Reply
Re: [Paul] Create directory only if it does not exist. In reply to
Thanks Paul!

I have been pulling what little hair I have out here... wondering why it woudn't work...

I now have:

#Generate the /editors/profiles directory if it does not already exists

unless (-d "$CFG->{build_root_url}/editors/profiles") {

mkdir("$CFG->{build_root_url}/editors/profiles", 0777);

}

Which just hangs....

So I tried:

#Generate the /editors/profiles directory if it does not already exists
mkdir("$CFG->{build_root_url}/editors/profiles", 0777) unless -d "$CFG->{build_root_url}/editors/profiles";


which also hangs...

so I removed the code, and now it still hangs!

I am really having a bad day with thisMad.... maybe i'll come back to it later...



http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Create directory only if it does not exist. In reply to
Here's where CPAN is truly your friend.

Look into the module File::Path which is a part of the core dsitribution. If not, then head over to download CPANPLUS which is a far more sophisticated tool than CPAN.pm -> http://cpanplus.sourceforge.net/ <-. Once this comes out of BETA it will replace CPAN.pm anyway, so it's a good idea to familiarize yourself, but it ain't that difficult to use.

Using File::Path you would use something like:

Code:
use File::Path;

my $dir = "/path/to/my/new/dir";

mkpath ("$dir") or die $!;
chmod 0777, "$dir ";

Note that I've added in a check to see if the directory was created. You don't have any error checking in your snippet which is probably why you're pulling your hair out "guessing" what the error is.

- wil

Last edited by:

Wil: May 31, 2002, 3:07 PM
Quote Reply
Re: [Wil] Create directory only if it does not exist. In reply to
Thanks Wil.

I am remotely hosted unfortuantely... but I suspect I may have those modules, so I will give your code a try! (as soon as I can figure out what is stopping me from loading my plugin now....) I reloaded the original file and it wont install.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Create directory only if it does not exist. In reply to
Sorry, I'm not familiar with any of the Gossamer products' plugin systems.

That module should be with you with all standard Perl distributions.

- wil
Quote Reply
Re: [Wil] Create directory only if it does not exist. In reply to
Ok, thanks everyone who helped in this thread. I think I have this working now.Smile


http://www.iuni.com/...tware/web/index.html
Links Plugins