2016年2月13日土曜日

ファイル内文字列置換vbs

dim fso
set fso = CreateObject("Scripting.FileSystemObject")

dim path : path = ファイルパス
dim objFile : set objFile = fso.getFile(path)

'置換処理
call replaceFile(objFile)

set fso = Nothing
set objFile = Nothing

msgbox "end"
'******************************************************
sub replaceFile(objFile)
dim tso : set tso = objFile.openAsTextStream(1)
dim cnt : cnt = 0
dim fileText : fileText = tso.readAll

if inStr(1, fileText, 検索文字列) > 0 then
cnt = 1
 fileText = replace(fileText, 検索文字列, 置換文字列)
end if

tso.Close

if cnt = 1 then
set tso = objFile.openAsTextStream(2)
tso.writeLine fileText
tso.Close
end if

end sub
'******************************************************

エラー処理は無しのためデータ消える可能性があります。
テキストストリームを書き込みモードで開いた瞬間にファイル内文字列は全て消滅。
差し替えの変数に文字列情報が正しく入っていないとデータが消えます。


0 件のコメント:

コメントを投稿