develooper Front page | perl.beginners | Postings from April 2002

RE: 2 questions

Thread Previous | Thread Next
Timothy Johnson
April 2, 2002 16:09
RE: 2 questions
Message ID:

To check if a file exists, you might be better off using the built-in file

if(-e $file){
   Do something...
   Do something else...

-----Original Message-----
From: Eric Plowe []
Sent: Tuesday, April 02, 2002 4:03 PM
To: Glenn Cannon
Subject: Re: 2 questions


to print the current directory name...try

#!/usr/bin/perl -w
use strict;
use Cwd;

$curr = cwd();
print "$curr\n";

easy enough

this is bit longer example.. you could prolly do it another way..but 
this is how *I* would do it
#!/usr/bin/perl -w
use strict;
use Cwd;

my $curr = cwd(); (sets $curr to the current directory the script is 
being ran from)
my @files = &getdirfiles();
my $nof = "the name of the file you're looking to see exists";

if (grep /^$nof$/, @files) {
    print "File $nof exists\n";
    $nof = '';
} elsif (!grep /^$nof$/, @files) {
    print "File $nof does not exsist";
    $nof = '';

sub getdirfiles {
opendir(CURRDIR, $curr) or die "Can't open directory\n";
@files = readdir(CURRDIR);
return @files;

hope those examples help...

On Tuesday, April 2, 2002, at 06:20 PM, Glenn Cannon wrote:

> Hi all,
> Couple of questions from a newbie...
> 1)  How can I print the current directory name?
> 2)  How can I check to see if a file I know the name of exists?
> Thx,
> Glenn
> --
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About