Solved

perl, how to find out if a file exist

Posted on 2011-09-29
10
279 Views
Last Modified: 2012-05-12
In perl, how do I check if a file exist?
#if file exist, print message
0
Comment
Question by:rgbcof
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 10

Accepted Solution

by:
jeromee earned 200 total points
ID: 36815628
Check this out: http://perldoc.perl.org/functions/-X.html

my $myfile = "/tmp/test.txt";
if( -e $myfile ) {
    print "$myfile exists\n";
} else {
    print "$myfile does not exist\n";

}

Open in new window

0
 

Author Comment

by:rgbcof
ID: 36815708
It works.  Thanks.
0
 
LVL 23

Assisted Solution

by:nemws1
nemws1 earned 50 total points
ID: 36815710
joremee is correct - you want the '-e' option.  The only change I'd make to his code is to put double-quotes around the file argument in line 2 (in case it has a space or other odd character):
if( -e "$myfile" ) {

Open in new window

0
 

Author Closing Comment

by:rgbcof
ID: 36815994
It works.  Thanks
0
 
LVL 10

Expert Comment

by:jeromee
ID: 36815996
Hi nemws1,
This script being a perl script, I don't believe that the double quotes are required.
Please correct me if I'm wrong.
0
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.

 
LVL 84

Expert Comment

by:ozo
ID: 36816974
the quotes are not required
0
 
LVL 10

Expert Comment

by:jeromee
ID: 36817162
Thanks Ozo.
I thought I was going crazy for a sec.
0
 
LVL 23

Expert Comment

by:nemws1
ID: 36817197
I should have been more clear - my apologies, jeromee.  No, quotes are not required for a single scalar argument.  The problem is that people get in the habit of doing it this way and then they write something like this:
 
$p="/home/user"
$f="datafile"
if (-e $p/$f) {
   .....
}

Open in new window

Which is obviously incorrect.  IMO, it is good coding habit (and also helps with readability) to put double quotes around files/paths.  Saves you many a headache.
0
 
LVL 84

Expert Comment

by:ozo
ID: 36817265
on the other hand, if people get in the habit of using unnecessary quotes
they may write something like this:

open $myfile,"/tmp/test.txt" or die;
if( -e "$myfile" )

which is not so obviously incorrect.
0
 
LVL 10

Expert Comment

by:jeromee
ID: 36817298
No apologies req'd nemws1.
Thanks anyway.
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
Syslog text file into database or into .CSV 19 110
Perl script to parse log and delete the file 17 172
Writing a parser for java language 4 71
combine multiple lines 2 44
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

12 Experts available now in Live!

Get 1:1 Help Now