Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying a .ini file and getting error

Status
Not open for further replies.

pellet

Programmer
Oct 23, 2008
55
US
Hi everyone,

First off, I want to thank everyone again for sharing their knowledge. I truly appreciate your hard work and dedication.

I am trying to modify a .ini file that has some similar wording inside. What I have tried is to use the bracket header as my search criteria and then use mid to modify what I want it to do. What is happening is somehow the data I write to the file gets written, but then gets overwritten by other data later on in the script. Here's a copy of my script. I am sure there is a better way to modify all the variables, but this was the only way I could get it to "semi work" without erasing words within the .ini file.

------------------------
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShl = WScript.CreateObject("WScript.Shell")
strZero = ("000000")
strRange = ("First")

strPrefix = InputBox("Please enter the letter prefix:")
If strPrefix = "" Then
Wscript.Quit
Else
strPriBegin = InputBox("Please enter the first range beginning #'s:")
If strPriBegin = "" Then
WScript.Quit
Else
strPriMaxNum = InputBox("Please enter the maximum number for the first range:")
If strPriMaxNum = "" Then
WScript.Quit
Else
strSecBegin = InputBox("Please enter the second range beginning #'s:")
If strSecBegin = "" Then
WScript.Quit
Else
strSecMaxNum = InputBox("Please enter the maximum number of the second range:")
If strSecMaxNum = "" Then
WScript.Quit
Else
End If
End If
End If
End If
End If

set objFile = objFSO.OpenTextFile ("C:\CustomerNumbers.ini", ForReading)
strCFG = objFile.ReadAll
strPre = Mid(strCFG, InStr(strCFG, "[LastName]") + 17, 1)
objFile.Close
set objFile = objFSO.OpenTextFile("C:\CustomerNumbers.ini", ForWriting, true)
strContent1 = replace(strCFG, strPre, strPrefix)
objFile.WriteLine strContent1
objFile.Close
WScript.Sleep (200)
'
set objFile = objFSO.OpenTextFile ("C:\CustomerNumbers.ini", ForReading)
strCFG = objFile.ReadAll
strPriBeg = Mid(strCFG, InStr(strCFG, "[BeginningNumbers]") + 28, 6)
objFile.Close
set objFile = objFSO.OpenTextFile("C:\CustomerNumbers.ini", ForWriting, true)
strContent2 = replace(strCFG, strPriBeg, strPriBegin)
objFile.WriteLine strContent2
objFile.Close
WScript.Sleep (200)
'
Set objFile = objFSO.OpenTextFile ("C:\CustomerNumbers.ini", ForReading)
strCFG = objFile.ReadAll
strPriUsed = Mid(strCFG, InStr(strCFG, "[EndingNumbers1]") + 34, 6)
objFile.Close
Set objFile = objFSO.OpenTextFile("C:\CustomerNumbers.ini", ForWriting, true)
strContent3 = replace(strCFG, strPriUsed, strZero)
objFile.WriteLine strContent3
objFile.Close
WScript.Sleep (200)
'
set objFile = objFSO.OpenTextFile ("C:\CustomerNumbers.ini", ForReading)
strCFG = objFile.ReadAll
strPriMax = Mid(strCFG, InStr(strCFG, "[MaxBeginning1]") + 24, 3)
objFile.Close
set objFile = objFSO.OpenTextFile("C:\CustomerNumbers.ini", ForWriting, true)
strContent4 = replace(strCFG, strPriMax, strPriMaxNum)
objFile.WriteLine strContent4
objFile.Close
WScript.Sleep (200)
'
set objFile = objFSO.OpenTextFile ("C:\CustomerNumbers.ini", ForReading)
strCFG = objFile.ReadAll
strSecBeg = Mid(strCFG, InStr(strCFG, "[BeginningNumbers2]") + 27, 6)
objFile.Close
set objFile = objFSO.OpenTextFile("C:\CustomerNumbers.ini", ForWriting, true)
strContent5 = replace(strCFG, strSecBeg, strSecBegin)
objFile.WriteLine strContent5
objFile.Close
WScript.Sleep (200)
'
set objFile = objFSO.OpenTextFile ("C:\CustomerNumbers.ini", ForReading)
strCFG = objFile.ReadAll
strSecMax = Mid(strCFG, InStr(strCFG, "[MaxSecondary2]") + 26, 3)
objFile.Close
set objFile = objFSO.OpenTextFile("C:\CustomerNumbers.ini", ForWriting, true)
strContent6 = replace(strCFG, strSecMax, strSecMaxNum)
objFile.WriteLine strContent6
objFile.Close
WScript.Sleep (200)
'
set objFile = objFSO.OpenTextFile ("C:\CustomerNumbers.ini", ForReading)
strCFG = objFile.ReadAll
strSecUsed = Mid(strCFG, InStr(strCFG, "[SecondaryLastNum]") + 36, 6)
objFile.Close
set objFile = objFSO.OpenTextFile("C:\CustomerNumbers.ini", ForWriting, true)
strContent7 = replace(strCFG, strSecUsed, strZero)
objFile.WriteLine strContent7
objFile.Close
WScript.Sleep (200)
'
set objFile = objFSO.OpenTextFile ("C:\CustomerNumbers.ini", ForReading)
strCFG = objFile.ReadAll
strRangeInUse = Mid(strCFG, InStr(strCFG, "[InUse]") + 25, 8)
objFile.Close
set objFile = objFSO.OpenTextFile("C:\CustomerNumbers.ini", ForWriting, true)
strContent8 = replace(strCFG, strRangeInUse, strRange)
objFile.WriteLine strContent8
objFile.Close
WScript.Sleep (200)
---------------------------------

Again, I know it is probably not written the best as far as the looks of it. Would someone be able to tell me why for instance, the beginning numbers get overwritten by my strZero variable? If I do it piece by piece, it works great. Inside the file looks something like this:

[BeginningNumbers]
Begin=123456

I am trying to include the bracketed text in my search and then count out the spaces to the 6 digit number. I cannot just use "Begin" because our secondary numbers look like this:

[BeginningNumbers2]
Begin=67890

So if I just search for "Begin" then I change both sets of numbers under different bracket headings.

Any help or different ideas on how to change the data is greatly appreciated. Thank you all for your time.

Pellet
 
when you open a file for writing, the contents is cleared and the position pointer is reset to the beginning of the file - basically overwriting current content with new content. To maintain the file contents, open the file for append. This will write content to the end of the file.

I think what you are trying to do is replace certain data from the .ini file with new data. My suggestion would be to read the .ini contents into a variable

Code:
strContents = objFile.ReadAll

and then replace data using the replace() function.

Code:
strContents = replace(strContents, find, replaceWith)

Once all the data replacement is done, THEN write the contents to the file.

Code:
objFile.Write(strContents)

-Geates
 
I'll give it a shot Geates - thanks for the idea! :)
 
Hi Geates,

I must be messing something up - I can make it append to the file, in fact it appends multiple times, but the first append shows the last name letter digit change, then the second shows the beginning number change, but the last name letter is changed back to what it was before.
 
can you provide me with the your .ini file.

-Geates
 
Sorry for the delayed response Geates, I was gone for a couple of days. I will get the .ini file to you - do you want it emailed to you? If so, can you provide your email address?
 
Hi Geates,

Here's a copy of the .ini file:

[LastName]
LastName=A
[BeginningNumbers]
BeginNumbers=111111
[EndingNumbers1]
EndingNumbers=000000
[MaxBeginning1]
MaxNum=1
RangeOneRemaining=1
[MaxBeginning1]
BeginNumbers=222222
[MaxSecondary2]
MaxNum=1
InUseRemaining=1
[EndingNumbers2]
EndingNumbers=000000
[InUse]
InUse=One

My strCFG numbers in the original script are not correct anymore since modifying the .ini file. As you can see, I have two EndingNumbers, which is why I searched for EndingNumbers1 and EndingNumbers2 and did my count from there, since they were different. I hope that makes sense.
Thanks for your help.
Pellet
 
Hmm..

You might have better luck reading each line into its own array element:
Code:
dim arrContents()
intCount = 0

set objFile = objFSO.OpenTextFile("C:\file.ini", 2, true)
do while NOT objFile.AtEndOfFile
   redim preserve arrContents(intCount)
   arrContents(intCount) = objFile.ReadLine
loop
objFile.close

Then, using a sub routine, run through each element looking for the "container" [EndingNumbers2]. Once found, keep searching for the var (EndingNumbers). Replace the data with the correct data

Code:
sub updateData(strContainer, strVar, strData)
   boolConFound = false
   for i = 0 to ubound(arrContents)
      strLine = arrContents(i)
      if (boolConFound) then arrContents(i) = strVar & "=" & strData
      if (inStr(strLine, strContainer)) then boolConFound = true
   next
end sub

Once all the data has been changed, write the Contents back to the file.
Code:
set objFile = OpenTextFile("C:\file.ini", 1, true)
for i = 0 to ubound(arrContents)
   objFile.WriteLine arrContents(i)
next
objFile.Close

usage:
strSecMaxNum = InputBox("Please enter the maximum number of the second range:")
updateData("[EndingNumbers2]", "EndingNumbers", strSecMaxNumber)

-Geates
 
I'll try it when time permits and report back. Thanks for helping Geates! I truly appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top