Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if a file exists

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
Hi Guys
Simple but annoying problem.
How do I test to see if a particular file exists on the server? OPEN does a sort of test but creates the file if it doesn't exist.
Thanks
Keith
 
Try something like:

Code:
my $filename = "/usr/bin/perl";
-e $filename or warn "Perl is not installed correctly";
 
Thanks
Is the -e switch, PERL's EXIST operator?
Keith
 
Yes, there are a number of file test operators - -w for a writeable file, -r for readable and so on. No doubt there's a complete list somewhere in the perl documentation.
 
Thanks for the replies.
Are these just command line switches or are they script based. My problem is that I am asking the questions from work so have no way of testing.
I am writing a Shopping Cart and each product has its own small picture. Some of the products have enlarged pictures which have the same name as the small pictures but reside in a different directory.
All I want to check for is the existence of a picture in the "large pic" directory. Other laguages I program have a specific 'EXIST' command or something similar.
Thanks
Keith
 
Code:
 if (-e "/path/to/file") {
   print "File exists";
 } else {
   print "Oh no it doesn't";
 }

HTH
--Paul
 
They are not command line switches!

"perl -e" does something quite different.

The code in my first post could be used as it is to form part of a perl program.
 
To expand slightly the file operators are generally unary boolean operators. i.e. they take a single argument (a scalar which evaluates to a filename or filehandle) and return true or false.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top