Solved

read and filter directory contents

Posted on 2008-06-22
4
644 Views
Last Modified: 2008-06-23
I use the following code to read a list of contents from a directory

[code]
@files = <$in/*>;
      foreach (@files) {
        }
[/code]

I know it's easy to filter one file type by placing the extension after the * but I would like to filter a list of file types but cannot figure out how to do it.

i.e. filter a list of picture files (jpg, gif, psd, etc).
0
Comment
Question by:NorthReptile
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:jerzy_sz
ID: 21840569
You can use regular expression to filter your "files" array.
@files = grep ( /jpg|gif|psd/i, <$in/*>);

Open in new window

0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 21840941
@files = <$in/*.{jpg,gif,psd,etc)}>;
0
 

Author Comment

by:NorthReptile
ID: 21841152
Thanks, Ozo's solution was exactly what I was after. I am sure the grep works just as fine too but i prefer the syntax of the other.

thanks to you both.
0
 
LVL 1

Expert Comment

by:jerzy_sz
ID: 21847441
Note, that second solution is case sensitive.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question