Win has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How do I read files in a directory and not directories?
by davidrw (Prior) on Mar 10, 2006 at 17:16 UTC
    just that partial snippet looks ok .. two general ideas:

    You can always put next unless -f $job; in the foreach -- that will exclude directories (see -X).

    A File::Find::Rule approach:
    my @files = File::Find::Rule->file() ->name(qr/^.{36}\.txt$/) ->maxdepth(1) ->in("G:/Perl_program/Input"); my @tags = map { /(.{36})\.txt$/ ? $1 : undef } @files;

      my @tags = map { /(.{36})\.txt$/ ? $1 : undef } @files;
      should be
      my @tags = map { /(.{36})\.txt$/ ? $1 : () } @files;
      or else you'll end up with a bunch of undefs in @tags.

      >perl -le "print 0 + map undef, 1..2 2 >perl -le "print 0 + map { () } 1..2 0
        yeah, good point. i actually almost wrote it as my @tags = grep {defined $_} map { ... } @files; (though i like the ? $1 : () much better).. left it out because, in theory, it should never not match, and if it does fail to match, perhaps (no clue if he does or not) OP wants to trap those cases ...
        foreach my $tag (@tags){ die "found a blank" unless defined $tag; .... }
        (though i guess in that case you can't, very easily, tie the failing tag back to an offending file ... hm.)
Re: How do I read files in a directory and not directories?
by ikegami (Patriarch) on Mar 10, 2006 at 17:51 UTC

    Use -d to check if it's a dir. Better yet, use -f to check if it's a normal file.

    my $path = File::Spec->catdir($dir, $file); next unless -f $path;
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How do I read files in a directory and not directories?
by Fletch (Bishop) on Mar 10, 2006 at 17:44 UTC

    Considering it's incomplete code calling a non-standard routine which does who-knows-what, you're pretty much going to get no useful answers.

    Well, you'll get useful answers like the above pointing you at File::Find::Rule; but you'll ignore them and blindly rage on and post 3 more all but identical questions trying to figure out why what you've not shown us isn't meeting your unspecified personal definition of "working"-ness.

    And despite the fact that you'll get several more people replying to each of them pointing you at How (Not) To Ask A Question YET AGAIN you'll still persist in posting poorly formulated, incomplete questions.

    If only PM had /ignore for SoPW . . .

Re: How do I read files in a directory and not directories?
by blue_cowdawg (Monsignor) on Mar 10, 2006 at 23:01 UTC
        when there is a folder within the directory

    Consider the following:

    | hand waving on how we got here... | opendir(DIR,".") or die $!; while(my $name=readdir(DIR)){ next if -d $name; # takes care of . and .. as well | do something with file here... }
    Certainly not the only solution.. but one of...

    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
    A reply falls below the community's threshold of quality. You may see it by logging in.