Jump to content

Use IniRead against INI like file


Cyri
 Share

Recommended Posts

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.

Link to comment
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?
Link to comment
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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...