Solved

Newbie Question : FileExists?

Posted on 2001-09-10
5
147 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
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

685 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