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

Given a path... say,
c:\mypath\myfile and
c:\mypath
, how would one identify whether it is a directory or a file?... Seems rather simple, but couldn't figure a way out...
Any suggestions would do.
THanks...Rupz.

Plz. could anyone also help on my earlier request? node:250821? THanks!!!


we're born with our eyes closed and our mouths wide open, and we spend our entire life trying to rectify that mistake of nature. - anonymous.

Replies are listed 'Best First'.
Re: directory or file?
by The Mad Hatter (Priest) on Apr 16, 2003 at 13:07 UTC
    To test if is a directory, use the -d file test operator. For example, this code would print "foo" if /bar was a directory.
    print "foo\n" if (-d "/bar");
    To test if something is a file, use the -f file test operator.
Re: directory or file?
by hiseldl (Priest) on Apr 16, 2003 at 13:07 UTC

    my $path = "/path/to/file_or_dir"; if (-f $path) { # this is a file } if (-d $path) { # this is a directory }

    --
    hiseldl
    What time is it? It's Camel Time!

Re: directory or file?
by pfaut (Priest) on Apr 16, 2003 at 13:08 UTC

    Use the filetest operators which are described in perldoc perlfunc. -f FH or -f "filename" returns true for a normal file. -d "filename" returns true for a directory. There are many others that return various pieces of information about the specified file.

    90% of every Perl application is already written.
    dragonchild

      perldoc -f -X will bring up the specific entry from perlfunc on the test operators. See also perldoc -f stat, or perldoc File::stat for the OOPy in the audience.

Re: directory or file?
by Futant (Acolyte) on Apr 16, 2003 at 13:10 UTC
    I would use -f example: do $file if(-f $file);