Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Search a file

0 views
Skip to first unread message

backg...@gmail.com

unread,
Sep 4, 2008, 1:42:44 PM9/4/08
to
Hello

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

TIA

David Precious

unread,
Sep 4, 2008, 2:07:20 PM9/4/08
to
backg...@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

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

Cheers

David Precious

Paul Lalli

unread,
Sep 4, 2008, 2:08:11 PM9/4/08
to
On Sep 4, 1:42 pm, backgoo...@gmail.com wrote:
> 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";
}

Jürgen Exner

unread,
Sep 4, 2008, 2:09:09 PM9/4/08
to
backg...@gmail.com wrote:
>Does anyone know how to find out whether a file exists or not?

perldoc -f -e

jue

backg...@gmail.com

unread,
Sep 4, 2008, 3:53:34 PM9/4/08
to
On Sep 4, 2:07 pm, David Precious <pinkm...@preshweb.co.uk> wrote:

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

Sherm Pendley

unread,
Sep 4, 2008, 4:19:00 PM9/4/08
to
backg...@gmail.com writes:

> 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

Jürgen Exner

unread,
Sep 4, 2008, 4:22:53 PM9/4/08
to
backg...@gmail.com wrote:

>> backgoo...@gmail.com wrote:
>> > Does anyone know how to find out whether a file exists or not?
>
>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. Sinan Unur

unread,
Sep 4, 2008, 4:28:46 PM9/4/08
to
backg...@gmail.com wrote in news:8e4ae12c-3235-4112-a476-2104557c2936
@p31g2000prf.googlegroups.com:

> 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/

0 new messages