I need to write a string value to a section of an ini file. I managed to
write to it but it appends to existing values. I need to either edit the
current value or delete the values in the section in question before I write
new values

For example:

file ="C:\VBS\myConfig.ini"
Section = "ORACLE TO SQL SERVER"
item = "CASE_X"
Value = "XYZ"

ini file output:

[ORACLE TO SQL SERVER]
CASE_X=ABC
CASE_X=XYZ
CASE_X=XYZ
DB_NAME=YYY
LSNAME=orclDB
DTSERROR_O2S="C:\sqlConfig\dtsError\orcl_2_mssql\xxx.txt"

How do I edit the original value of ABC to XYZ without having to write new
lines as I need only I line for CASE_X Values. Any input would be greatly
apreciated

--
fhillipo

Re: Edit/Delete section of an ini file by McKirahan

McKirahan
Sun Nov 28 12:48:30 CST 2004

"fhillipo" <fhillipo@discussions.microsoft.com> wrote in message
news:CA722256-B05C-47CC-BF23-751CE9947738@microsoft.com...
> I need to write a string value to a section of an ini file. I managed to
> write to it but it appends to existing values. I need to either edit the
> current value or delete the values in the section in question before I
write
> new values
>
> For example:
>
> file ="C:\VBS\myConfig.ini"
> Section = "ORACLE TO SQL SERVER"
> item = "CASE_X"
> Value = "XYZ"
>
> ini file output:
>
> [ORACLE TO SQL SERVER]
> CASE_X=ABC
> CASE_X=XYZ
> CASE_X=XYZ
> DB_NAME=YYY
> LSNAME=orclDB
> DTSERROR_O2S="C:\sqlConfig\dtsError\orcl_2_mssql\xxx.txt"
>
> How do I edit the original value of ABC to XYZ without having to write new
> lines as I need only I line for CASE_X Values. Any input would be greatly
> apreciated
>
> --
> fhillipo

Here's an interactive solution that uses an HTA (HTML Application):

<html>
<head>
<title>ini.hta</title>
<HTA:Application ID = "INI"
ApplicationName = "Update"
Border = "thin"
BorderStyle = "normal"
Caption = "yes"
Icon = ""
MaximizeButton = "yes"
MinimizeButton = "yes"
ShowInTaskBar = "yes"
SingleInstance = "yes"
SysMenu = "yes"
Version = "1.0"
WindowState = "maximize"
>
<script type="text/vbscript">
Option Explicit

Sub INI(OPT)
'*
'* Declare Constants
'*
Const cHTA = "ini.hta"
Const cINI = "ini.ini"
'*
'* Delare Variables
'*
Dim strOTF
'*
'* Declare Objects
'*
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOTF
'*
'* Read or Write File
'*
If OPT = 0 Then
document.title = cHTA & " updates " & cINI
'*
'* Read File
'*
Set objOTF = objFSO.OpenTextFile(cINI,1)
strOTF = objOTF.ReadAll()
Set objOTF = Nothing
document.getElementById("text").value = strOTF
Else
document.title = cHTA & " updated " & cINI
'*
'* Write File
'*
strOTF = document.getElementById("text").value
Set objOTF = objFSO.OpenTextFile(cINI,2,True)
objOTF.Write(strOTF)
Set objOTF = Nothing
Set objOTF = Nothing
End If
'*
'* Destroy Objects
'*
Set objFSO = Nothing
End Sub
</script>
</head>
<body onload="INI(0)">
<form>
<input type="button" value="Update INI" id="butt" onclick="INI(1)">
<br>
<textarea name="text" cols="80" rows="24"></textarea>
</form>
</body>
</html>



Re: Edit/Delete section of an ini file by mayayana

mayayana
Sun Nov 28 13:37:54 CST 2004

