Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB script to modify text file

Posted on 2004-10-27
6
Medium Priority
?
269 Views
Last Modified: 2008-01-16
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.
0
Comment
Question by:octhd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 1000 total points
ID: 12428043
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
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12432089
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
 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 1000 total points
ID: 12459816
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question