CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Reading directory as per regex

 



rdugar
New User

Nov 8, 2011, 1:21 AM

Post #1 of 8 (1691 views)
Reading directory as per regex Can't Post

I have to read a dir as per regex like (abc|xyz)* i.e. file names starting with abc or xyz only.

In normal PERL dir read, if the dir. contains thousands of file then the read dir. command by default reads all the files and it take a lot of time. In my case there are 600000 files.

Is there any way to pass argument to read function.


wickedxter
User

Nov 8, 2011, 3:04 AM

Post #2 of 8 (1676 views)
Re: [rdugar] Reading directory as per regex [In reply to] Can't Post

are you reading the dir one at a time? b/c if not you might want to b/c slurping all that will consume a lot of memory and can slow the process once memory is low.


Code
 
opendir DIR, 'dir/to/somewhere/';
while(my $contents = readdir(<DIR>)){
#make sure its a file or folder
}
closedir(DIR);



(This post was edited by wickedxter on Nov 8, 2011, 3:08 AM)


rdugar
New User

Nov 8, 2011, 3:22 AM

Post #3 of 8 (1672 views)
Re: [wickedxter] Reading directory as per regex [In reply to] Can't Post

I am reading all the files in a specific directory.
The function
"opendir DIR, 'dir/to/somewhere/';"
reads all the files in the dir.
I just want to have files that satisfies the regex criteria in the file handle DIR.

Is this possible?

In Reply To


wickedxter
User

Nov 8, 2011, 3:55 AM

Post #4 of 8 (1666 views)
Re: [rdugar] Reading directory as per regex [In reply to] Can't Post

the opendir assigns a file glob to DIR the readdir does the work of reading the contents of the directory. Yes it possible to filter the files by regex supply some more details about what to filter... spectific file .doc or .html .txt???


BillKSmith
Veteran

Nov 8, 2011, 4:44 AM

Post #5 of 8 (1663 views)
Re: [rdugar] Reading directory as per regex [In reply to] Can't Post

Refer: perldoc -f glob
Good Luck,
Bill


rdugar
New User

Nov 8, 2011, 7:41 AM

Post #6 of 8 (1644 views)
Re: [wickedxter] Reading directory as per regex [In reply to] Can't Post

All the file matching the regex (abc|FRE)*.txt should be read.
Eg: abc123.txt
FREZ.txt

In Reply To


wickedxter
User

Nov 8, 2011, 9:51 AM

Post #7 of 8 (1635 views)
Re: [rdugar] Reading directory as per regex [In reply to] Can't Post


Code
 
my @matches;

opendir DIR, 'dir/to/where/ever';
while (my $file = readdir(DIR)){
my ($name,$type) = split(/\./,$file);
next if $type =~ /\w{3}/; #skip folders
next if $file eq "." or $file eq "..";

push @maches, $file if $name =~ /abc/; #saves the matcheing file if it matches abc
next;
}
closedir DIR;

print "found matches:\n";

for (@matches){
print "$_ \n";
}


this is one way of doing this....


Chris Charley
User

Nov 9, 2011, 8:59 AM

Post #8 of 8 (1598 views)
Re: [rdugar] Reading directory as per regex [In reply to] Can't Post


Code

Quote
Is there any way to pass argument to read function.


Not that I'm aware of.

The while loop (as of Perl 5.11.2) suggestion by Wickedxter uses the smallest amount of memory so may be the fastest solution.

Don't know Unix well, but it has the find function.


(This post was edited by Chris Charley on Nov 11, 2011, 7:57 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives