Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Faster Method for Gathering Data

by crouchingpenguin (Priest)
on Jul 31, 2003 at 13:18 UTC ( #279558=note: print w/ replies, xml ) Need Help??


in reply to Faster Method for Gathering Data

Sure, you can use Inline::C. Here is an example :

#!/usr/bin/perl #use strict; #use warnings; use Inline C; if( scalar(@ARGV) < 2 ){ print "Usage: $0 [directory] [file extension]\n"; exit 1; } my $count = count_file_extension_matches(@ARGV); print $count,"\n"; __END__ __C__ /* * recursively search for a file extension * within the directory given. * */ int count_file_extension_matches(const char* dirname,const char* exten +sion){ DIR *tmp = opendir(dirname); struct dirent *file; struct stat file_stat; int total_count = 0; char* result; if( tmp != NULL ){ if( chdir(dirname) == -1 ){ printf("chdir failed\n"); return 0; } while( (file = readdir(tmp)) ){ /* make sure we don't have . or .. */ if( (strcmp(file->d_name,"..")) == 0 || (strcmp(file->d_na +me,".") == 0) ) continue; /* setup file_stat struct */ if( (stat(file->d_name,&file_stat)) == -1) continue; /* if its a directory, recurse */ if( S_ISDIR(file_stat.st_mode) ){ total_count += count_file_extension_matches(file->d_na +me,extension); if( chdir("..") == -1 ){ printf("chdir .. failed\n"); return 0; } continue; } /* only look at regular files */ if( S_ISREG(file_stat.st_mode) ){ /* continue unless our extension is found within the f +ilename */ if( ( result = strstr(file->d_name,extension) ) == NUL +L ) continue; /* result should match extension, continue otherwise * +/ if( strcmp(extension,result) != 0 ) continue; total_count += 1; } } closedir(tmp); }else{ printf("Error opening directory: %s\n",dirname); exit(1); } return total_count; }

cp
----
"Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."


Comment on Re: Faster Method for Gathering Data
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2015-07-31 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls