Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

directory or file?

by rupesh (Hermit)
on Apr 16, 2003 at 13:03 UTC ( #250879=perlquestion: print w/replies, xml ) Need Help??

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);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://250879]
Approved by robartes
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2021-05-18 01:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (171 votes). Check out past polls.

    Notices?