Perl多个文件读取替换字符串,写入多个文件



我有这段代码在工作,但这仅适用于一个具有特定名称的文件,我怎样才能让它完成当前文件夹中的所有文件.vb并输出文件名加 _1 在后面

#!/usr/bin/perl
use strict;
use warnings;
open my $fhIn,  '<', 'file.vb'          or die $!;
open my $fhOut, '>', 'file_1.vb'        or die $!;
while (<$fhIn>) {
    print $fhOut "'01/20/2016 Added ngpFrmPosition = Me.Locationn" if /MessageBox/ and !/'/;
    
    print $fhOut $_;
}
close $fhOut;
close $fhIn;

我可能会这样处理它。(这假定脚本在与.vb文件相同的目录中运行)。

#!/usr/bin/perl
use strict;
use warnings;
# script running in same directory as the .vb files
for my $file (glob "*.vb") {
    my $outfile = $file =~ s/(?=.vb$)/_1/r;
    print "$file $outfilen"; # DEBUG 
    # open input and output files
    # do the while loop
}

循环中的 print 语句用于调试目的 - 查看是否正确创建新文件名。您可以删除它或在满意地获得所需文件时将其注释掉。

更新:将 glob 放在 for 循环中,而不是将其读取到数组中。

最新更新