‘VB scrpit 修改設定檔(XML):
Dim WshShellSet WshShell = CreateObject("Wscript.Shell")Set fso = CreateObject("Scripting.FileSystemObject")' Create log file for the script Set logStream = fso.createtextfile("updateConfig.log" , True)logStream.writeline "Start update Config" ' 假定要修改的為一個資料庫設定檔,要更新DataSource 和DataSourceMirror 兩個元素dataSource = "localhost"dataSourceMirror = "localhost" '假定要從另一個test.xml中讀取DataSource 和DataSourceMirror 的值,然後更新到 configFile.configtestPath= "test.xml"Set xmlDoc = CreateObject("Msxml2.DOMDocument") If (fso.FileExists(testPath)) Then msg = nBoxConfigFilePath & " exists" logStream.writeline msg ' Load the test config file xmlDoc.load(testPath) ' 假定test.xml的結構相對複雜,用xpath搜尋出對應的節點: Set dataSourceNode = xmlDoc.selectSingleNode("//test[@name='DataSource']").parentNode Set dataSourceMirrorNode = xmlDoc.selectSingleNode("//test[@name='DataSourceMirror ']").parentNode ' 取得節點的屬性值 dataSource = dataSourceNode.getAttribute("name") dataSourceMirror = dataSourceMirrorNode.getAttribute("name") Else msg = "Warning" & testPath & " doesn't exists " ' 如果testPath 路徑不存在那麼將警告資訊寫入log檔案 logStream.writeline msg End If Set WshEnv = WshShell.Environment("PROCESS")' 請注意將config dir 和configFile 替換成對應的內容 filePath = “configdir/configFile.config" If (fso.FileExists(filePath)) Then msg = filePath & " exists" ' 如果filePath 路徑不存在那麼將警告資訊寫入log檔案 logStream.writeline msg 'Loading config file xmlDoc.load(filePath) logStream.writeline "Loading config file " & filePath 'Get the datasource and mirror node Set rtDataSourceNode = xmlDoc.selectSingleNode("//add[@key='DataSource']") Set rtDataSourceMirrorNode = xmlDoc.selectSingleNode("//add[@key='DataSourceMirror']") 'Set the value attribute for the datasource and mirror node rtDataSourceNode.setAttribute "value", dataSource rtDataSourceMirrorNode.setAttribute "value", dataSourceMirror 'Save the config file XMLDoc.save(filePath) logStream.writeline "Save the config file" logStream.writeline "Finish the configuration" Else msg = "Error:" & filePath & " doesn't exists" End If logStream.Close‘ 釋放WshShell Set WshShell = Nothing
附configFile.config樣本:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="DataSource" value="localhost" />
<add key="DataSourceMirror" value="localhost" />
<add key="InitialCatalog" value="TestDB" />
</appSettings>
</configuration>