opendir(DIR,"/var/test");
while (defined($file=readdir(DIR))) {
$file=~s/^\.//;
$file=~s/^\.//;
if ($file){
print "$file\n";
}
}
closedir(DIR);
#!/usr/bin/perl
print "enter the complete path:";
$cin_t = ;
chomp $cin_t;
our @stack; #保存文件夹目录名。
&EnumDir($cin_t);
#######################################
sub EnumDir
{
@dir_f = @_;
my @struct_dirs;
my $path = join(//, @dir_f); #数组转化为标量。
#print "$path \n";
print "[Files in Directory $path] \n";
$ret_b = opendir(HFILE, $path);
die "open dir error $!" if(!$ret_b);
@file_s = readdir(HFILE);
for($count = 0; $count<@file_s; $count++)
{
#print "$count \n";
if($file_s[$count] =~ m/\./ ) #打印文件;
{
if($file_s[$count] =~ m/^\.\.?$/ ) #过滤. 和..;
{
next;
}
print "$file_s[$count] \n";
}
#///////////////////////////#
else
{
print "$file_s[$count] \n"; #打印文件夹并压栈;
$subdir_t = $file_s[$count];
$subdir = "$path/$subdir_t";
&PushDirs($subdir);
}
}
closedir(HFILE);
$sub_dir = &PopDirs();
#die "no dir " if($sub_dir =0);
&EnumDir($sub_dir);
}
##############################
sub PushDirs #目录名压栈。
{
my @dir_fs = @_;
my $path_fs = join(//, @dir_fs);
push (@stack, $path_fs);
}
#-----------------------
sub PopDirs #取一个目录名。
{
$size = @stack;
if($size != 0)
{
$buf_pop = pop(@stack);
return $buf_pop;
}
die "nofile empty";
}
阅读(1302) | 评论(0) | 转发(0) |