It's awkward to do. The two options are 1) to
read out one line at a time and then rewrite the file
from those lines, replacing the line with the value in
question. Or 2) use Instr to find "CASE_X=", then
use InStr to find the end of that line, then concatenate
the first part (up to "CASE_X=") with your new value
and the rest of the file (following the vbCrLf at the
end of that line.

This download may be useful:
http://www.jsware.net/jsware/scripts.html#classpk

It includes a class for reading and writing with INI
files. The class is created as an object and provides
all the basic INI file operations. It works by reading
the whole INI file out into a Dictionary object for easy
access to all keys and values.

--
_____________________________

mayayXXana1a@mindYYspring.com
For return email remove XX and YY.
_____________________________
fhillipo <fhillipo@discussions.microsoft.com> wrote in message
news:CA722256-B05C-47CC-BF23-751CE9947738@microsoft.com...
> I need to write a string value to a section of an ini file. I managed to
> write to it but it appends to existing values. I need to either edit the
> current value or delete the values in the section in question before I
write
> new values
>
> For example:
>
> file ="C:\VBS\myConfig.ini"
> Section = "ORACLE TO SQL SERVER"
> item = "CASE_X"
> Value = "XYZ"
>
> ini file output:
>
> [ORACLE TO SQL SERVER]
> CASE_X=ABC
> CASE_X=XYZ
> CASE_X=XYZ
> DB_NAME=YYY
> LSNAME=orclDB
> DTSERROR_O2S="C:\sqlConfig\dtsError\orcl_2_mssql\xxx.txt"
>
> How do I edit the original value of ABC to XYZ without having to write new
> lines as I need only I line for CASE_X Values. Any input would be greatly
> apreciated
>
> --
> fhillipo



Re: Edit/Delete section of an ini file by MikeB

MikeB
Sun Nov 28 13:44:11 CST 2004


"fhillipo" <fhillipo@discussions.microsoft.com> wrote in message
news:CA722256-B05C-47CC-BF23-751CE9947738@microsoft.com...
> I need to write a string value to a section of an ini file. I managed to
> write to it but it appends to existing values. I need to either edit the
> current value or delete the values in the section in question before I write
> new values
>
> For example:
>
> file ="C:\VBS\myConfig.ini"
> Section = "ORACLE TO SQL SERVER"
> item = "CASE_X"
> Value = "XYZ"
>
> ini file output:
>
> [ORACLE TO SQL SERVER]
> CASE_X=ABC
> CASE_X=XYZ
> CASE_X=XYZ
> DB_NAME=YYY
> LSNAME=orclDB
> DTSERROR_O2S="C:\sqlConfig\dtsError\orcl_2_mssql\xxx.txt"

Also, INI format has to have Name=Value pairs where Name is unique. That is
how INI files are accessed. In your example output, you have three endtries
with the same "Name" CASE_X.

> How do I edit the original value of ABC to XYZ without having to write new
> lines as I need only I line for CASE_X Values. Any input would be greatly
> apreciated
>
> --
> fhillipo



Re: Edit/Delete section of an ini file by Al

Al
Sun Nov 28 17:26:11 CST 2004


"MikeB" <m.byerleyATVerizonDottieNettie> wrote in message
news:%23lTBSKY1EHA.3452@TK2MSFTNGP14.phx.gbl...
>
> "fhillipo" <fhillipo@discussions.microsoft.com> wrote in message
> news:CA722256-B05C-47CC-BF23-751CE9947738@microsoft.com...
> > I need to write a string value to a section of an ini file. I managed to
> > write to it but it appends to existing values. I need to either edit the
> > current value or delete the values in the section in question before I
write
> > new values
> >
> > For example:
> >
> > file ="C:\VBS\myConfig.ini"
> > Section = "ORACLE TO SQL SERVER"
> > item = "CASE_X"
> > Value = "XYZ"
> >
> > ini file output:
> >
> > [ORACLE TO SQL SERVER]
> > CASE_X=ABC
> > CASE_X=XYZ
> > CASE_X=XYZ
> > DB_NAME=YYY
> > LSNAME=orclDB
> > DTSERROR_O2S="C:\sqlConfig\dtsError\orcl_2_mssql\xxx.txt"
>
> Also, INI format has to have Name=Value pairs where Name is unique.

Not in all cases. Consider how many "driver=" pairs there were in the
system.ini file in Win9x.

/Al

> That is
> how INI files are accessed. In your example output, you have three
endtries
> with the same "Name" CASE_X.
>
> > How do I edit the original value of ABC to XYZ without having to write
new
> > lines as I need only I line for CASE_X Values. Any input would be
greatly
> > apreciated
> >
> > --
> > fhillipo
>
>



Re: Edit/Delete section of an ini file by mayayana

mayayana
Sun Nov 28 17:56:10 CST 2004



> Not in all cases. Consider how many "driver=" pairs there were in the
> system.ini file in Win9x.
>

I'm on Win98SE and there are no driver= values
in my system.ini. There are, however, 6 device= values
under the 386Enh key. I never noticed that before.
Do you know how that gets handled? Maybe the
* after the "=" has a connection?




Re: Edit/Delete section of an ini file by MikeB

MikeB
Sun Nov 28 18:16:07 CST 2004


"mayayana" <mayaXXyana1a@mindYYspring.com> wrote in message
news:uAtqd.8609$Ua.253@newsread3.news.atl.earthlink.net...
>
>
> > Not in all cases. Consider how many "driver=" pairs there were in the
> > system.ini file in Win9x.
> >
>
> I'm on Win98SE and there are no driver= values
> in my system.ini. There are, however, 6 device= values
> under the 386Enh key. I never noticed that before.
> Do you know how that gets handled? Maybe the
> * after the "=" has a connection?

Yes, dumbshit me... It is just like a where clause.
It's where Name=SomeValue. Shoulda drank that coffee with the Espresso shot
before I answered ;-)

