Page 1 of 1 1
Topic Options
#188651 - 08/07/09 11:45 PM WriteProfileString
jmuirh Offline
Fresh Scripter

Registered: 07/09/22
Posts: 21
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.

Top
#188652 - 08/07/10 12:14 AM Re: WriteProfileString [Re: jmuirh]
Glenn Barnas Moderator Offline
KiX Supporter
*****

Registered: 03/01/28
Posts: 3843
Loc: New Jersey
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
_________________________
Visit the Inno-Tech web site for Kix tools/UDFs
Lost:Rocket, appx 8' tall, Green w/ yellow fins, nose, and computer bay.. Last seen streaking skyward in NY

Top
#188653 - 08/07/10 02:07 AM Re: WriteProfileString [Re: Glenn Barnas]
jmuirh Offline
Fresh Scripter

Registered: 07/09/22
Posts: 21
Thanks Glenn. Works great!
Top
#188654 - 08/07/10 02:38 AM Re: WriteProfileString [Re: jmuirh]
Glenn Barnas Moderator Offline
KiX Supporter
*****

Registered: 03/01/28
Posts: 3843
Loc: New Jersey
Cool! Thanks for the feedback!

Glenn
_________________________
Visit the Inno-Tech web site for Kix tools/UDFs
Lost:Rocket, appx 8' tall, Green w/ yellow fins, nose, and computer bay.. Last seen streaking skyward in NY

Top
Page 1 of 1 1


Moderator:  Bryce, Jochen, Howard Bullock, Allen, Sealeopard, Radimus, Kdyer, Chris S., Glenn Barnas, Benny69, ShaneEP, Mart 
Hop to:
Shout Box

Who's Online
1 registered (ShaneEP) and 15 anonymous users online.
Newest Members
Donet, mmichalak, Fedaykyn, DIStec, sambapdc
16801 Registered Users

Generated in 0.035 seconds in which 0.013 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org