Perl gets the file under the directory or contains a subdirectory under the file

Source: Internet
Author: User

1 my $dir="d:\\my_work\\temp";2 my @files= ();3 4 #gets the file in the given directory5 @files= Get_dir_files ($dir); 6 7 #gets the file under the given directory and subdirectories8 @files= Get_dir_files_x ($dir);9 Ten #gets the files under the directory and subdirectories, and returns the results that contain the path One Subget_dir_files_x{ A     my($dir _p) =@_; -     my @files= (); -     my @dirs= ($dir _p); the      die "error $basedir: $!" unless(-D$dir _p); -      while(@dirs){ -         $dir=$dirs[0]; -         OpendirFolder$dir|| die "Can not open this directory"; +         my @filelist=Readdirfolder; -         Closedirfolder; +         foreach(@filelist) { A             Next if($_eq"."Or$_eq".."); at             my $f= Path_join ($dir,$_); -             Push(@dirs,$f)if(-D$f); -             Push(@files,$f)if(-F$f); -         } -         Shift(@dirs); -     } in     return @files; - } to  + #get file under directory - Subget_dir_files{ the     my($dir) =@_; *     my @files= (); $     OpendirDIR,$dirOr die "Can not open this dir";Panax Notoginseng     my @file_list=ReaddirDIR; -     ClosedirDIR; the     foreach(@file_list) { +         Push(@files,$_)if(-F Path_join ($dir,$_)); A     } the     return @files; + } -  $ #get a path merge operation, automatically with a slash, slash and no slash can be combined $ Subpath_join{ -     my($path 1,$path 2) =@_; -     my $path=$path 1."\\".$path 2; the     $path=~s/[\\|\/]+/\\/G; -     return $path;Wuyi}

Perl gets the file under the directory or contains a subdirectory under the file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.