>
>



Re: Edit/Delete section of an ini file by mayayana

mayayana
Sun Nov 28 22:20:19 CST 2004

"A where clause" ? You mean like SQL?
I don't understand. There's nothing I can find in
the WritePrivateProfileString API docs that
indicates the possibility of writing two values
with the same key name, so I don't see how
it works.
_____________________________
>
> Yes, dumbshit me... It is just like a where clause.
> It's where Name=SomeValue. Shoulda drank that coffee with the Espresso
shot
> before I answered ;-)
>
> >
> >
>
>



RE: Edit/Delete section of an ini file by fhillipo

fhillipo
Mon Nov 29 00:57:02 CST 2004

Thanks for the input, guys. It's very much appreciated. However, I need to
point out that there are duplicate 'CASE_X=XYZ' string values under the
[ORACLE TO SQL SERVER SECTION] because each time I execute the vbscript code
it writes a new line under 'CASE_X=ABC'. What I set out to achieve was update
'ABC' to 'XYZ' and not write new lines under. As it stands if I execute the
code 10 times I get 10 new lines

"fhillipo" wrote:

> I need to write a string value to a section of an ini file. I managed to
> write to it but it appends to existing values. I need to either edit the
> current value or delete the values in the section in question before I write
> new values
>
> For example:
>
> file ="C:\VBS\myConfig.ini"
> Section = "ORACLE TO SQL SERVER"
> item = "CASE_X"
> Value = "XYZ"
>
> ini file output:
>
> [ORACLE TO SQL SERVER]
> CASE_X=ABC
> CASE_X=XYZ
> CASE_X=XYZ
> DB_NAME=YYY
> LSNAME=orclDB
> DTSERROR_O2S="C:\sqlConfig\dtsError\orcl_2_mssql\xxx.txt"
>
> How do I edit the original value of ABC to XYZ without having to write new
> lines as I need only I line for CASE_X Values. Any input would be greatly
> apreciated
>
> --
> fhillipo

Re: Edit/Delete section of an ini file by fhillipo

fhillipo
Mon Nov 29 01:01:07 CST 2004

Thanks so much. What I did originally was what you spelt out in option 2. I
get new lines written but I need only 1 line of 'CASE_X=???' with the
original 'ABC' update to 'XYZ'. I can read from ini files and I can write to
it but I'm struggling with updating values already written or if that's not
feasible deleting content of a section before writing the new line. Looking
4ward ti reading from you.

"mayayana" wrote:

