jmuirh
(Fresh Scripter)
2008-07-09 11:45 PM
WriteProfileString

Hi,

Wondering if anyone here is aware of a way to make WriteProfileString useful when attempting to write or update INI-like configuration files that don't have or use [SECTION] structure.

In other words, the file is not broken into (and has no) sections, instead it's just:

parm1=xxxxx
parm2=yyyyyyyyyyyy
.....
parmx=zzzzzzzzzzzzzzzzzzzzzz

I've tested it a little. It's not possible to omit or provide a blank parm2 when calling WriteProfileString and see that it doesn't work.

I know it'd be fairly easy to write something to read/write the file, but WriteProfileString is so much easier.

Any ideas?

Thanks.


Glenn BarnasAdministrator
(KiX Supporter)
2008-07-10 12:14 AM
Re: WriteProfileString

The ProfileString functions work with INI files, which have a well defined structure. There's no way to make them work with files that don't have that structure. However...

Let's say you have a file like you describe, called "config.!INI" (!="not"). You could temporarily and quickly "convert" it to an INI file with the following four lines:
 Code:
$ = RedirectOutput('.\config.ini')
'[SECTION]' ?
Display 'config.!INI'
$ = RedirectOutput('')

Now, your non-ini file has a header called "SECTION". Use EnumIni to get a list of all values in that section.
 Code:
$Values = EnumINI('.\config.ini', 'SECTION')

Then you can enumerate the list of values in the $Values array and use
ReadProfileString('.\config.ini', 'SECTION', $Value)
to read each value into an array, or even use the INI2Array UDF to shortcut things a bit. Let's assume you create a $Data array to hold the data that corresponds with each value.

Writing things back could go two ways - you could write the entire array back to a new file, or use WriteProfileString to update just a few values, and then remove the first line containing "[SECTION]".
 Code:
Del 'config.!INI'
$ = RedirectOutput('config.!INI')
For $I = 0 to UBound($Values)
  $Value[$I] '=' $Data[$I] ?        ; write the value=data pair
Next
[code]
or, assuming you've used WriteProfileString to update your temporary file
[code]
$Cmd = '%COMSPEC% /c find.exe /v "[SECTION]" .\config.ini > .\config.!ini'
Shell $Cmd

Of course, you could open, read, skip, then read / write loop until done in Kix, but the Find command will be much faster and easier to code.

The find command opens & reads the config.ini, looking for "[CONFIG]" (it's case sensitive!!!), and with /V will output every line that does NOT contain the search string.

Hope that gets you going...

Glenn


jmuirh
(Fresh Scripter)
2008-07-10 02:07 AM
Re: WriteProfileString

Thanks Glenn. Works great!

Glenn BarnasAdministrator
(KiX Supporter)
2008-07-10 02:38 AM
Re: WriteProfileString

Cool! Thanks for the feedback!

Glenn