How can I list files under a directory with a specific name pattern using Perl?

后端 未结 6 877
长发绾君心
长发绾君心 2021-01-07 12:15

I have a directory /var/spool and inside that, directories named

a  b  c  d  e  f  g  h i  j  k  l  m  n  o  p q  r  s  t  u  v  x  y z

An

6条回答
  •  春和景丽
    2021-01-07 12:59

    Try this:

    sub browse($);
    
    sub browse($)
    {    
        my $path = $_[0];
    
        #append a / if missing
        if($path !~ /\/$/)
        {
            $path .= '/';
        }
    
        #loop through the files contained in the directory
        for my $eachFile (glob($path.'*')) 
        {
    
            #if the file is a directory
            if(-d $eachFile) 
            {
                #browse directory recursively
                browse($eachFile);
            } 
            else 
            {
               # your file processing here
            }
        }   
    }#browse
    

提交回复
热议问题