【发布时间】:2010-10-29 10:07:41
【问题描述】:
我有以下脚本,它接受输入文件、输出文件和 用其他字符串替换输入文件中的字符串并写出 输出文件。
我想更改脚本以遍历文件目录 即脚本不应提示输入和输出文件,而应采用 作为参数的目录路径,例如 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);
【问题讨论】: