--- Peter Lemus <[EMAIL PROTECTED]> wrote:
> I'm writing a script that will read a file, the data
> on the file are directories in a win2k server. I
> would like to test if the folder exists and create the
> folder if it doesn't exist. Please give me an example
> of how I can accomplish this.
open FOO, ">>$file" or die $!; # create if not there
open FOO, $file or die $!; # open to read
The first open is an append, which will create the file if it's there,
but won't harm it if it's not. The second open is a read-only, but
causes an implicit close on FOO before reopening it. Then just process
it accordingly.
There are myriad ways to do this, but I prefer this one, as it only
adds one line to your program (the appending open), doesn't require any
modules, etc. You could always add a close FOO; statement, but the 2nd
open does that, and the 2 open() statements immediately together tends
to draw attention.
On the other hand, you called it a folder. More explicit and possibly
more circumstantially correct code:
mkdir $folder unless -e $folder and -d _;
this creates directory $folder unless it already exists as a directory.
To be cleaner:
unless (-e $folder) {
mkdir $folder;
} else {
die "$folder not a directory!" unless -d _;
}
Then (assuming you're reading the directory info),
opendir DIR, $folder or die $!;
Then readdir to get your data.
As always,
Hope that helps.
Paul
__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/