file open question

R

r_stringer66

Hello,

I want to write a simple perl script which checks to see if a given
file exists if so it will do one thing and if not something else, kind
of like:

if( fileexists)
do step1
else
do step2

So far I looked at perl's open function but it does not seem to return
a boolean in order to indicate of the file was opened successfully or
not.

Thanks in advance for any ideas you might have.

Regards,
Rob
 
B

Bart Van der Donck

(e-mail address removed) wote:
I want to write a simple perl script which checks to see
if a given file exists if so it will do one thing and if
not something else, kind of like:

if( fileexists)
do step1
else
do step2

So far I looked at perl's open function but it does not
seem to return a boolean in order to indicate of the file
was opened successfully or not.

I think you are looking for something like this:

$file = "/usr/me/file.jpg";
if (-e $file) {
print "$file exists";
} else {
print "$file does not exist";
}
 
A

A. Sinan Unur

(e-mail address removed) wrote in
I want to write a simple perl script which checks to see if a given
file exists if so it will do one thing and if not something else,
kind of like:

if( fileexists)
do step1
else
do step2

So far I looked at perl's open function but it does not seem to return
a boolean in order to indicate of the file was opened successfully or
not.

It seems like you missed

perldoc -f open

On the other hand, depending on what step1 and step2 are, this may not
be the right way to go about things, *especially* if step2 includes
creating the file whose existence you checked.

See

perldoc -q lock

Sinan
 
A

A. Sinan Unur

[ snipped ]

Please choose one posting address, and stick with it, to make it easier to
add you to my killfile.

I do not want to add everyone posting with X-No-Archive set (although I
see Tad's point in that regard).

Sinan
 
J

Jürgen Exner

I want to write a simple perl script which checks to see if a given
file exists if so it will do one thing and if not something else,

"perldoc -f -X" and pay particular attention to the "-e" test.
So far I looked at perl's open function but it does not seem to return
a boolean in order to indicate of the file was opened successfully or
not.

Why do you think so? Did you check the documentation for the functions you
are using?
From "perldoc -f open":
Open returns nonzero upon success, the undefined value
otherwise. [...]
To me this sounds very much like what you were looking for in your last
paragraph.

However, please notice that there is a significant difference between what
you were asking for in the first paragraph (file exists) and what you are
asking for in the last paragraph (file can be opened for whatever).

jue
 
J

Jürgen Exner

[Please do not top post]
[Please do not blindly fullquote]
On 15 May 2005 10:03:20 -0700, (e-mail address removed)
if( -f "filename.txt" ) {

Why are you testing for plain file if the OP wants to know if a file exists?

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top