大佬教程收集整理的这篇文章主要介绍了perl – 如何遍历目录中的所有文件;如果它有子目录,我也想遍历子目录中的文件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
opendir(DIR,"$pwd") or die "CAnnot open $pwd\n"; my @files = readdir(DIR); closedir(DIR); foreach my $file (@files) { next if ($file !~ /\.txt$/i); my $mtime = (stat($filE))[9]; print $mtime; print "\n"; }
基本上我想要记下目录中所有txt文件的时间戳.如果有一个子目录,我也希望在该子目录中包含文件.
有人可以帮我修改上面的代码,以便它也包含子目录.
如果我在Windows中使用下面的代码我获取文件夹中所有文件的时间戳,甚至在我的文件夹之外
@H_512_2@my @dirs = ("C:\\Users\\Peter\\Desktop\\folder"); my %seen; while (my $pwd = shift @dirs) { opendir(DIR,"$pwd") or die "CAnnot open $pwd\n"; my @files = readdir(DIR); closedir(DIR); #print @files; foreach my $file (@files) { if (-d $file and !$seen{$filE}) { $seen{$filE} = 1; push @dirs,"$pwd/$file"; } next if ($file !~ /\.txt$/i); my $mtime = (stat("$pwd\$file"))[9]; print "$pwd $file $mtime"; print "\n"; } }use warnings; use Strict; my @dirs = ("."); my %seen; while (my $pwd = shift @dirs) { opendir(DIR,"$pwd") or die "CAnnot open $pwd\n"; my @files = readdir(DIR); closedir(DIR); foreach my $file (@files) { next if $file =~ /^\.\.?$/; my $path = "$pwd/$file"; if (-d $path) { next if $seen{$path}; $seen{$path} = 1; push @dirs,$path; } next if ($path !~ /\.txt$/i); my $mtime = (stat($path))[9]; print "$path $mtime\n"; } }
以上是大佬教程为你收集整理的perl – 如何遍历目录中的所有文件;如果它有子目录,我也想遍历子目录中的文件全部内容,希望文章能够帮你解决perl – 如何遍历目录中的所有文件;如果它有子目录,我也想遍历子目录中的文件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。