Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a list of files (recursively)

Status
Not open for further replies.

moroshko

Programmer
May 24, 2008
14
IL
Hello experts !

What is the most efficient way to get a list of all file names (a full path) in a certain directory ?
This should work recursively and include only files (not directories).

Thanks in advance !
 
What OS are you using?
One possibility:
Code:
sub dir_scan {
   my($curr_path) = $_0 ;
   my($local_file) ;
   if (-f $curr_path) {
      # global var here
      $dir_list{'file'}{$curr_path) = 1 ;
   } elsif (-d $curr_path)
      $dir_list{'dir'}{$curr_path) = 1 ;
      if (opendir(DH, $curr_path))) {
         foreach $local_file (grep !/^\.\.?$/, readdir DH) {   
            &dir_scan($local_file);
         };
      } else {
         # Save off the usual $!, $? $@ error vars
         # here as appropriate.
         #$dir_list{'dir_err'}{$curr_path) = <err data>
      };
      # Personal pref - I like putting this out here vs.
      # under the 'if-true' - Mostly subjected to Win env
      # and sometimes this sort of thing makes the difference
      # between getting a call at 2am when something obscure
      # is locked up.
      closedir DH ;
   } else {
      # Ends up being an interesting way to investigate
      # your particular OS.
      $dir_list{'other'}{$curr_path) = 1 ;
   };
   return 1;
};
 
Pardon my lack of regard above: I should have noted that the code within the "grep ... readdir DH" statements should be accredited to the Camel book.
 
... sheesh found newbie error above:

Code:
         foreach $local_file (grep !/^\.\.?$/, readdir DH) {   
            &dir_scan("[b]$curr_path/[/b]$local_file");
         };
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top