Sign in to follow this  
Followers 0
Cyri

Use IniRead against INI like file

6 posts in this topic

I have some files that resemble an INI file, but they don't have any sections. It's basically one big file looking like this...

X=1

Y=2

Blah=false

The built-in INI functions of AutoIT don't work when there's no section name. Is there an easy way to pull out these values or am I going to have to loop through and do a readline through the file? Then do some string manipulation based on the equals sign.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Use StringRegExp

StringRegExp (FileRead ($sFile), "(?i)\Q" & $sKey & "\E=(.*)", 1)

The result will be in the 0th element.

Mat

Edit: Made it case insensitive.

Edited by Mat

Share this post


Link to post
Share on other sites

Use StringRegExp

StringRegExp (FileRead ($sFile), "(?i)\Q" & $sKey & "\E=(.*)", 1)

The result will be in the 0th element.

Mat

Edit: Made it case insensitive.

Looks like that may work for what I need. Question: why does it only return the first match? The flag = 1 on the regex should return an array of matches but it doesn't seem to work. Also, is there a way to strip out the @CR in the StringRegExp matches so I don't have to strip them out afterwards?

Share this post


Link to post
Share on other sites

Why not just stick a dummy [whatever] in front of it ?

(cheating, but effective )

whim

Share this post


Link to post
Share on other sites

Why not just stick a dummy [whatever] in front of it ?

(cheating, but effective )

whim

In front of what exactly? I have no control over the values that get written into the file.

Share this post


Link to post
Share on other sites

I presumed you only wanted the first... Change the flag to 3.

Func _IniLikeRead ($sFile, $sKey, $sDefault = "")
   Local $asRet = StringRegExp (FileRead ($sFile), "(?:\n|\n|\f|\a)\Qblah\E=(.*?)(?:\r|\n|\f|\z)", 3), $sRet = "|"
   If @Error Then Return SetError (1, 0, $sDefault)
   For $i = 0 to UBound ($asRet) - 1
      If StringinStr ($sRet, "|" & $asRet[$i] & "|") Then ContinueLoop
      $sRet &= $asRet[$i] & "|"
   Next
   Return SetExtended ($i, StringTrimLeft (StringTrimRight ($sRet, 1), 1))
EndFunc ; ==> _IniLikeRead

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0