Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why doesn't if (-e $file) work proper like?

Status
Not open for further replies.

greadey

Technical User
Oct 25, 2001
231
GB
Hi peeps,

I'm using this code;

$file = "true_existing_file";

if(-e $file) {

print "File exists\n";

} else {

print "No luck sucker\n";

}

the other way is;

open(F, $file) or die "Etc\n";

if(-e FILE) {

print "FILE exists\n";

} else {

print "No luck sucker\n";

}

Which ever way I try (Win NT) I always end up being the sucker despite providing a real and existing file name.

Any ideas?

cheers,

gready
 
I don't see any problem with the first example.. make sure the file you're checking for is in the same folder as the script. Otherwise, make sure the path is correct:

$file = "C:/TEMP/file.txt";

In the second one, if you want to create a file that doesn't already exist, you want to do this:

open(FILE, ">$file") or die "Can't open $file: $!";
if(-e FILE) {
....

hope this helps..

JT
 
JT your second code snippet will zero-byte and create the file whether it existed before or not.

To check a file exists, use the test '-e' as you have done. However, bear in mind that this will only return true if either the file you pass is fully pathed (absolute or relative) or exists in the current directory. If running via a web browser it'll be from where the web server process starts your script. In Apache this is the cgi-bin directory, whereas IIS will be in your document directory.

Barbie
Leader of Birmingham Perl Mongers
 
I'm using Tk::FileDialog to get the file name it returns something like;

/dir1/dir2/filename.ext.

I have then added "C:" to it to get

C:/dir1/dir2/filename.ext

and I still have the same problem.

On my Linux box at home this works with no problem at all.
 
from an XP cmd window:
Code:
C:\perl\eg>dir
 Volume in drive C is IBM_PRELOAD
 Volume Serial Number is F8D0-0ABA

 Directory of C:\perl\eg

28/01/2003  14:52    <DIR>          .
28/01/2003  14:52    <DIR>          ..
28/01/2003  14:52    <DIR>          aspSamples
28/01/2003  14:52    <DIR>          Core
02/05/2001  10:29                33 example.pl
28/01/2003  14:52    <DIR>          IEExamples
28/01/2003  14:55    <DIR>          PDK
02/05/2001  10:29               836 Readme.txt
28/01/2003  14:52    <DIR>          Windows Script Components
28/01/2003  14:52    <DIR>          Windows Script Host
               2 File(s)            869 bytes
               8 Dir(s)  26,690,920,448 bytes free

C:\perl\eg>perl -e &quot;print \&quot;works\n\&quot; if -e 'Readme.txt';&quot;
works

C:\perl\eg>perl -e &quot;print \&quot;works\n\&quot; if -e 'C:\perl\eg\Readme.txt';&quot;
works

C:\perl\eg>perl -e &quot;print \&quot;works\n\&quot; if -e 'C:/perl/eg/Readme.txt';&quot;
works

C:\perl\eg>perl -e &quot;print \&quot;works\n\&quot; if -e 'C:/perl/eg/nonexist.txt';&quot;

C:\perl\eg>


I use ActiveState perl 5.6.1, build 626 on XP (when I'm forced away from my linux box!)

I hope that this helps,


fish

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top