Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I want to check if a directoy name exists.
/somedir/directory

For example want to check if "directoy", "Directory", "diRECtory" or "direCTORY" ....... exists
It's so i wont create the "directory" if "DiRectorY" already exists.
mkpath($filepath) unless -d "$filepath" ;
Will only be true on same-case directory's
Is there someway i can check it like a regex =~ /directory/i i can use "find" to create a dir list and check if it exists, but i thought that there might be a cleaner/easier way.
Thanks,
Milo

Replies are listed 'Best First'.
Re: check for directory no mather what case (linux)
by andyford (Curate) on Oct 19, 2007 at 19:22 UTC

    Just switch to an OS that has a non-case sensitive file system.

    Seriously though, you've got the idea, just look up the readdir function (perldoc -f readdir might work for you or try readdir) and you'll have what you need.

    non-Perl: Andy Ford

Re: check for directory no mather what case (linux)
by tuxz0r (Pilgrim) on Oct 19, 2007 at 20:16 UTC
    Just toying around with the possibilites, I don't think you necessarily need opendir or readdir. Using glob() might do the trick, as long as you can get the base (root) of the directory you're checking for, for example:
    $dir = "/home/Bob/XXX"; $dir =~ m/(\/.*)\/([^\/]+)\/?$/ and ($base,$dirname) = ($1,$2); mkdir $dir unless scalar grep(m/$dirname/i, glob("$base/*"));
    Caveat, depending on the platform the slashes might trip you up (forward or backward), but in general it seems to work when I test it out on my Linux and AIX machines just fine. NOTE: Correction, I had pasted the code from the wrong test script - correct code above now