Solved

Newbie Question : FileExists?

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to dynamically set the form action using jQuery.
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 …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now