CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
how to check if the file already exists?

 



chander1986
Novice

May 1, 2009, 8:39 PM

Post #1 of 9 (829 views)
how to check if the file already exists? Can't Post

I want to write a snippet to create a file say temp.txt. But, if the file already exists then first delete it and then create a new one.

I am not able to figure out how to go about it??


KevinR
Veteran


May 1, 2009, 11:39 PM

Post #2 of 9 (822 views)
Re: [chander1986] how to check if the file already exists? [In reply to] Can't Post

open the file for creating or overwriting:


Code
open(my $FILE,">","path/to/temp.txt") or die "$!";


if it does not exist it will be created, if it does exist, it will be overwritten with any new data.
-------------------------------------------------


chander1986
Novice

May 1, 2009, 11:43 PM

Post #3 of 9 (821 views)
Re: [KevinR] how to check if the file already exists? [In reply to] Can't Post

Thanks a lot kevin ...


KevinR
Veteran


May 1, 2009, 11:54 PM

Post #4 of 9 (820 views)
Re: [chander1986] how to check if the file already exists? [In reply to] Can't Post

You're welcome Smile
-------------------------------------------------


vikas.deep
User

May 2, 2009, 2:57 AM

Post #5 of 9 (818 views)
Re: [KevinR] how to check if the file already exists? [In reply to] Can't Post

Kevin R
Sir,
(As far as possible) I try to closely follow all your posts. In this case (how to check if file exists or not) I was wondering if we can use "defined" on the file handle. Lets us say what if the person doesn't wants to create a file if it didn't exist at the first place.
#!/usr/bin/perl
use strict;
use warnings;
{
open (my $FH, "new.pl") or die " $!";
if (defined $FH)
{
print " File EXISTS!";
}
}
Unimpressed
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


FishMonger
Veteran / Moderator

May 2, 2009, 6:09 AM

Post #6 of 9 (813 views)
Re: [vikas.deep] how to check if the file already exists? [In reply to] Can't Post

There no reason to use defined to check $FH

If the open call succeeds, then $FH will be defined, if it fails then the die statement kicks in and your if block never gets executed.

If you want, you could do a -e file test prior to the open statement, but it's rare that you'd need/want to do that test.


vikas.deep
User

May 2, 2009, 6:56 AM

Post #7 of 9 (809 views)
Re: [FishMonger] how to check if the file already exists? [In reply to] Can't Post

Sir,
My post was in reply to a query. After your response I rechecked the original query. Now there is a bit of ambiguity because the title of query "how to check if the file already exists?" means something different from the content of query "I want to write a snippet to create a file say temp.txt. But, if the file already exists then first delete it and then create a new one. "
If the user wants to create a file (to write) then of course
the way is open(FILEHANDLE, ">Filename"); but my code simply tells "how to check if the file already exists?". and not create one if it does not exist and It may be rare "you'd need/want to do that test" as you said but if the original qery poser wants it so what can be done . For the benefit of all you have also pointed out the -e file test.

Quote
Thnks for the illuminationSmile
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


FishMonger
Veteran / Moderator

May 2, 2009, 7:32 AM

Post #8 of 9 (807 views)
Re: [vikas.deep] how to check if the file already exists? [In reply to] Can't Post


Quote
but my code simply tells "how to check if the file already exists


No, that's not what your code does.

The open function can fail and execute the die statement even if the file exists, for example if you don't have read permission or the file is opened and locked by another process. The -e file test is what is used to do the exists check, not the open function and not the defined function. However, if the user neglected to test the return code of the open call, then an if block such as yours could be used to check the status/value of the filehandle, but if the filehandle is not defined would that be because the file doesn't exist or because of some other reason?


vikas.deep
User

May 2, 2009, 7:51 AM

Post #9 of 9 (804 views)
Re: [FishMonger] how to check if the file already exists? [In reply to] Can't Post

I got your point in case the file is there but one doesn't has a read permission or the file is locked open will fail and $fh will not be defined. I also see that the status of $fh variable is different from the status of file. Unimpressed
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives