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