用perl批量处理文件夹下所有文件
opendir()获取目录权柄;readdir()读取目录下文件
#!/usr/bin/perl -w
$dirname = "/tmp"; #指定一个目录
opendir ( DIR, $dirname ) || die "Error in opening dir $dirnamen";
while(($filename = readdir(DIR))){
print("$filenamen"); #循环输出该目录下的文件。
### do something ###
}
closedir(DIR);
例 把每个文件复制一次:
#!/usr/bin/perl
use warnings;
use strict;
my (%h, $filename);
my $dirname="/home/bmk/";
opendir (DIR, $dirname)|| die "Error in opening dir $dirnamen";
while (($filename=readdir(DIR))){
open (FILE, "/home/bmk/".$filename)|| die "can not open the file $filenamen";
open (OUT, ">/home/bmk/$filename.bak")|| die "can not open the $filename.bakn";
while (<FILE>){
print OUT "$_";
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!