Search a file

P

Paul Lalli

Does anyone know how to find out whether a file exists or not?

if (-e 'file.txt') {
print "file.txt exists\n";
} else {
print "file.txt does not exist\n";
}
 
B

backgoodoo

if (-f $filename) {
     # $filename is a file which exists

}

Full list of tests:http://perldoc.perl.org/functions/-X.html

A couple of seconds Googling probably would have found you the answer.

Cheers

David Precious

Let me clarify my question again.

I want to search a file I do not know where it is but know its name
such as "text.exe".
So I want to check whether or not it is under c:\program file and sub
folders.

TIA
 
S

Sherm Pendley

Let me clarify my question again.

Not that it matters, but this seems more like an entirely different
question than a clarification of the original. :)
I want to search a file I do not know where it is but know its name
such as "text.exe".

Have a look at the File::Find module.

sherm--
 
J

Jürgen Exner

I want to search a file I do not know where it is but know its name
such as "text.exe".

Don't you think those two requirements are quite different?

Anyway, the File::Find module has functions to search for the location
of a given file.

jue
 
A

A. Sinan Unur

(e-mail address removed) wrote in @p31g2000prf.googlegroups.com:
....

Let me clarify my question again.

You did not ask a clear question to begin with and this is the first
time you are clarifying the question, so the 'again' above is not
necessary
I want to search a file I do not know where it is but know its name
such as "text.exe".
So I want to check whether or not it is under c:\program file and sub
folders.

perldoc File::Find

I also like:

http://search.cpan.org/~texmec/File-Find-Iterator-0.4/

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;
use File::Find::Iterator;
use File::Spec::Functions qw( canonpath );

my $find = File::Find::Iterator->create(
dir => [ $ENV{TEMP} ],
filter => sub { /t\.pl\z/ },
);

while ( my $found = $find->next ) {
print "$found\n";
}

__END__




--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

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

Staff online

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top