我有以下脚本,它接收输入文件、输出文件并用其他字符串替换输入文件中的字符串并写出输出文件。
我想更改脚本以遍历文件目录,即不提示输入和输出文件,脚本应将目录路径作为参数,例如 C:\temp\allFilesTobeReplaced\ 并搜索字符串 x 并替换它用 y 表示该目录路径下的所有文件并写出相同的文件。
我该怎么做呢?
谢谢。
$file=$ARGV[0];
open(INFO,$file);
@lines=<INFO>;
print @lines;
open(INFO,">c:/filelist.txt");
foreach $file (@lines){
#print "$file\n";
print INFO "$file";
}
#print "Input file name: ";
#chomp($infilename = <STDIN>);
if ($ARGV[0]){
$file= $ARGV[0]
}
print "Output file name: ";
chomp($outfilename = <STDIN>);
print "Search string: ";
chomp($search = <STDIN>);
print "Replacement string: ";
chomp($replace = <STDIN>);
open(INFO,$file);
@lines=<INFO>;
open(OUT,">$outfilename") || die "cannot create $outfilename: $!";
foreach $file (@lines){
# read a line from file IN into $_
s/$search/$replace/g; # change the lines
print OUT $_; # print that line to file OUT
}
close(IN);
close(OUT);