> It's awkward to do. The two options are 1) to
> read out one line at a time and then rewrite the file
> from those lines, replacing the line with the value in
> question. Or 2) use Instr to find "CASE_X=", then
> use InStr to find the end of that line, then concatenate
> the first part (up to "CASE_X=") with your new value
> and the rest of the file (following the vbCrLf at the
> end of that line.
>
> This download may be useful:
> http://www.jsware.net/jsware/scripts.html#classpk
>
> It includes a class for reading and writing with INI
> files. The class is created as an object and provides
> all the basic INI file operations. It works by reading
> the whole INI file out into a Dictionary object for easy
> access to all keys and values.
>
> --
> _____________________________
>
> mayayXXana1a@mindYYspring.com
> For return email remove XX and YY.
> _____________________________
> fhillipo <fhillipo@discussions.microsoft.com> wrote in message
> news:CA722256-B05C-47CC-BF23-751CE9947738@microsoft.com...
> > I need to write a string value to a section of an ini file. I managed to
> > write to it but it appends to existing values. I need to either edit the
> > current value or delete the values in the section in question before I
> write
> > new values
> >
> > For example:
> >
> > file ="C:\VBS\myConfig.ini"
> > Section = "ORACLE TO SQL SERVER"
> > item = "CASE_X"
> > Value = "XYZ"
> >
> > ini file output:
> >
> > [ORACLE TO SQL SERVER]
> > CASE_X=ABC
> > CASE_X=XYZ
> > CASE_X=XYZ
> > DB_NAME=YYY
> > LSNAME=orclDB
> > DTSERROR_O2S="C:\sqlConfig\dtsError\orcl_2_mssql\xxx.txt"
> >
> > How do I edit the original value of ABC to XYZ without having to write new
> > lines as I need only I line for CASE_X Values. Any input would be greatly
> > apreciated
> >
> > --
> > fhillipo
>
>
>

Re: Edit/Delete section of an ini file by Torgeir

Torgeir
Mon Nov 29 02:10:18 CST 2004

fhillipo wrote:

> I need to write a string value to a section of an ini file. I managed to
> write to it but it appends to existing values. I need to either edit the
> current value or delete the values in the section in question before I write
> new values
Hi

You can use the FileSystemObject to handle INI files as
text files), for reading that is fine, but for writing you
should be sure that no other process is also trying to
update the INI file while your script is locking the file.

FileSystemObject example in this link, see the ReadIni function
and the WriteIni sub:

http://groups.google.com/groups?selm=3E8240AB.98EACD54%40hydro.com


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Re: Edit/Delete section of an ini file by alaniski

alaniski
Mon Nov 29 06:06:43 CST 2004

I use this regularly and have never had any problems with it.
Hope it helps!

'********To Write ini File********
'call WRITEINI (File,section,item,value)

' WRITEINI ( file, section, item, value )
' file = path and name of ini file
' section = [Section] must be in brackets
' item = the variable to read; the item must be immediately followed by =
'with no spaces.
' value = the value to assign to the item.

Sub WriteIni( file, section, item, value )

