Option Explicit
Dim fso, iniFile, args, patid, patlname, patfname
Dim section, lines, line, foundSection, i, key
Dim iniPath, exePath
' Paths
iniPath = "C:\Aaa\CliniView\Ini\CliniView.ini"
exePath = "C:\Aaa\CliniView\CliniView.exe"
Set args = WScript.Arguments
If args.Count < 3 Then
WScript.Echo "Usage: CliniView.vbs PATID PATLNAME PATFNAME"
WScript.Quit 1
End If
patid = args(0)
patlname = args(1)
patfname = args(2)
section = "[PracticeManagementInterface]"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(iniPath) Then
Set iniFile = fso.OpenTextFile(iniPath, 1, False)
lines = Split(iniFile.ReadAll, vbCrLf)
iniFile.Close
Else
lines = Array()
End If
' Prepare new key-values
Dim newValues
Set newValues = CreateObject("Scripting.Dictionary")
newValues.Add "USE_PRACTICE_MANAGEMENT", "1"
newValues.Add "CLEAR_PRACTICE_MANAGEMENT_AUTOMATICALLY", "1"
newValues.Add "PATID", patid
newValues.Add "PATLNAME", patlname
newValues.Add "PATFNAME", patfname
foundSection = False
' Rebuild file line by line
Dim resultLines()
ReDim resultLines(0)
Dim resultCount : resultCount = 0
For i = 0 To UBound(lines)
line = lines(i)
If Trim(UCase(line)) = UCase(section) Then
foundSection = True
resultLines(resultCount) = section
resultCount = resultCount + 1
ReDim Preserve resultLines(resultCount)
' Add new values instead of reading existing
For Each key In newValues.Keys
resultLines(resultCount) = key & "=" & newValues(key)
resultCount = resultCount + 1
ReDim Preserve resultLines(resultCount)
Next
' Skip old section content
Do While i + 1 <= UBound(lines)
If Left(Trim(lines(i + 1)), 1) = "[" Then
resultLines(resultCount) = "" ' blank line between sections
resultCount = resultCount + 1
ReDim Preserve resultLines(resultCount)
Exit Do
End If
i = i + 1
Loop
Else
resultLines(resultCount) = line
resultCount = resultCount + 1
ReDim Preserve resultLines(resultCount)
End If
Next
If Not foundSection Then
resultLines(resultCount) = section
resultCount = resultCount + 1
ReDim Preserve resultLines(resultCount)
For Each key In newValues.Keys
resultLines(resultCount) = key & "=" & newValues(key)
resultCount = resultCount + 1
ReDim Preserve resultLines(resultCount)
Next
End If
' Write back to file
Set iniFile = fso.OpenTextFile(iniPath, 2, True)
For i = 0 To resultCount - 1
If i = resultCount - 1 Then
iniFile.Write resultLines(i) ' without ending CRLF
Else
iniFile.WriteLine resultLines(i)
End If
Next
iniFile.Close
' Run the CliniView executable
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run """" & exePath & """", 1, False
|