【发布时间】:2014-12-15 04:45:20
【问题描述】:
我正在尝试开发一个脚本,它将通过项目目录结构和源文件(主要是 java 和 xml 文件)查找文件名,这些文件名可能在代码中的错误情况下如何在文件所在的目录/驱动器上命名位于。例如,驱动器上代码 Abc.xml 中的 ABC.xml。在我们从 windows 迁移到 linux 时发现了这个问题。
我最初考虑使用 ACK,但这里的防火墙似乎阻止了 CPAN,并且它一直无法使用 dmake 在我的计算机上手动安装。 (使用最新版本的草莓)
到目前为止,这是我能够汇总的内容,它递归地搜索基本路径下的每个子目录,获取 java 和 xml 文件。然后它将打开找到的每个文件并对源列表中的每个名称进行不区分大小写的搜索,然后对搜索结果进行区分大小写的匹配以删除大小写相同的结果,然后将否定结果存储在每个源文件(键)都有一个数组(值)存储每个文件的名称,其中发现案例与文件名不匹配。最后我打算打印出哈希。
我目前难以设置数组的哈希值,但我愿意接受替代/更简单的解决方案。
my $source = "C:/sampleSourcefiles";
my $base_path = "C:/baseDIRprojectCode";
my @searchList;
my %report;
#open source file directory.
if($source){
opendir (DIR, $source) or die "Directory not found \n" ;
@searchList = grep(/^.+\..+$/, readdir(DIR));
closedir DIR;
}
#code does not have file extensions trim from names
foreach my $file (@searchList){
$file =~ s/\.dat|xml$//;
#print "$file\n";
}
process_files ($base_path);
# Accepts one argument: the full path to a directory.
sub process_files {
my $path = shift;
# Open the directory.
opendir (DIR, $path) or die "Unable to open $path: $!";
# Read in the files.
my @files = grep {!/^\./} readdir (DIR);
closedir (DIR);
# append the full path to the file names.
@files = map { $path . '/' . $_ } @files;
for (@files) {
# If the file is a directory
if (-d $_) {
process_files ($_);
# If it isn't a directory, process the file.
} else {
file_search($_);
}
}
}
# Accepts one argument: the source file to search
sub file_search {
my $file = shift;
#ignore all files not java or xml
if ($file =~ /\.xml|java$/){
#search for match to any file in the list
foreach my $item (@searchList){
open(F, $file);
my @lines = <F>;
close F;
my @result = grep /$item/i , @lines;
if (@result){
%report($item, @result);
#foreach my $res (@result){
# if($res eq $file){
# print "good result\n";
# } else {
# print "Inequality match found in file $file for $res\n";
# }
#}
} else {
}
}
}
}
【问题讨论】:
标签: perl