Jump to content

ini to reg converter


Mat
 Share

Recommended Posts

I always begin a project with 3 or 4 settings in a small ini, and soon get to the stage where I realise I should probably be using the registry instead. It is quite a long job to edit every single setting to use the registry, so I wrote this. It's a lot smaller than I thought it would be, and works perfectly from what i've tested. I also decided that making it user friendly is pointless, as its a tool for programmers, who should not have to use it very often.

; ini to reg

$sFile = FileOpenDialog ("Open script", @Workingdir, "Autoit Scripts (*.au3)|All Files (*.*)")
If @Error Then Exit

$sRegKey = InputBox ("Key", "What do you want the registry key to be called?", "", " M")
If @Error Then Exit

$sRegKey = """HKEY_LOCAL_MACHINE\\SOFTWARE\\" & $sRegKey & "\\"" & "

$sData = FileRead ($sFile)

$aIni = StringRegExp ($sData, "(?i)(?:Ini.*?)\s*\((.*?),", 3)
$sIni = "(?:\Q" & $aIni[0] & "\E"

For $i = 0 to UBound ($aIni) - 1
   If $aIni[$i] <> $sIni Then $sIni &= "|\Q" & $aIni[$i] & "\E"
Next
$sIni &= ")"

;write
$sData = StringRegExpReplace ($sData, "(?i)Iniwrite\s*\(" & $sIni & ",\s*(.*?),\s*(.*?),\s*(.*?)\)", _
   "RegWrite (" & $sRegKey & "\1, \2, ""REG_SZ"", \3)")

; read
$sData = StringRegExpReplace ($sData, "(?i)Iniread\s*\(" & $sIni & ",\s*(.*?),\s*(.*?),\s*(.*?)\)", _
   "_RegRead (" & $sRegKey & "\1, \2, \3)")

If @Extended > 0 Then $sData &= @CRLF & @CRLF & "Func _RegRead ($key, $name, $ret)" & @CRLF & _
@TAB & "Local $Result = RegRead ($key, $name)" & @CRLF & _
@TAB & "If @Error Then Return SetError (@Error, 0, $ret)" & @CRLF & _
@TAB & "Return SetError (@Error, 0, $Result)" & @CRLF & _
"EndFunc   ;==>_RegRead"

; read section
$sData = StringRegExpReplace ($sData, "(?i)Inireadsection\s*\(" & $sIni & ",\s*(.*?)\)", _
   "_RegReadSection (" & $sRegKey & "\1)")
If @Extended > 0 Then $sData &= @CRLF & @CRLF & "Func _RegReadSection ($key)" & @CRLF & _
@TAB & "Local $aRet[1][2]" & @CRLF & _
@TAB & @TAB & "$aRet[0][0] = 0" & @CRLF & _
@TAB & "$i = 0" & @CRLF & _
@TAB & "While 1" & @CRLF & _
@TAB & @TAB & "$i += 1" & @CRLF & _
@TAB & @TAB & "$var = RegEnumVal($sRegKey, $i)" & @CRLF & _
@TAB & @TAB & "If @error <> 0 Then ExitLoop" & @CRLF & _
@TAB & @TAB & "ReDim $aRet[UBound ($aRet, 1) + 1][2]" & @CRLF & _
@TAB & @TAB & "$aRet[UBound ($aRet, 1) - 1][0] = $var" & @CRLF & _
@TAB & @TAB & "$aRet[UBound ($aRet, 1) - 1][1] = RegRead ($sRegKey, $var)" & @CRLF & _
@TAB & @TAB & "If @Error Then ReDim $aRet[UBound ($aRet, 1) - 1][2]" & @CRLF & _
@TAB & @TAB & "$aRet[0][0] += 1" & @CRLF & _
@TAB & "WEnd" & @CRLF & _
@TAB & "Return $aRet" & @CRLF & _
"Endfunc   ;==>_RegReadSection"

; Read names
$sData = StringRegExpReplace ($sData, "(?i)Inireadsectionnames\s*\(" & $sIni & ",\s*(.*?)\)", _
   "_RegReadSectionNames (" & $sRegKey & "\1)")
If @Extended > 0 Then $sData &= @CRLF & @CRLF & "Func _RegReadSectionNames ($key)" & @CRLF & _
@TAB & "Local $aRet[1]" & @CRLF & _
@TAB & @TAB & "$aRet[0] = 0" & @CRLF & _
@TAB & "$i = 0" & @CRLF & _
@TAB & "While 1" & @CRLF & _
@TAB & @TAB & "$i += 1" & @CRLF & _
@TAB & @TAB & "$var = RegEnumKey($sRegKey, $i)" & @CRLF & _
@TAB & @TAB & "If @error <> 0 Then ExitLoop" & @CRLF & _
@TAB & @TAB & "ReDim $aRet[UBound ($aRet, 1) + 1]" & @CRLF & _
@TAB & @TAB & "$aRet[UBound ($aRet, 1) - 1] = $var" & @CRLF & _
@TAB & @TAB & "If @Error Then ReDim $aRet[UBound ($aRet, 1) - 1]" & @CRLF & _
@TAB & @TAB & "$aRet[0][0] += 1" & @CRLF & _
@TAB & "WEnd" & @CRLF & _
@TAB & "Return $aRet" & @CRLF & _
"Endfunc   ;==>_RegReadSectionNames"

; Delete
$sData = StringRegExpReplace ($sData, "(?i)Inidelete\s*\(" & $sIni & ",\s*(.*?),\s*(.*?)\)", _
   "RegDelete (" & $sRegKey & "\1, \2)")

; File delete:
$sData = StringRegExpReplace ($sData, "(?i)FileDelete\s*\(" & $sIni & "\)", "RegDelete (" & $sRegKey & ")")

; File*
$sData = StringRegExpReplace ($sData, "(?i)(File.*\(" & $sIni & "\s*,.*)", ";~ \1")

$sData = StringReplace ($sData, """ & """, "")

$sFile = FileSaveDialog ("Save new script", StringRegExpReplace ($sfile, ".*\\", ""), "Autoit script (*.au3)|All Files (*.*)")
If @Error Then Exit

FileWrite ($sFile, $sData)

two things left:

* seperate registry entries for different ini's (it currently detects multiple, but throws them all in the same key)

* same thing backwards

Mat

Edited by Mat
Link to comment
Share on other sites

Thank you for creating this example and sharing it with us.

IMO - registry can be indeed used to store/retrieve data (like an ini file) but, manipulating the registry in infinitely more dangerous than having an ini file somewhere. I agree - registry manipulation can be 99.99% accurate but, if something goes wrong you could pay dearly.

I prefer to use registry just to store some info (install path, log file, ini file ...) for installed programs.

But that's only me :)

Keep up the good work.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

The registry was inititially designed as a central storage point for data. the fact that its now so "dangerous" to use is becouse literally everything is stored in there, However, registry manipulation is so important to windows that you might as well learn to do it properly! Writing to a fixed key like this does under HKLM\SOFTWARE, the wosrst you could do is type in a key name that already exists. And I could put a check in for that, but since you can do the same with inis (ever written an autorun.inf to te c drive? haven't tried to yet...), and since this is a tool for the computer know-hows, I think its safe.

The registry also has speed gains, i don't know how much, but i'm fairly sure it does. I noticed a difference when I converted one of my bigger scripts though, regread-ing is very fast.

Mat

Edited by Mat
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...