Does anyone know how to find out whether a file exists or not?
TIA
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
if (-e 'file.txt') {
print "file.txt exists\n";
} else {
print "file.txt does not exist\n";
}
perldoc -f -e
jue
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
> 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--
--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
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
> On Sep 4, 2:07 pm, David Precious <pinkm...@preshweb.co.uk> wrote:
>> backgoo...@gmail.com wrote:
>> > Hello
>>
>> > Does anyone know how to find out whether a file exists or not?
>>
>> if (-f $filename) {
>> # $filename is a file which exists
>>
>> }
>>
>> Full list of tests:http://perldoc.perl.org/functions/-X.html
...
> 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 <1u...@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/