Re: Question.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



One correction, you will want to replace this line:
 
open(INF,"ls $input_dir|") or &error_msg("Cannot get directory listing for \'$input_dir\'.  Error: \'$!\'\n");
 
with this:
 
open(INF,"ls $input_dir|") or die("Cannot get directory listing for \'$input_dir\'.  Error: \'$!\'\n");
 
I forgot to remove the call to a custom error handling routine I usually use.  ;)
 
 
Tom
----- Original Message -----
From: Shockwave
Sent: Wednesday, November 23, 2005 10:29 AM
Subject: Re: Question.

Hi,
 
I'm not an expert Perl developer, but I may be able to help you.  Here's the way I've done it in the past:
 
----------------begin code sample------------------
 
#!/usr/bin/perl -w
 
use strict;
 
my $input_dir = '/path/to/files/';   # directory to search
my $fileext = '.lisa';                   # file extension to find
my $count = 0;                         # files found counter
my %files = ();                          # hash for file list
 
# open filehandle to read output from "ls" command
open(INF,"ls $input_dir|") or &error_msg("Cannot get directory listing for \'$input_dir\'.  Error: \'$!\'\n");
while(<INF>)
{
 chomp;   # drop line terminator
 if ($_ =~ m/$fileext$/) # match files ending in specified file extension
 {
  $files{$count} = $_;   # add entry to hash
  $count++;                # increment file counter
 }
close(INF);     # drop filehandle
 
exit(0);
 
----------------end code sample------------------
 
I hope this helps.
 
 
Tom
 
----- Original Message -----
Sent: Wednesday, November 23, 2005 9:59 AM
Subject: Question.

Hi,
I am a beginner perl developer.
I am writing a script for pasing all files in a directory with perticular suffix.
I use File::Find but I want to avoid searching in subdirectories. I tried to use File::Find::prune in "wanted" function. bit it did not work. So I use a heck
return unless ($File::Find::topdir eq $File::Find::dir); to avoid processing in sub directories. but this is an inefficient way. I want to limit search only to first directory level.
Please let me know the efficient way to do it. and any comments/modification on the code would also be appriciated.
Regards
Pankaj

sub wanted {
    ## dir tree Depth = 1
    return unless ($File::Find::topdir eq $File::Find::dir);
    $fileext= ".cgd";
    if ($_ =~ /$fileext$/) { # $_ contain file base name.
                             # $ sign at end of pattern looks for
                             # .lisa suffix

       $files[$Count] = "$File::Find::name";
       $Count++;
   }
}


--
Fedora-perl-devel-list mailing list
Fedora-perl-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/fedora-perl-devel-list


--
Fedora-perl-devel-list mailing list
Fedora-perl-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/fedora-perl-devel-list

[Index of Archives]     [Fedora Announce]     [Fedora Kernel]     [Fedora Testing]     [Fedora Legacy Announce]     [Fedora PHP Devel]     [Kernel Devel]     [Fedora Legacy]     [Fedora Maintainers]     [Fedora Desktop]     [PAM]     [Red Hat Development]     [Big List of Linux Books]     [Gimp]     [Yosemite Information]
  Powered by Linux