- 论坛徽章:
- 0
|
原帖由 condor_P 于 2006-7-10 15:04 发表
- 1 sub get_dir {
- 2 my $dir = shift;
- 3 opendir (DIR,"$dir") or die "Can't open dir:$!";
- 4 my @file = readdir(DIR);
- 5 closedir(DIR);
- 6 foreach (@file){
- 7 if (-d $_){
- 8 get_dir($_);
- 9 }else{
- 10 get_file($_);
- 11 }
- 12 }
- 13 }
复制代码
Hallo,
the wrong point is at if (-d $_) { ...
reddir() reads relative filename and put it (without fullpath) in the array @file!
You should check $_ with fullpath!!!
i.e.:
-
- 6 foreach (@file) {
- next if $_ eq '.' or $_ eq '..'; // skip it
- 7 my $fullfilename = "$dir/$_";
- 8 if (-d $fullfilename) {
- 9 get_dir($fullfilename);
- 10 } else {
- 11 get_file($fullfilename);
- 12 }
- 13 }
复制代码
Read more about perldoc perlfunc =>relative opendir(), readdir()
Best,
ulmer
--------
Just 4 Fun |
|