Search a file

Discussion in 'Perl Misc' started by backgoodoo@gmail.com, Sep 4, 2008.

  1. Guest

    Hello

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

    TIA
    , Sep 4, 2008
    #1
    1. Advertising

  2. 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
    David Precious, Sep 4, 2008
    #2
    1. Advertising

  3. Paul Lalli Guest

    On Sep 4, 1:42 pm, 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";
    }
    Paul Lalli, Sep 4, 2008
    #3
  4. wrote:
    >Does anyone know how to find out whether a file exists or not?


    perldoc -f -e

    jue
    Jürgen Exner, Sep 4, 2008
    #4
  5. Guest

    On Sep 4, 2:07 pm, David Precious <> wrote:
    > 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


    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
    , Sep 4, 2008
    #5
  6. 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
    Sherm Pendley, Sep 4, 2008
    #6
  7. wrote:
    >> 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
    Jürgen Exner, Sep 4, 2008
    #7
  8. wrote in news:8e4ae12c-3235-4112-a476-2104557c2936
    @p31g2000prf.googlegroups.com:

    > On Sep 4, 2:07 pm, David Precious <> wrote:
    >> 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 <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, Sep 4, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TGFrc2htaSBOYXJheWFuYW4uUg==?=

    Google search result like site search!! How?

    =?Utf-8?B?TGFrc2htaSBOYXJheWFuYW4uUg==?=, May 5, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    673
    Lucas Tam
    May 6, 2005
  2. Andy
    Replies:
    1
    Views:
    357
    Jack Klein
    Nov 25, 2003
  3. Anand Pillai

    String search vs regexp search

    Anand Pillai, Oct 12, 2003, in forum: Python
    Replies:
    10
    Views:
    593
    Anand Pillai
    Oct 15, 2003
  4. mason66
    Replies:
    0
    Views:
    422
    mason66
    Jul 27, 2006
  5. Abby Lee
    Replies:
    5
    Views:
    400
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page