?
Solved

Newbie Question : FileExists?

Posted on 2001-09-10
5
Medium Priority
?
152 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 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses

801 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