2021/02/09(火) [n年前の日記]
#2 [prog] VBScriptでiniファイルを書き換える処理を書いてみた
.iniファイル内に書かれたフォルダパス(ディレクトリパス)を、現在フォルダで書き換えたいなと。
エディタで iniファイルを開いて、手作業で書き換えても目的は果たせるけれど、何度も試していると面倒臭い。ここは少しでも自動化したい。
ということで、WSH(VBScript)を使って、.ini ファイルを書き換えられないか試してみたり。環境は、Windows10 x64 20H2。
処理としては、以下のような感じ。
テンプレートファイルは以下。
_template_ini.txt
VBScript は以下。
_replace_ini.vbs
これで、以下のようなファイルを同じフォルダに置いておいて…。
エディタで iniファイルを開いて、手作業で書き換えても目的は果たせるけれど、何度も試していると面倒臭い。ここは少しでも自動化したい。
ということで、WSH(VBScript)を使って、.ini ファイルを書き換えられないか試してみたり。環境は、Windows10 x64 20H2。
処理としては、以下のような感じ。
- スクリプトの置いてあるフォルダパス(カレントフォルダ)を取得。
- iniファイルの元になるテンプレートファイルが存在するか調べる。
- iniファイルがカレントフォルダ内に存在するか探す。
- テンプレートファイルを読み込んで、特定文字列(今回は "[INSTALL_DIR]") をフォルダパスで置換。
- 置換後の内容で、iniファイルを上書き更新。
テンプレートファイルは以下。
_template_ini.txt
fullscreen_bin=[INSTALL_DIR]\driveqp_scrsaver.exe config_bin=wscript [INSTALL_DIR]\driveqp_scrsaver_about.vbs preview_img=[INSTALL_DIR]\driveqp_scrsaver_preview.bmp
VBScript は以下。
_replace_ini.vbs
' スクリーンセーバラッパー scrsavwr 用の ini ファイルを生成するvbs(VBScript)
' フルスクリーン表示用プログラム群が入っているディレクトリ内で実行すると、
' template_ini.txt の内容に従って、パスを反映させたiniファイルを生成する。
'
' Generate an ini file for scrsavwr (screensaver wrapper).
' Run in the directory that contains the full screen program.
' Generate an ini file that reflects the path according to the contents of template_ini.txt.
Option Explicit
Dim objFso
Dim objFile
Dim objDir
Dim cdir
Dim ini_name
Dim template_path
Dim ret
Dim cmdstr
Dim i
Dim s
ini_name = ""
template_path = "template_ini.txt"
set objFso = createObject("Scripting.FileSystemObject")
If Err.Number <> 0 Then
MsgBox "Error : " & Err.Description
WScript.Quit
End If
' Get folder path of the script file
cdir = objFso.getParentFolderName(WScript.ScriptFullName)
' Check exists template file
If objFso.FileExists(template_path) = False Then
MsgBox "Not found : " & template_path, , "Error"
Set objFso = Nothing
WScript.Quit
End If
' Search ini file
Set objDir = objFso.GetFolder(cdir)
For Each objFile In objDir.Files
If Right(objFile.Name, 4) = ".ini" Then
ini_name = objFile.name
End If
Next
If ini_name = "" Then
' Not found ini file
MsgBox "Not found .ini file",, "Error"
Set objFso = Nothing
WScript.Quit
Else
' Found ini file
ret = MsgBox("Found " & ini_name & vbCrLf & "Replace ?", vbYesNo + vbQuestion)
If ret <> vbYes Then
MsgBox "Cancel the process."
Set ret = Nothing
Set objFso = Nothing
WScript.Quit
End If
Set ret = Nothing
End If
' Open template file
Set objFile = objFso.OpenTextFile(template_path)
' Record the contents of the file line by line in an array
i = 0
Do Until objFile.AtEndOfStream
' Extends the size of the array while preserving its contents
redim Preserve lines(i)
' Read one line
cmdstr = objFile.ReadLine
' Replace folder path
lines(i) = Replace(cmdstr, "[INSTALL_DIR]", cdir)
i = i + 1
Loop
objFile.Close
' Export to ini file
Set objFile = objFso.OpenTextFile(ini_name, 2, True)
For Each s In lines
objFile.WriteLine(s)
Next
objFile.Close
MsgBox "Replace " & ini_name
Set objFso = Nothing
Set ObjFile = Nothing
これで、以下のようなファイルを同じフォルダに置いておいて…。
- replace_ini.vbs
- template_ini.txt
- hoge.ini
[ ツッコむ ]
以上です。