in_section = False
section_exists = False
item_exists = ( ReadIni( file, section, item ) <> "" )
wrote = False
path = Mid( file, 1, InStrRev( file, "\" ) )
Set fso = CreateObject("Scripting.FileSystemObject")
Set read_ini = fso.OpenTextFile( file, 1, True, TristateFalse )
Set write_ini = fso.CreateTextFile( path & "temp_ini.ini", False )

While read_ini.AtEndOfStream = False
line = read_ini.ReadLine

If wrote = False Then
If line = "[" & section & "]" Then
section_exists = True
in_section = True

ElseIf InStr( line, "[" ) = 1 Then
in_section = False
End If
End If

If in_section Then
If item_exists = False Then
write_ini.WriteLine line
write_ini.WriteLine item & "=" & value
wrote = True
in_section = False

ElseIf InStr( line, item & "=" ) = 1 Then
write_ini.WriteLine item & "=" & value
wrote = True
in_section = False

Else
write_ini.WriteLine line
End If
Else
write_ini.WriteLine line
End If
Wend

If section_exists = False Then ' section doesn't exist
write_ini.WriteLine
write_ini.WriteLine "[" & section & "]"
write_ini.WriteLine item & "=" & value
End If

read_ini.Close
write_ini.Close
fso.DeleteFile file
fso.MoveFile path & "temp_ini.ini", file
End Sub


'===========================================================
Function ReadIni( file, section, item )
ReadIni = ""
Set FileSysObj = CreateObject("Scripting.FileSystemObject")
If FileSysObj.FileExists( file ) Then
Set ini = FileSysObj.OpenTextFile( file, 1, False)

Do While ini.AtEndOfStream = False
line = ini.ReadLine

If line = "[" & section & "]" Then
line = ini.ReadLine
Do While Left( line, 1) <> "["
If InStr( line, item & "=" ) = 1 Then
ReadIni = mid( line, Len( item ) + 2 )
Exit Do
End If

If ini.AtEndOfStream Then Exit Do
line = ini.ReadLine
Loop
Exit Do
End If
Loop
ini.Close
End If
End Function


Alan van Wyk

Re: Edit/Delete section of an ini file by MikeB

MikeB
Mon Nov 29 08:59:54 CST 2004


"mayayana" <mayaXXyana1a@mindYYspring.com> wrote in message
news:7sxqd.3396$6K5.875@newsread2.news.atl.earthlink.net...
> "A where clause" ? You mean like SQL?
> I don't understand. There's nothing I can find in

No... I was right before I was wrong (phew!) .. Actually, I was thinking of an
improper useage of or misnaming of a file that I use for PdfMerge where I use
the same keyName multiple times in a section, and named the file ext "INI". A
complete misnomer, but convenient to recognize it as pertinent to an
application. I have been using Delphi as well and from the Delphi Docs:

procedure WriteString(const Section, Ident, Value: String); virtual; abstract;

you could fairly conclude the Name value Pairs have to be unique.

To test this, I put multiple Keys with the same name but with different values
and then wrote a new value to the KeyName and All the keys were updated with
the same value.

Apologies for the muckup..



> the WritePrivateProfileString API docs that
> indicates the possibility of writing two values
> with the same key name, so I don't see how
> it works.
> _____________________________
> >
> > Yes, dumbshit me... It is just like a where clause.
> > It's where Name=SomeValue. Shoulda drank that coffee with the Espresso
> shot
> > before I answered ;-)
> >
> > >
> > >
> >
> >
>
>



Re: Edit/Delete section of an ini file by mayayana

mayayana
Mon Nov 29 09:01:53 CST 2004

There won't be duplicates once you fix the method.
Also, if you use the read-by-line technique you can
add a function to check for duplicates. That's an
advantage of using the Dictionary in the
class that I provided the link for in the earlier post.
Dictionary has an
Exists method. So if you go through with Textstream,
reading a line at a time and storing them in a Dictionary,
it's easy to do something like the following with each line:

Pt = Instr(1, sLine, "=")
sKey = Left(sLine, (Pt - 1)) '-- get key
sVal = Right(sLine, (len(sLine) - Pt)) '-- get value
If Dic.Exists(sKey) = False then
Dic.Add sKey, sVal '-- if it's not a duplicate then add to Dict.
End If

That would weed out all duplicates and provide
a very easy way to then change values:
Dic.Item(sKey) = NewValue

Once you're done you can get arrays of
the Dictionary keys or items in a single call
and reassemble those to rewrite your file:
A1 = Dic.Keys

--
_____________________________

mayayXXana1a@mindYYspring.com
For return email remove XX and YY.
_____________________________
fhillipo <fhillipo@discussions.microsoft.com> wrote in message
news:3EFEF2F2-0AE3-4116-BA5D-2C7437B4D5C6@microsoft.com...
> Thanks for the input, guys. It's very much appreciated. However, I need to
> point out that there are duplicate 'CASE_X=XYZ' string values under the
> [ORACLE TO SQL SERVER SECTION] because each time I execute the vbscript
code
> it writes a new line under 'CASE_X=ABC'. What I set out to achieve was
update
> 'ABC' to 'XYZ' and not write new lines under. As it stands if I execute
the
> code 10 times I get 10 new lines
>
> "fhillipo" wrote:
>
> > I need to write a string value to a section of an ini file. I managed to
> > write to it but it appends to existing values. I need to either edit the
> > current value or delete the values in the section in question before I
write
> > new values
> >
> > For example:
> >
> > file ="C:\VBS\myConfig.ini"
> > Section = "ORACLE TO SQL SERVER"
> > item = "CASE_X"
> > Value = "XYZ"
> >
> > ini file output:
> >
> > [ORACLE TO SQL SERVER]
> > CASE_X=ABC
> > CASE_X=XYZ
> > CASE_X=XYZ
> > DB_NAME=YYY
> > LSNAME=orclDB
> > DTSERROR_O2S="C:\sqlConfig\dtsError\orcl_2_mssql\xxx.txt"
> >
> > How do I edit the original value of ABC to XYZ without having to write
new
> > lines as I need only I line for CASE_X Values. Any input would be
greatly
> > apreciated
> >
> > --
> > fhillipo



Re: Edit/Delete section of an ini file by mayayana

mayayana
Mon Nov 29 09:20:51 CST 2004

> you could fairly conclude the Name value Pairs have to be unique.
>
> To test this, I put multiple Keys with the same name but with different
values
> and then wrote a new value to the KeyName and All the keys were updated
with
> the same value.
>

That's interesting. Yet there must be some official
way of treating the values as an array if Windows
manages to write 6 "device=" keys all with different
values.



Re: Edit/Delete section of an ini file by MikeB

MikeB
Mon Nov 29 10:26:04 CST 2004


"mayayana" <mayaXXyana1a@mindYYspring.com> wrote in message
news:n7Hqd.9204$Ua.5942@newsread3.news.atl.earthlink.net...
> > you could fairly conclude the Name value Pairs have to be unique.
> >
> > To test this, I put multiple Keys with the same name but with different
> values
> > and then wrote a new value to the KeyName and All the keys were updated
> with
> > the same value.
> >
>
> That's interesting. Yet there must be some official
> way of treating the values as an array if Windows
> manages to write 6 "device=" keys all with different
> values.

That's because device is a [device] Section, not a key (I just looked at one of
the old 98 boxes downstairs). Under the device section there are about half a
dozen keys, all different.

