我想编写一个遍历目录及其子目录的脚本,获取所有 XML 文件并解析它们。我遇到了麻烦chdir
。这工作正常:
my $search = "/home/user/books";
chdir($search) or die "cant change dir to $search $!";
system("ls");
但我希望用户决定他想要搜索它的路径,所以我正在使用Getopt::Long
:
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
use Getopt::Long;
my $outputFile = '';
my $searchPath = "";
my $debug = 0;
GetOptions('outputFile=s' => \$outputFile, 'searchPath=s' => \$searchPath);
if ($outputFile eq '' or $searchPath = '') {
die("parameter --outpulFile=s is required.");
}
$searchPath =~ s/\/*$/\//;
my @founddirs = `cd $searchPath`;
foreach my $foundfiles (@founddirs) {
print $foundfiles;
chdir($foundfiles) or die "cant change dir to $searchPath $!";
chdir('..');
}
运行命令:
perl sample.pl --outputFile=books.txt --searchPath=/home/user/june18
我想从子目录中获取所有 recursive.xml 文件并解析它们。有谁知道如何做到这一点?