Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2003
    Posts
    31

    Unanswered: search file then read the file howto

    Bellow script search for "txt"and find the file
    I want to know how can I read the file

    use strict;
    use warnings;
    use File::Find;
    my $result;
    my $name = "txt";
    finddepth(\&wanted,"/myprog/perl/search_file");
    sub wanted {
    $result=$File::Find::name if $File::Find::name =~ $name
    }
    print "$result\n";

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Code:
    open(R_FILE,"<$result") or die "Can't read file $result: $!";
    my @lines_of_file = <R_FILE>;
    close R_FILE
    When opening the file you are creating a filehandle (in this case it is named R_FILE), then you decide what to do with the file read("<"), write(">") or both("+<").
    The term after "or die" is for exception-handling. $! tells you what went wrong if it isn't successful.
    In the second line you read all lines into an array. In each element of the array is one line of file.
    Lastly you have to close the filehandle.

    Sorry for my poor english. I hope you can understand what I want to tell you.

  3. #3
    Join Date
    Sep 2003
    Posts
    31

    search file then read the file howto

    The above script print the $result as

    /perl_prog/learn_perl/3/CUSTABCD Quarterly Q1-2004-Errors.xls

    What I want to do is to be able to split the above file and get CUSTABCD HOW do I modify my script

  4. #4
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    So you want the filename?? So test this code:
    Code:
    #! /usr/bin/perl
    
    use File::Basename;
    
    $basename = basename($result);
    print $basename;
    Does this work as you want?

    Otherwise describe your problem more detailed. The subject is named "read the file" so I guessed you want to get the content of your file.
    board.perl-community.de - The German Perl-Community

  5. #5
    Join Date
    Sep 2003
    Posts
    31

    read file and split the name

    May be I confused you sorry !! here is again
    The script bellow, search and print the path of the file as
    /myprog/perl/search_file/CUSTABCD Quarterly Q1-2004-Errors.xls
    OK
    Now what I need to do is split the file, the file name is
    (CUSTABCD Quarterly Q1-2004-Errors.xls) and print only CUSTABCD in this sample.

    use strict;
    use warnings;
    use File::Find;
    my $result;
    my $name = "Errors.xls";
    finddepth(\&wanted,"/myprog/perl/search_file");
    sub wanted {
    $result=$File::Find::name if $File::Find::name =~ $name
    }
    print "$result\n";

  6. #6
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Code:
    use strict;
    use warnings;
    use File::Find;
    use File::Basename;
    
    my $result;
    my $name = "Errors.xls";
    finddepth(\&wanted,"/myprog/perl/search_file");
    
    my $basename = basename($result);
    print $basename;
    print (split(/\s/,$basename))[0];
    
    sub wanted {
      $result=$File::Find::name if($File::Find::name =~ /$name/)
    }
    board.perl-community.de - The German Perl-Community

  7. #7
    Join Date
    Sep 2003
    Posts
    31
    The code generate Errors as follow;
    print (...) interpreted as function at search2.pl line 10.
    syntax error at search2.pl line 10, near ")["
    Execution of search2.pl aborted due to compilation errors.

  8. #8
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Code:
    use strict;
    use warnings;
    use File::Find;
    use File::Basename;
    
    my $result;
    my $name = "Errors.xls";
    finddepth(\&wanted,"/myprog/perl/search_file");
    
    my $basename = basename($result);
    print $basename;
    my $customer = (split(/\s/,$basename))[0];
    print $customer;
    
    sub wanted {
      $result=$File::Find::name if($File::Find::name =~ /$name/)
    }
    board.perl-community.de - The German Perl-Community

  9. #9
    Join Date
    Sep 2003
    Posts
    31

    get file name print customer name

    Thanx Reneeb, it is working

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •