情報システム科
ディレクトリ内及びその下のディレクトリ内のファイルの内容を一気に、複数内容の置換え
# ディレクトリ内及びその下のディレクトリ内のファイルの内容を一気に、複数内容の置換え
# 動作開始コマンド
# perl c.pl
# 置換え前の文字-----下記に文字を記入-------
@s_from=("abcd", "xyz");
# 置換え後の文字-----下記に文字を記入-------
@s_to= ("123", "45");
# 対象ファイル名-----下記に文字を記入-------
$sa="*.html";
# メイン
$dep = 1;
&dirproc($dep);
# dirprocサブルーチン
sub dirproc{
my($dep)=@_;
my(@files,$file);
@files=glob($sa);
foreach $file(@files){
if(-f $file){
print $file."\n";
# ファイルの内容を変更
open( IN,$file);
@a=; # ファイルの内容をすべて@a に読み込む
close( IN );
@b=(); # 初期化
$no=@s_from;
foreach (@a){
for ($n=0; $n<$no; ++$n){
s/@s_from[$n]/@s_to[$n]/g; # @s_fromを@s_toに置き換える
}
push( @b,$_); # @b の最後尾に追加
}
open (OUT,">$file");
print OUT @b;
close (OUT);
}
}
@files=glob("*");
foreach $file(@files){
if(-d $file){
print $file."\n";
++$dep;
chdir($file);
&dirproc($dep);
chdir("..");
--$dep;
}
}
}
ホームへ