我正在研究一个脚本,它将读取文件,并根据文件内容重新命名文件。如何遍历目录和子目录中的所有文件
问题,我现在面临:
1日,我无法读取文件,该文件是在子目录中。 第二我想重命名文件,但出现错误。
这里是我的代码:
Folder Structur :
Logs
///\
a b c d
/\ \
e file file
其中A,B,C,d,e为DIR 文件,文件是txt文件
#!/usr/bin/perl
use strict;
use warnings;
use File::Copy::Recursive;
use Cwd;
my $file;
my (@FILES,@File_01);
my ($dir_01,$dir_02);
my $new_file="Kernel.txt";
chdir('C:\\abd\\efg');
$dir_01 = getcwd;
opendir(DIR_01, $dir_01);
@FILES=readdir(DIR_01);
close(DIR_01);
foreach $dir_02 (@FILES)
{
opendir (DIR_02, "$dir_01"."/"."$dir_02") ;
@File_01=readdir(DIR_02);
close(DIR_02);
foreach $file (@File_01)
{
open(FH,"<","$dir_01/$dir_02/$file") or die "can not open the file $!\n";
while(my $lines = <FH>)
{
if($lines=~ m/Linux kernel Version/i || $lines=~ m/USB_STATE=DISCONNECTED/gi)
{
#print "\t$lines\n\n";
rename($file,$new_file) or die "can not change the name";
}
}
}
}
你的意思是“无法打开”重命名?你有错误信息吗?哪一个? – Matteo
[如何遍历目录中的所有文件;如果它有子目录,我想遍历子目录中的文件](http://stackoverflow.com/questions/9600395/how-to-traverse-all-the-files-in-a-directory-if-it-has -subdirectories -i-want-t) – innaM