VB script to modify text file

We have an application with several settings stored in an ini file. as shown below

[Startup]
BinListPath="\\OldServer\ShareName\dir2\"
ViewCardData=True
ProgPath="\\OldServer\ShareName\dir2\"
UserDBPath="\\OldServer\ShareName\db\"
HistDBPath="\\OldServer\ShareName\dir2\"
CrdPath="\\OldServer\ShareName\dir3\"
ContrListPath="C:\Program Files\dir\IntelliCAT\"
ReportPath="\\OldServer\ShareName\Reports\"
CGSName="1401"
CGSPath="\\OldServer\ShareName\Queue\1401\"
CGSetup="True"
TimeOut="0"
LogoutEachTrns="TRUE"
IPTimeOut="255"
ManualFeed="False"
ShowGenie="True"
ReportPrinter="0"

[Communication]
ICCommPort=" 2"
IPCommPort=" 1"
Machine="data"

[Reports]
ReportName1="Export Database"
ReportFileName1="0\\OldServer\ShareName\Reports\Export_Database.rpt"

[DSN]
EKSNE="05C4AB64E01AFD18"

[Controller]
ServerName="1401"
Machine="machiner"
QueueLocation="\\OldServer\ShareName\Queue\1401"
WorkLocation="C:\Program Files\dir\program\"
ScanInt="3"

[Admin]
IPCommPort="1"
ICCommPort="1"
EquipType="equip"
EquipSettings="9600,e,7,1"
AdminCo="2"

[Window]
Config_Module_Settings="60,0,12000,8805"
Config_Module_State="0"
Config_Module_Last_Tab="0"

[REGISTRATION]
FName="Jane"
LName="doe"
CoName="company"
CoAddress="12345 anywhere"
CoCity="city"
CoState="state"
CoZip="zip"
CoPhone="12345678"
CoFax="12345678"

[ACTIVATION]
1234567

[RecentFiles]
File4="File5"
Path4="\\OldServer\ShareName\dir3\file1"
File3="file6"
Path3="\\OldServer\ShareName\dir3\file2"
File2="file7"
Path2="\\OldServer\ShareName\dir3\file3"
File1="file8"
Path1="\\OldServer\ShareName\dir3\file4"

we will be decomissioning OldServer soon and moving the database the application connects to NewSever.  So I need to create a script that will change all entries in the ini file from OldServer to NewServer all other lines must remain the same.  This file needs to be modified on approximatley 200 workstations.  Replacing the file is not an option as the software would require us to input a new activation number.
octhdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
If you are looking to do this from VB,here is a function that may help. I used a command button to call the function as an example,but it could easily be turned into a command line utility that can be called from a logon script.



Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal _
lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, _
ByVal nSize As Long, ByVal lpFileName As String) As Long

Private Declare Function WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal _
lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long


Private Sub Command1_Click()
ReplaceServerInINI "C:\Test.ini", "\\OldServer\", "\\NewServer\"
End Sub


Private Sub ReplaceServerInINI(IniFileName As String, OldSvr As String, NewSvr As String)
Dim ff As Integer, arrIni() As String, Ln As String, cnt As Integer
Dim arrEnt() As String, Section As String, i As Integer
cnt = 1

' open ini file and read into array
ff = FreeFile
Open IniFileName For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln

' get the section name
If InStr(Ln, "[") > 0 And InStr(Ln, "]") > 0 Then
    Section = Replace$(Trim$(Ln), "[", "")
    Section = Replace$(Section, "]", "")
End If

' if line has an "=" then get the values and add to array
If InStr(Ln, "=") > 0 Then
    arrEnt = Split(Ln, "=")
    ReDim Preserve arrIni(cnt)
    arrIni(cnt) = Section & "<->" & arrEnt(0) & "<->" & arrEnt(1)
    cnt = cnt + 1
End If
Loop
Close #ff

Erase arrEnt()

' loop through array & use API to change entries in file
For i = 1 To UBound(arrIni)
    arrEnt = Split(arrIni(i), "<->")
    Result = WritePrivateProfileString(arrEnt(0), arrEnt(1), Replace(arrEnt(2), OldSvr, NewSvr), IniFileName)
Next i

End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jimbobmcgeeCommented:
If you are determined to use VBS files, however, you will have a problem with the above code, as VBS does not like Declare statements...

In that case you will have to create a File System Object and do it line-by-line, the old-fashioned way...!!

J.
0
David LeeCommented:
Here's a sample script that uses the line-by-line approach that jimbobmcgee mentioned to make the changes.  All you need to do to use this is change the paths pointed to by strInputFile and strOutputFile.

    Dim objFSO, objTSInput, objTSOutput, strInputFile, strOutputFile, strBuffer
    strInputFile = "C:\EETesting\Testing.ini"
    strOutputFile = "C:\EETesting\NewFile.ini"
    Set objTSInput = objFSO.OpenTextFile(strInputFile)
    Set objTSOutput = objFSO.CreateTextFile(strOutputFile)
    While Not objTSInput.AtEndOfStream
        strBuffer = objTSInput.ReadLine
        strBuffer = Replace(strBuffer, "OldServer", "NewServer", , , vbTextCompare)
        objTSOutput.WriteLine strBuffer
    Wend
    objTSInput.Close
    objTSOutput.Close
    objFSO.CopyFile strOutputFile, strInputFile, True
    objFSO.DeleteFile strOutputFile
    Set objTSInput = Nothing
    Set objTSOutput = Nothing
    Set objFSO = Nothing
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.