>



Re: Edit/Delete section of an ini file by mayayana

mayayana
Mon Nov 29 18:09:07 CST 2004

No, I didn't say Device section. It's device
KEYS under the [386Enh] section, with
different values.
Sample:

device=*dynapage
device=*vcd
device=*vpd
device=*int13

--
_____________________________

mayayXXana1a@mindYYspring.com
For return email remove XX and YY.
_____________________________
MikeB <m.byerleyATVerizonDottieNettie> wrote in message
news:OJ9dNAj1EHA.1400@TK2MSFTNGP11.phx.gbl...
>
> "mayayana" <mayaXXyana1a@mindYYspring.com> wrote in message
> news:n7Hqd.9204$Ua.5942@newsread3.news.atl.earthlink.net...
> > > you could fairly conclude the Name value Pairs have to be unique.
> > >
> > > To test this, I put multiple Keys with the same name but with
different
> > values
> > > and then wrote a new value to the KeyName and All the keys were
updated
> > with
> > > the same value.
> > >
> >
> > That's interesting. Yet there must be some official
> > way of treating the values as an array if Windows
> > manages to write 6 "device=" keys all with different
> > values.
>
> That's because device is a [device] Section, not a key (I just looked at
one of
> the old 98 boxes downstairs). Under the device section there are about
half a
> dozen keys, all different.
>
> >
>
>



Re: Edit/Delete section of an ini file by Al

Al
Mon Nov 29 22:07:13 CST 2004


"mayayana" <mayaXXyana1a@mindYYspring.com> wrote in message
news:7sxqd.3396$6K5.875@newsread2.news.atl.earthlink.net...
> "A where clause" ? You mean like SQL?
> I don't understand. There's nothing I can find in
> the WritePrivateProfileString API docs that
> indicates the possibility of writing two values
> with the same key name, so I don't see how
> it works.

