需要修改以下内容以处理多个csv文件: 以下脚本可以使用
执行 perl csv_xlsx.pl --ifile C:\Users\batman\source\repos\perlproj\sample_20200609.csv --odir C:\Users\batman\source\repos\perlproj
但需要处理多个文件
use strict;
use warnings;
use Getopt::Long;`enter code here`
use Text::CSV_XS qw( csv );
use Excel::Writer::XLSX;
use File::Basename;
# get digit from string
sub get_digit {
my ($string) = @_;
my ($digit) = $string =~ /(\d+)/;
return ((defined $digit) ? $digit : '');
}
# write data to excel file
sub write_excel {
my ($worksheet,$aoa) = @_;
my $row = 0;
foreach my $internal_array_ref (@$aoa) {
my $col=0;
foreach my $element (@$internal_array_ref) {
# if element is not having any value
$element = '' unless defined $element;
$worksheet->write( $row, $col++, $element );
}
$row++;
}
}
### Main ##
my $csv_file;
my $output_dir;
GetOptions(
'ifile=s' => \$csv_file,
'odir=s' => \$output_dir,
) or die "Usage: $0 --ifile FILE PATH --odir DIRECTORY PATH \n";
if (not defined $csv_file) {
print "\n Argument 'ifile' is mandatory \n";
print "for example $0 --ifile FILEPATH";
exit 1;
}
if (not defined $output_dir) {
print "\n Argument 'odir' is mandatory \n";
print "for example $0 --odir DIRECTORY PATH";
exit 1;
}
# check for file exists and not empty
if ((-s $csv_file) && (-d $output_dir)) {
# Check for file contain date digit
my $date_digit = get_digit($csv_file);
if ($date_digit eq '') {
print "\n $csv_file not contain date information \n";
exit 1;
} else {
# excel file name with date digit
# get file name from given file path
my $filename = basename("$csv_file", ".csv");
# this is for windows
my $excel_file = "$output_dir\\$filename.xlsx";
# for linux
#my $excel_file = "$output_dir/$filename.xlsx";
# Read whole file in memory (as array of array)
# change seperate char as per your csv file data
# change quote char as per your csv file data (else just mentioned undef)
# change escape char as per your csv file data (else just mentioned undef)
eval {
my $aoa = csv (in => $csv_file,
encoding => "UTF-8",
sep_char => ',',
quote_char => '"',
escape_char => undef);
if (scalar @$aoa) {
# Create a new Excel workbook
my $workbook = Excel::Writer::XLSX->new( $excel_file );
# Add a worksheet
my $worksheet = $workbook->add_worksheet();
# write to excel file
write_excel($worksheet,$aoa);
$workbook->close();
print "\n The $excel_file created sucessfully. \n";
}
};
if ($@) {
print "\n Invalid CSV file or check CSV file format \n";
exit 1;
}
}
} else {`enter code here`
print "\n Please provide valid file path: $csv_file or Please provide valid directory path ";
exit 1;
}
=============================================== ======================
答案 0 :(得分:1)
您可以简单地循环调用程序。
for %q in (...\*.csv) do csv_xlsx.pl --ifile "%q" --odir "%~dpq"
但是,好的,该程序需要一点时间来加载,也许您需要避免这种情况。
如果程序的界面更加明智,则可以轻松地将其更改为接受多个文件。问题是您有强制性选项。如何同时具有强制性和可选性?是的,这是糟糕的设计。有时是必需的,但这里不是。该程序可以提供以下界面:
perl csv_xlsx.pl [--odir <dir>] [<file.csv> [...]]
这是一个非常简单的更改。
my $output_dir = ".";
GetOptions(
'odir=s' => \$output_dir,
)
or ...;
my @csv_files = @ARGV;
for my $csv_file (@csv_files) {
...
}