Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Regular expression to list all files of type in folder

by Phinix (Acolyte)
on Dec 05, 2012 at 21:20 UTC ( #1007400=perlquestion: print w/ replies, xml ) Need Help??
Phinix has asked for the wisdom of the Perl Monks concerning the following question:

Having a bit of a strange issue with a regular expression I was using to list all files of type .jpg in a folder. It seems it is missing files that are named very similar to the file before it.

Here is the code:

#!/usr/bin/perl use strict; use warnings; my $dir = 'C:/Some/Random/Folder/tmp/bin'; opendir (DIR, $dir); open(DATA, ">file.txt"); while (my $file = readdir(DIR)) { # Use a regular expression to find files ending in .jpg next unless ($file =~ m/\.jpg$/); print DATA "C:\\Some\\Random\\Folder\\perl\\bin\\$file\n"; } close(DATA);
The problem is I have a file named "test.jpg" and another called "testl.jpg" and the expression only picks up the first one. Other than that, it works fine.

Comment on Regular expression to list all files of type in folder
Download Code
Re: Regular expression to list all files of type in folder
by 2teez (Priest) on Dec 05, 2012 at 21:40 UTC

    try this: next unless ($file =~ m/.+?\.jpg$/);
    Please, always check the return of your open function like so: open ... or die "can't open file: $!"; or use autodie

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      Why would that regex match if the other doesn't?

        The OP said the other does match. Please, check the OP last statement.
        "..The problem is I have a file named "test.jpg" and another called "testl.jpg" and the expression only picks up the first one. Other than that, it works fine..."

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
      UPDATE: I addressed my answer to the wrong node. 2teez initial node was modified after runrig pointed out the open issue. So please read this as if addressed, as intended, to 2teez. </UPDATE>
      It's highly impolite to modify your (mistaken) post without acknowledging the update.

      <UPDATE3 with strikeout in light of the reply, ack'ing that the sequence he asserts might have coincided nearly simultaneously with what I saw.> It's even more so when you ADD a suggestion made by another Monk that was not included in your own original, inaccurate (and apparently, untested) advice. <RESUMING UPDATE3 NOTE: I continue to held the view that "your own original, inaccurate (and apparently, untested) advice...." was ill-considered.</RESUMING...>

      - -, and dog votes be damned.

      P.S. (and yes, this is an update, too) It's also wise to test your code (and that to which you're responding) to make sure yours
          a) does what you thought it should do...
              and
          b) that your solution actually solves or illuminates OP's problem.

        Hi ww,
        I cannot be more surprise than anything else than your comment, as regard my initial OP. After I came to this thread.
        Indeed had posted a solution, which stated as this Use this: next unless ($file =~ m/.+?\.jpg$/);
        then added this: Please, always check the return of your open function like so: open ... or die "can't open file: $!"; or use autodie
        Only for me to go back into the "thread" to see that runrig had posted this:...Also, unless you're using autodie, you should check the status of open and opendir.

        On my honor, I didn't update my regex used in my initial OP, neither did I updated my post after seeing runrig post.
        Probably, the only mistake I made was not to have put a word out that I wasn't updating my post after seeing that of runrig.
        Infact, to prove that, why was opendir not included in my "update"? If I had seen that of runrig before writing mine.

        Please, if you may, check my other post before now. I had shown Update, when my initial post had been updated. Acknowledged, when the original thought was not mine, been grateful with an update of the monks name when I was corrected on some post.
        I feel so hurt here, because I had been called a "thief" but in a polite way.
        I believe your assessment of what went down here is WRONG, please.

        Update:
        Sorry, I didn't include this: Except for changing the usage of "Use this" to "try this:" I didn't update anything else.
        And I did that because I felt that using "Use this", was not polite enough.
        And if I had wanted to lie about this, I would not be including this.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: Regular expression to list all files of type in folder
by runrig (Abbot) on Dec 05, 2012 at 21:47 UTC

    Did you put a print "$file\n" before the "next unless" to see if the file even gets returned by readdir?

    Also, unless you're using autodie, you should check the status of open and opendir.

      I skipped the "or die" checks because this is just a quick hack to populate the files I need to include in my PARPacker build list. I agree though this should always be done.

      I just checked on your suggestion and you are right, readdir is the one not picking up the similarly named files, though I have absolutely no idea why.

      There are no strange files present. No file named "0" or any weird symbols (not that it should matter). It is basically just a folder full of a couple hundred .jpg files with simple names.

      I'm using Perl 5.12 so the use in a while loop should be no problem.
        what do you get from

        print join "\t", <$dir/*>

        maybe you confused the path?

        'C:/Some/Random/Folder/tmp/bin' ne "C:\\Some\\Random\\Folder\\perl\\bin\\"

        Cheers Rolf

Re: Regular expression to list all files of type in folder
by Kenosis (Priest) on Dec 05, 2012 at 21:59 UTC

    In case it's a case issue, you could try:

    $file =~ m/\.jpg$/i
Re: Regular expression to list all files of type in folder
by bart (Canon) on Dec 05, 2012 at 23:03 UTC
    Is there perhaps a file called "0" in the directory? That would make the loop prematurely break off.
      > Is there perhaps a file called "0" in the directory? That would make the loop prematurely break off.

      Thankfully this doesn't happen!!!

      (I was ready to complain about rotten implementations...=)

      Probably saved by one of these special magic behaviors of while

      UPDATE:here it is

      from perlop#I/O-Operators

      The following lines are equivalent:
      while (defined($_ = <STDIN>)) { print; } while ($_ = <STDIN>) { print; } while (<STDIN>) { print; } for (;<STDIN>;) { print; } print while defined($_ = <STDIN>); print while ($_ = <STDIN>); print while <STDIN>;

      This also behaves similarly, but avoids $_ :

      while (my $line = <STDIN>) { print $line }

      In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see whether it is defined. The defined test avoids problems where line has a string value that would be treated as false by Perl, for example a "" or a "0" with no trailing newline. If you really mean for such values to terminate the loop, they should be tested for explicitly:

      doesn't mention readdir but I think it's a similar case (and a documentation hole)

      Cheers Rolf

        B::Deparse thinks so :)
        $ perl -MO=Deparse,-p -le " opendir BLAH,'.'; while(readdir BLAH){prin +t;}" BEGIN { $/ = "\n"; $\ = "\n"; } opendir(BLAH, '.'); while (defined(($_ = readdir(BLAH)))) { print($_); } -e syntax OK

        Mmh, i tried to reproduce it exactly as described by Phinix and i get the files. Even when i add other files i get them. What do i miss?

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1007400]
Approved by blue_cowdawg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2014-09-23 19:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (239 votes), past polls