I don't know how long "WritePrivateProfileString" has been around, but there
is a possibility that it was preceded by the .ini file format. So, how *are*
duplicate keys properly processed? My guess - serially. Like the duplicate
"device=" (my bad, I thought it was "driver=") entries. It is fairly obvious
(to me at least) that this file was originally processed on startup by
reading through it looking for "device=" entries, rather than doing
enquiries for whatever might have matched.

/Al

> >
> > Yes, dumbshit me... It is just like a where clause.
> > It's where Name=SomeValue. Shoulda drank that coffee with the
Espresso
> shot
> > before I answered ;-)
> >
> > >
> > >
> >
> >
>
>



Re: Edit/Delete section of an ini file by mayayana

mayayana
Tue Nov 30 08:01:13 CST 2004

>> There's nothing I can find in
> > the WritePrivateProfileString API docs that
> > indicates the possibility of writing two values
> > with the same key name, so I don't see how
> > it works.
>
> I don't know how long "WritePrivateProfileString" has been around, but
there
> is a possibility that it was preceded by the .ini file format. So, how
*are*
> duplicate keys properly processed? My guess - serially. Like the duplicate
> "device=" (my bad, I thought it was "driver=") entries. It is fairly
obvious
> (to me at least) that this file was originally processed on startup by
> reading through it looking for "device=" entries, rather than doing
> enquiries for whatever might have matched.
>
It seems odd that MS would have carried over an older,
different INI format from Windows 3.1 or earlier without
updating the files. But your theory might be the only one that
fits, and it also makes sense because at load time the INI
files are probably read by WIN.COM or another DOS
executable that wouldn't know about Win32.

I tried GetPrivateProfileString on my system.ini
386Enh/device keys and the return, as expected, was just
the value of the first key.

So I guess that would mean that system.ini is an abberation,
along with the other Win9x boot INIs, but that in general the
key names must be unique in order for a file to function
as an INI file.




Re: Edit/Delete section of an ini file by MikeB

MikeB
Tue Nov 30 08:23:26 CST 2004


"mayayana" <mayaXXyana1a@mindYYspring.com> wrote in message
news:J2%qd.4435$6K5.3300@newsread2.news.atl.earthlink.net...
> >> There's nothing I can find in
> > > the WritePrivateProfileString API docs that
> > > indicates the possibility of writing two values
> > > with the same key name, so I don't see how
> > > it works.
> >
> > I don't know how long "WritePrivateProfileString" has been around, but
> there
> > is a possibility that it was preceded by the .ini file format. So, how
> *are*
> > duplicate keys properly processed? My guess - serially. Like the duplicate
> > "device=" (my bad, I thought it was "driver=") entries. It is fairly
> obvious
> > (to me at least) that this file was originally processed on startup by
> > reading through it looking for "device=" entries, rather than doing
> > enquiries for whatever might have matched.
> >
> It seems odd that MS would have carried over an older,
> different INI format from Windows 3.1 or earlier without
> updating the files. But your theory might be the only one that
> fits, and it also makes sense because at load time the INI
> files are probably read by WIN.COM or another DOS
> executable that wouldn't know about Win32.
>
> I tried GetPrivateProfileString on my system.ini
> 386Enh/device keys and the return, as expected, was just
> the value of the first key.
>
> So I guess that would mean that system.ini is an abberation,
> along with the other Win9x boot INIs, but that in general the
> key names must be unique in order for a file to function
> as an INI file.
>

I have found a couple of googles that state "The following entries are the
default entries for a new System.ini file:"
[386Enh]
device=*vshare
device=*dynapage
device=*vcd
device=*vpd
device=*int13
keyboard=*vkd
display=*vdd
mouse=*vmouse, msmouse.vxd
woafont=dosapp.fon
device=*enable

This would tend to indicate that the "device=" statements were never created
via API, but were created by the install program with those values for, as you
have surmised, access by programs/OS utilities that read the file in a NON API
method.



>



RE: Edit/Delete section of an ini file by fhillipo

fhillipo
Wed Jan 12 03:27:01 CST 2005

Thank you all for your input. Please find below the solution I eventually
went with:
It loops through a list of values (over 100 lines of 3 or 4 characters) in a
text file uses Regular Expression to update the ini file with the next value.
The DTS Package Global variable picks up the new value from the ini file to
run designated packages.

'*****************************************************
' Visual Basic ActiveX Script
'*****************************************************

Function Main()


'-----------------------------------Loop thru List of values

' Set up Constants
Const ForWriting = 2 ' Input OutPut mode
Const Create = True

Dim arrStartPhrase(128)
Dim strLine, strStartPhrase, strInputFile, strWriteLine
Dim boolFirstLine
Dim intArrayIndex
Dim f
Dim file, myExp, line
Dim imp73file, imp73path, read_cmd, write_cmd, line2

Dim fs ' FileSystemObject
Dim ts ' TextStreamObject

intArrayIndex = 0
boolFirstLine = true
strInputFile = "G:\Microsoft SQL
Server\MSSQL\sqlconfig\dcs_Cases\dcs_cases.txt" ' INPUT FILENAME
'strInputFile = "\\delphilip\dcsCases\dcs_cases.txt"

Set oFso = CreateObject("Scripting.FileSystemObject")
Set f = oFso.OpenTextFile(strInputFile, 1)
while f.AtEndOfStream = false
strLine = trim(f.ReadLine)
' msgbox "Active Value = " &strLine

'---------------------------------Update INI file

'Dim file, myExp, line, strline

file = "G:\Microsoft SQL
Server\MSSQL\sqlconfig\globalvariables\dcsconfig.ini"
'file = "\\delphilip\gv\dcsconfig.ini"

'Set oFSO = CreateObject("Scripting.FileSystemObject")
path = Mid( file, 1, InStrRev( file, "\" ) )
' Check if path to file exists, quit if not so
If Not oFSO.FolderExists ( path ) Then
MsgBox "Error: WriteIni failed, folder path to ini file " & file & " not
found!"
Wscript.Quit
End If

file = Trim(file)

Set read_ini = oFSO.OpenTextFile( file, 1, True, TristateFalse )
line=read_ini.readall
read_ini.close

set MyExp=new regexp
myexp.pattern="CASE_X=\w*"
myexp.ignorecase=true
myexp.global=true

line=myexp.replace(line,"CASE_X=" & strLine)
' msgbox "line = " & line

Set write_ini = oFSO.openTextFile( file, 2)
write_ini.write line
write_ini.close

'--------------------------------------Execute DTS Package

Dim DTSPackage
Set DTSPackage = CreateObject("DTS.Package")

DTSPackage.LoadFromSQLServer ".", , , 256,,,,
"Execute_Sp_Launch_Pkgs_ORCL_MSSQL" ' 256 For Trusted, 0 for SQL Server
Security

DTSPackage.FailOnError = True
DTSPackage.Execute
DTSPackage.Uninitialize()
Set DTSPackage = Nothing

wend
Main = DTSTaskExecResult_Success
End Function

"fhillipo" wrote:

> I need to write a string value to a section of an ini file. I managed to
> write to it but it appends to existing values. I need to either edit the
> current value or delete the values in the section in question before I write
> new values
>
> For example:
>
> file ="C:\VBS\myConfig.ini"
> Section = "ORACLE TO SQL SERVER"
> item = "CASE_X"
> Value = "XYZ"
>
> ini file output:
>
> [ORACLE TO SQL SERVER]
> CASE_X=ABC
> CASE_X=XYZ
> CASE_X=XYZ
> DB_NAME=YYY
> LSNAME=orclDB
> DTSERROR_O2S="C:\sqlConfig\dtsError\orcl_2_mssql\xxx.txt"
>
> How do I edit the original value of ABC to XYZ without having to write new
> lines as I need only I line for CASE_X Values. Any input would be greatly
> apreciated
>
> --
> fhillipo