Solved

Newbie Question : FileExists?

Posted on 2001-09-10
5
151 Views
Last Modified: 2013-12-25
Hi.

How do I check to see whether a file exists or not.  I've tried opening a file for reading but it returns a value, even though the file does not exist.

I've tried using this...

            if (-e "$name.txt") {
              do stuff
            } else {
              do other stuff
            }

...but it always acts as if the file given by $name.txt exists, even though I know it doesn't.

Thanks in advance,

John.
0
Comment
Question by:Jaymol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Accepted Solution

by:
holli earned 50 total points
ID: 6471525
that's strange. the above should work. check your code again.

if you can't find any error post your code.
put a print "\nexists file: $name\n"; statement to see if $name is what you expect.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6472382
Jaymol,

"..How do I check to see whether a file exists.."

"..    if (-e "$name.txt") {.."

Can you please post the entire code here?

What OS are you on (Win* or UN*X)?

Please provide as much  more detail as you can.

This will help you get a more accurate answer, faster.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6472546
what is    "$name.txt"   ?
do you mean
    $name
or
    "name.txt"
0
 
LVL 6

Author Comment

by:Jaymol
ID: 6473394
holli - thanks for the suggestion.  I know that the $name variable is correct as there are another 5 subroutines that all work.  I did try it though, and it gave me what I expected it to.  (I create a filename from a passed variable from HTML.)

maneshr - I'm afraid I can't post any code here as it's something work related and I'd get in a little bit of strife if I did.  Also, the script will be running on a unix server.

ahoffmann - As I said to holli, the $name variable is a filename I create from a variable passed from HTML.

I hope this helps you come up with some more suggestions.  I know the code should work as I got it directly from perl documentation, but, for some reason, it don't want to.

Thanks again, and I look forward to your suggestions.

John.
0
 
LVL 6

Author Comment

by:Jaymol
ID: 6473411
Thanks holli.

I am such a donkey!  I was declaring the $name variable and adding the ".txt" extension to it at the beginning of the script.  Then, when it came to run the subroutine that checks to see if the file existed, it was appending ".txt" again.

I'm afraid that I was being rather silly.

Thanks for the help everyone.

John.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question