#!/usr/bin/perl
#
# 这个是我用来遍历目录的脚本
#
#
#
#
use warnings; #启用代码告警
use strict; #启用严格的代码审查
use diagnostics; #启用详细的代码警告说明
use Cwd; #启用当前目录模块
my $cmt_pgm = 0 ;
my $proj_dir = getcwd(); #获得运行当前脚本所在的目录
$proj_dir = "dir_1";
#print $proj_dir."\n";
if( -d $proj_dir ){
print "$proj_dir is a directory!\n";
}else{
print "$proj_dir is not a directory!\n";
}
print "The current Project directory is :$proj_dir\n";
opendir PROJ_DIR, $proj_dir or die "Can not open the $proj_dir!\n Err $!";
my @proj_dir_list = readdir(PROJ_DIR);
shift @proj_dir_list;
shift @proj_dir_list;
#foreach my $current_discriptor ( @proj_dir_list ) {
# print " The current file discriptor is: $current_discriptor\n";
# if( -d $current_discriptor ){
# print "$current_discriptor is a directory!\n" if $cmt_pgm;
# if( dir_is_end($current_discriptor) eq 1 ){
# print "$current_discriptor is the end dir!\n";
# }else{
# print "$current_discriptor is not the end dir!\n";
# }
# }
#}
print getcwd()."\n";
dir_is_end($proj_dir);
print getcwd()."\n";
trace_all_end_dirs($proj_dir);
sub trace_all_end_dirs{ #递归搞定所有的最后一级目录,并在最后一级目录中完成文件操作
if( (@_ ne 1) ){
print "Func trace_all_end_dirs Err: You can only input one parameter and you must input one parameter!\n";
return -1;
}
if( (! -d $_[0]) ){
print "Func trace_all_end_dirs Err: Your input paramter is not a directory!\n";
return -1;
}
my $under_process_dir = $_[0];
#printf "1:当前文件夹为:%s\n",getcwd();
#printf "2:trace_all_end_dirs 参数为:$under_process_dir\n";
if( dir_is_end($under_process_dir) eq 1 ){
print"恭喜你! 当前处理的文件夹 $under_process_dir 已经是最后一级文件夹了!\n";
return 1;
}else{
opendir UNDER_PROCESS_DIR,$under_process_dir or die "Can't open DIR: $under_process_dir";
my @under_process_dir_fnl = readdir( UNDER_PROCESS_DIR ); # file node list
shift @under_process_dir_fnl;
shift @under_process_dir_fnl;
closedir UNDER_PROCESS_DIR;
chdir $under_process_dir;
foreach my $down_dir (@under_process_dir_fnl){
#printf "当前文件夹为: %s\n",getcwd();
#print "当前判断节点 $down_dir 是否为目录!\n" if 0;
if( -d $down_dir ){
#print "$down_dir为目录!\n";
#printf "当前文件夹为: %s\n",getcwd();
trace_all_end_dirs($down_dir);
#chdir ".."; #执行完了最后返回上一级文件夹
#printf "当前文件夹为: %s\n",getcwd();
}
}
chdir "..";
}
}
sub dir_is_end { # 判断传入的一个参数是不是已经是最后一级目录了
print ("------- Start of Func dir_is_end! -------\n") if $cmt_pgm ;
if( (@_ ne 1) ){
print "Func dir_is_end Err: You can only input one parameter and you must input one parameter!\n";
return -1;
}
if( (! -d $_[0]) ){
print "Func dir_is_end Err: Your input paramter is not a directory!\n";
return -1;
}
my $down_dir = $_[0];
#my $up_dir = $_[1];
print "down_dir is $down_dir\n" if $cmt_pgm;
#print "up_dir is $updir\n";
opendir DOWN_DIR,$down_dir;
my @down_fl = readdir( DOWN_DIR );
closedir DOWN_DIR;
shift @down_fl;
shift @down_fl;
print ("down_fl is @down_fl\n") if $cmt_pgm;
chdir $down_dir;
printf ("Current dir is %s\n",getcwd()) if $cmt_pgm;
foreach (@down_fl){
if( -d $_ ){
print "$_ is a directory!\n" if $cmt_pgm;
chdir ".." if (getcwd() ne $down_dir) or 1;
printf ("Current dir is %s\n",getcwd()) if $cmt_pgm;
return 0;
}
}
print ("------- Return of Func dir_is_end! Return value is 1 -------\n") if $cmt_pgm;
chdir ".." if (getcwd() ne $down_dir);
printf ("Current dir is %s\n",getcwd()) if $cmt_pgm ;
return 1;
}