Solved

Newbie Question : FileExists?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
help pulling data string using Awk 9 62
After Effects Random Number, but different each time 5 91
Sed question 2 68
Active DirectoryScript to change the Pager Field 1 48
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

911 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

16 Experts available now in Live!

Get 1:1 Help Now