Recurse through directory and perform action on file


/ Published in: Perl
Save to your folder(s)

This is used to recurse through a directory and perform some action on each file


Copy this code and paste it in your HTML
  1. #!/user/bin/perl -w
  2.  
  3. use strict;
  4. use File::Find;
  5.  
  6. my @DIRLIST = ("c:\\scratch", "c:\\sukanta\\scratch");
  7.  
  8. my @table_list;
  9.  
  10.  
  11. # Returns the SBRRES_* tables, there can be duplicate tokens
  12. sub get_tables_from_process {
  13. # read the input file
  14. open(FILE, $_) or die "Unable to open file $!";
  15. my @tokens;
  16. while(<FILE>) {
  17. push @tokens, split(" ");
  18. }
  19. close(FILE);
  20. map ($_ = uc($_), @tokens);
  21. @tokens = grep(/\bSBRRES_.*\b/, @tokens);
  22. return @tokens;
  23. # find the unique tables
  24. }
  25.  
  26.  
  27.  
  28. #
  29. # This function will be called for each file by the find function
  30. #
  31. sub process_file {
  32. my $fname = $_;
  33. if (-e $fname && !-d $fname && lc($fname) =~ /.*\.process/) {
  34. push @table_list, get_tables_from_process($fname);
  35. }
  36. }
  37. find(\&process_file, @DIRLIST);
  38.  
  39. my %unique = ();
  40. foreach my $item (@table_list) {
  41. $unique{$item} ++;
  42. }
  43.  
  44. @table_list = sort(keys %unique);
  45.  
  46. foreach my $item(@table_list) {
  47. print $item, "\n";
  48. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.