Jump to content

Covert Reg to AU3


BrettF
 Share

Recommended Posts

Going through a lot of old scripts, came across this. It's probably not fully finished but here it is.

Converts .Reg Files to AU3.

Supports command line (When compiled)-

/INFILE- Specifies input .reg file. Leave blank to select a file using FileOpen

/OUTFILE- Specifies ouput .au3 file. Leave blank to copy AU3 output to clipboard.

/OVERWRITE- Only used when /OUTFILE is used. 0 = No Overwrite, will append to end of file, 1 = Overwrite, will clear file.

A 112MB reg file complete in just under 2 minutes. (2GB RAM, Intel Core 2 Duo 1.8GHz, Uncompiled.)

#include <GUIConstantsEx.au3>
Dim $infile = "", $outfile = "", $output = "", $overwrite = False, $offset = 0, $post = ""

;Command line for if compiled
If @Compiled And $CmdLineRaw Then
    $strings = StringSplit($CmdLineRaw, "/")
    For $i = 1 To $strings[0]
        $cmd = StringSplit($strings[$i], ' "', 1)
        If $cmd[0] <> 1 Then
            $param = StringReplace($cmd[1], '"', "")
            $param1 = StringReplace($cmd[2], '"', "")
            Switch $param
                Case "INFILE" 
                    $infile = $param1
                Case "OUTFILE" 
                    $outfile = $param1
                Case "OVERWRITE" 
                    If $param1 = 0 Then $overwrite = False
                    If $param1 = 1 Then $overwrite = True
            EndSwitch
        EndIf
    Next
EndIf

;If not compiled/no command line, ask user to select files
If $infile = "" Then $infile = FileOpenDialog("Open Reg File", "", "Registry Files (*.reg)")
If $infile = "" Then
    MsgBox(0, "", "Needs Input File!")
    Exit
EndIf
If $outfile = "" Then $outfile = FileSaveDialog("Save Output", "", "AutoIt V3 Scripts(*.au3)")

;If .au3 not on the end of outfile, add it.
If StringRight($outfile, 4) <> ".au3"  Then $outfile &= ".au3" 

;Start Timer
$timer = TimerInit()

;Read all Sections
$var = IniReadSectionNames($infile)

;Set Totals for Progress
$total1 = $var[0]
$total2 = 0

;Create GUI
$Form1 = GUICreate("Reg2Au3", 230, 100, 193, 115)
$Label1 = GUICtrlCreateLabel("", 8, 8, 214, 37)
$Progress1 = GUICtrlCreateProgress(8, 52, 214, 17)
GUICtrlSetLimit(-1, 100, 0)
$Progress2 = GUICtrlCreateProgress(8, 76, 214, 17)
GUICtrlSetLimit(-1, 100, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

If @error Then
    MsgBox(4096, "Error", "Error getting sections.")
Else
    For $i = 1 To $var[0]
        $var123 = StringReplace($var[$i], "'", "''")
        $text = $i & " /" & $total1 & " Sections Done - 1/1 Keys " & @CRLF & "Running for " & Round(TimerDiff($timer) / 1000, 2) & " seconds" 
        GUICtrlSetData($Label1, $text)
        If GUIGetMsg() = -3 Then Exit
        $percent1 = Round(($i / $total1) * 100, 0)
        GUICtrlSetData($Progress1, $percent1)
        $section = IniReadSection($infile, $var[$i])
        If @error Then
            $var123 = StringReplace($var123, "'", "''")
            $output &= "RegWrite ('" & $var123 & "')" & @CRLF
        Else
            $total2 = $section[0][0]
            For $a = 1 To $section[0][0]
                $text = $i & " /" & $total1 & " Sections Done - " & $a & "/" & $total2 & " Keys " & @CRLF & "Running for " & Round(TimerDiff($timer) / 1000, 2) & " seconds" 
                GUICtrlSetData($Label1, $text)
                If GUIGetMsg() = -3 Then Exit
                $percent2 = Round(($a / $total2) * 100, 0)
                GUICtrlSetData($Progress2, $percent2)
                $key = StringReplace($section[$a][0], "@", "")
                $value = $section[$a][1]
                $type = "REG_SZ" 
                $pos = StringInStr($value, ":")
                $str = StringLeft($value, $pos - 1)
                If @error <> 1 Then
                    $type = $str
                    If $type = "Hex"  Then
                        $type = "REG_BINARY" 
                        $value = StringReplace(StringTrimLeft($value, $pos), ",", "")
                    ElseIf $type = "dword"  Then
                        $type = "REG_DWORD" 
                        $value = StringTrimLeft($value, $pos)
                    ElseIf $type = "hex(7)"  Then
                        $type = "REG_MULTI_SZ" 
                        $value = StringTrimLeft($value, $pos)
                    ElseIf $type = "hex(2)"  Then
                        $type = "REG_EXPAND_SZ" 
                        $value = StringTrimLeft($value, $pos)
                    ElseIf $type = "hex(0)"  Then
                        $type = "REG_NONE" 
                        $value = StringTrimLeft($value, $pos)
                        $post = ";" 
                    Else
                        $type = "REG_SZ" 
                    EndIf
                EndIf
                If StringLeft($value, 1) = '"'  Then $value = StringTrimLeft($value, 1)
                If StringRight($value, 1) = '"'  Then $value = StringTrimRight($value, 1)
                $value = StringReplace($value, "'", "''")
                $key = StringReplace($key, "'", "''")
                $var123 = StringReplace($var123, "'", "''")
                If $type = "" Then
                    $type = ""
                ElseIf $key = "" Then
                    $key = ""
                EndIf
                $output &= $post & "RegWrite ('" & $var123 & "', '" & $key & "', '" & $type & "', '" & $value & "')" & @CRLF
                $post = ""
            Next
        EndIf
    Next
EndIf
;No outfile then paste to clipboard
If $outfile = "" Then
    ClipPut($output)
Else
    If $overwrite = True Then $offset = 10
    $file = FileOpen($outfile, 2 + $offset)
    FileWrite($file, $output)
    FileClose($file)
EndIf
GUIDelete($Form1)
MsgBox(0, "Complete!", "Process completed in " & Round(TimerDiff($timer) / 1000, 2) & "seconds")

Probably a one off release, I hope it helps someone- whether its parsing Reg files, or acutally using it to help in scripts.

Cheers,

Brett

:P

Edited by BrettF
Link to comment
Share on other sites

i found an old version here in this forum... which is very fast compare to yours...

tested with 25.6kb file:

yours take >>1.93s

the old one is >>0.16s (maybe because it doesnt have a progress bar...)

here's the source:

$Reg = FileOpenDialog("Choose a registry file", @ScriptDir, "Registry Files (*.reg)", 1)
If @error = 1 Then Exit

$Dir = StringLeft($Reg, StringInStr($Reg, '\', 0, -1) - 1)
$NewReg = FileSaveDialog("Choose a file name", $Dir, "Scripts (*.au3)", 16)
If StringRight($NewReg, 4) <> ".au3"  Then $NewReg &= ".au3"
If @error = 1 Then Exit
$timer = TimerInit()

RunWait(@ComSpec & ' /c TYPE "' & $Reg & '" > "%TEMP%\' & GetFilename($Reg) & '"', @SystemDir, @SW_HIDE)
$Reg = @TempDir & "\" & GetFilename($Reg)
$OpenReg = FileOpen($Reg, 0)
$OpenNewReg = FileOpen($NewReg, 2)
MsgBox(0, "Complete!", "Process completed in " & Round(TimerDiff($timer) / 1000, 2) & " seconds")

Dim $Key
Dim $valuename
Global $Oldvalue = ""
$value = ""
$Type = ""
$Write = "Yes"
$Continue1 = "No"
$Continue2 = "No"
$Continue3 = "No"

$Line = FileReadLine($OpenReg, 2)
$Line = ""

While 1
   $Line = FileReadLine($OpenReg)
   If @error Then ExitLoop
   
   If StringLeft($Line, 1) = ' ' Then
      $value = $Line
      
      If $Continue1 = "Yes" Then
         $Type = "REG_BINARY"
         If StringRight($value, 1) = "\" Then
            $value = StringTrimRight($value, 1)
            $Oldvalue = $Oldvalue & $value
            $Continue1 = "Yes"
            $Write = "No"
         Else
            $Oldvalue = $Oldvalue & $Line
            $Match1 = StringInStr($Oldvalue, ":")
            If $Match1 >= 1 Then $Oldvalue = StringTrimLeft($Oldvalue, $Match1)
            $value = StringReplace($Oldvalue, ",", "")
            $value = Chr(34) & StringStripWS($value, 8) & Chr(34)
            $Oldvalue = ""
            $Continue1 = "No"
            $Write = "Yes"
         EndIf
         
      ElseIf $Continue2 = "Yes" Then
         $Type = "REG_EXPAND_SZ"
         If StringRight($value, 1) = "\" Then
            $value = StringTrimRight($value, 1)
            $Oldvalue = $Oldvalue & $value
            $Continue2 = "Yes"
            $Write = "No"
         Else
            $Oldvalue = $Oldvalue & $Line
            $value = Chr(34) & CharsToString (StringStripWS($Oldvalue, 8)) & Chr(34)
            $Oldvalue = ""
            $Continue2 = "No"
            $Write = "Yes"
         EndIf
         
      ElseIf $Continue3 = "Yes" Then
         $Type = "REG_MULTI_SZ"
         If StringRight($value, 1) = "\" Then
            $value = StringTrimRight($value, 1)
            $Oldvalue = $Oldvalue & $value
            $Continue3 = "Yes"
            $Write = "No"
         Else
            $Oldvalue = $Oldvalue & $Line
            $value = Chr(34) & CharsToString (StringStripWS($Oldvalue, 8)) & Chr(34)
            $Oldvalue = ""
            $Continue3 = "No"
            $Write = "Yes"
         EndIf
      EndIf
      
      If $Write = "Yes" Then FileWrite($OpenNewReg, 'RegWrite("' & $Key & '", ' & $valuename & ', "' & $Type & '", ' & $value & ')' & @CRLF)
      
   EndIf
   
   If $Line <> "" Then
      Select
         Case StringLeft($Line, 1) = ";"
            FileWrite($OpenNewReg, $Line & @CRLF)
            
         Case StringLeft($Line, 1) = "["
            If StringRight($Line, 1) = " " Then
               $Key = StringTrimRight($Line, 2)
            Else
               $Key = StringTrimRight($Line, 1)
            EndIf
            $Key = StringTrimLeft($Key, 1)
            
            If StringLeft($Key, 1) = "-" Then FileWrite($OpenNewReg, 'RegDelete("' & StringTrimLeft($Key, 1) & '")' & @CRLF)
            
         Case StringLeft($Line, 1) = '@'
            $value = StringTrimLeft($Line, 2)
            $value = StringReplace($value, '\\', '\')
            If Not StringLen($value) = 2 Then $value = StringReplace($value, '""', '"')
            
            If StringLeft($value, 1) = "-" Then
               FileWrite($OpenNewReg, 'RegDelete("' & $Key & '", "")' & @CRLF)
            Else
               FileWrite($OpenNewReg, 'RegWrite("' & $Key & '", "", "REG_SZ", ' & $value & ')' & @CRLF)
            EndIf
            
         Case StringLeft($Line, 1) = '"'
            $line_split = StringSplit($Line, "=")
            $valuename = $line_split[1]
            $value = $line_split[2]
            
            If StringInStr($value, 'hex:') >= 1 Then
               $Type = "REG_BINARY"
               If StringRight($value, 1) = "\" Then
                  $value = StringTrimRight($value, 1)
                  $Oldvalue = $value
                  $Continue1 = "Yes"
                  $Write = "No"
               Else
                  $value = StringReplace($value, ",", "")
                  $Match1 = StringInStr($value, ":")
                  If $Match1 >= 1 Then $value = StringTrimLeft($value, $Match1)
                  $value = Chr(34) & StringStripWS($value, 8) & Chr(34)
                  $Continue1 = "No"
                  $Write = "Yes"
               EndIf
               $Match1 = StringInStr($value, ":")
               If $Match1 >= 1 Then $value = StringTrimLeft($value, $Match1)
            ElseIf StringInStr($value, "hex(2):") >= 1 Then
               $Type = "REG_EXPAND_SZ"
               If StringRight($value, 1) = "\" Then
                  $value = StringTrimRight($value, 1)
                  $Oldvalue = $value
                  $Continue2 = "Yes"
                  $Write = "No"
               Else
                  $value = Chr(34) & CharsToString (StringStripWS($value, 8)) & Chr(34)
                  $Continue2 = "No"
                  $Write = "Yes"
               EndIf
               
            ElseIf StringInStr($value, "hex(7)") >= 1 Then
               $Type = "REG_MULTI_SZ"
               If StringRight($value, 1) = "\" Then
                  $value = StringTrimRight($value, 1)
                  $Oldvalue = $value
                  $Continue3 = "Yes"
                  $Write = "No"
               Else
                  $value = Chr(34) & CharsToString (StringStripWS($value, 8)) & Chr(34)
                  $Continue3 = "No"
                  $Write = "Yes"
               EndIf
               
            ElseIf StringLeft($value, 5) = "dword" Then
               $Type = "REG_DWORD"
               $value = StringTrimLeft($value, 6)
               If StringLeft($value, 1) = "0" Then
                  For $i = 1 To StringLen($value)
                     $Char = StringMid($value, $i, 1)
                     If $Char <> "0" Then ExitLoop
                  Next
                  
                  $value = StringTrimLeft($value, ($i - 1))
                  If $value = "" Then $value = "0"
               EndIf
               
               If StringLeft($value, 1) <> '"' And StringRight($value, 1) <> '"' Then $value = '"' & $value & '"'
               
            Else
               
               $Type = "REG_SZ"
            EndIf
            
            $value = StringReplace($value, '\\', '\')
            If Not StringLen($value) = 2 Then $value = StringReplace($value, '""', '"')
            
            If StringLeft($value, 1) = "-" Then
               FileWrite($OpenNewReg, 'RegDelete("' & $Key & '", ' & $valuename & ')' & @CRLF)
            Else
               If $Write = "Yes" Then FileWrite($OpenNewReg, 'RegWrite("' & $Key & '", ' & $valuename & ', "' & $Type & '", ' & $value & ')' & @CRLF)
            EndIf
            
      EndSelect
      
   Else
      FileWrite($OpenNewReg, @CRLF)
   EndIf
Wend

FileClose($OpenNewReg)
Exit

Func CharsToString ($Instring)
   Local $Match1
   Local $Type
   
   $Match1 = StringInStr($Instring, ":")
   If $Match1 >= 1 Then $Instring = StringTrimLeft($Instring, $Match1)
   
   Dim $Temparray, $Count
   $Instring = StringReplace($Instring, " ", "")
   
   ;Remove trailing nulls 
   While StringRight($Instring, 3) = ",00"
      $Instring = StringTrimRight($Instring, 3)
   Wend
   
   ;Create an array of character values and build string
   $Temparray = StringSplit($Instring, ",")
   $Instring = ""
   $x = 0
   For $Count = 1 To $Temparray[0] 
      If $Temparray[$Count] <> "00" Then
         $Instring = $Instring & Chr(Dec($Temparray[$Count])) ;convert hex to dec then get character value and append to return string
         $x = 0
      Else
         $x = $x + 1
         If $x = 3 Then
            $x = 0
            $Instring = $Instring & '" & @LF & "'
         EndIf
      EndIf
   Next
   Return $Instring
   
EndFunc

Func Debug ($message)
   Select
      Case $message = "#open"
         Opt ("WinTitleMatchMode", 2)
         Run("notepad")
         WinWait("Untitled", "")
         WinSetTitle("Untitled", "", "Debug Window")
         
      Case Else
         ControlSend("Debug Window", "", "Edit1", $message)
         
   EndSelect
EndFunc   ;==>Debug

Func GetFilename($Path)
   Local $TempArr
   Local $Filename
   $TempArr = StringSplit($Path, "\")
   If @error Then
      $Filename = $Path
   Else
      $Filename = $TempArr[UBound($TempArr) - 1]
   EndIf
   Return $Filename
EndFunc
Link to comment
Share on other sites

  • 3 weeks later...
  • 7 months later...

if the key have many lines of values like this

[HKEY_LOCAL_MACHINE\SOFTWARE\Adobe\Adobe Acrobat\7.0\FeatureLockDown\cDefaultLaunchAttachmentPerms]

"sBuiltInPermList"=hex:76,65,72,73,69,6f,6e,3a,31,7c,2e,61,64,65,3a,33,7c,2e,\

61,64,70,3a,33,7c,2e,61,70,70,3a,33,7c,2e,61,73,70,3a,33,7c,2e,62,61,73,3a,\

33,7c,2e,62,61,74,3a,33,7c,2e,62,7a,3a,33,7c,2e,62,7a,32,3a,33,7c,2e,63,65,\

72,3a,33,7c,2e,63,68,6d,3a,33,7c,2e,63,6c,61,73,73,3a,33,7c,2e,63,6d,64,3a,\

33,7c,2e,63,6f,6d,3a,33,7c,2e,63,6f,6d,6d,61,6e,64,3a,33,7c,2e,63,70,6c,3a,\

33,7c,2e,63,72,74,3a,33,7c,2e,63,73,68,3a,33,7c,2e,65,78,65,3a,33,7c,2e,66,\

78,70,3a,33,7c,2e,67,7a,3a,33,7c,2e,68,65,78,3a,33,7c,2e,68,6c,70,3a,33,7c,\

2e,68,71,78,3a,33,7c,2e,68,74,61,3a,33,7c,2e,69,6e,66,3a,33,7c,2e,69,6e,69,\

3a,33,7c,2e,69,6e,73,3a,33,7c,2e,69,73,70,3a,33,7c,2e,69,74,73,3a,33,7c,2e,\

6a,6f,62,3a,33,7c,2e,6a,73,3a,33,7c,2e,6a,73,65,3a,33,7c,2e,6b,73,68,3a,33,\

7c,2e,6c,6e,6b,3a,33,7c,2e,6c,7a,68,3a,33,7c,2e,6d,61,64,3a,33,7c,2e,6d,61,\

66,3a,33,7c,2e,6d,61,67,3a,33,7c,2e,6d,61,6d,3a,33,7c,2e,6d,61,71,3a,33,7c,\

2e,6d,61,72,3a,33,7c,2e,6d,61,73,3a,33,7c,2e,6d,61,74,3a,33,7c,2e,6d,61,75,\

3a,33,7c,2e,6d,61,76,3a,33,7c,2e,6d,61,77,3a,33,7c,2e,6d,64,61,3a,33,7c,2e,\

6d,64,65,3a,33,7c,2e,6d,64,74,3a,33,7c,2e,6d,64,77,3a,33,7c,2e,6d,64,7a,3a,\

33,7c,2e,6d,73,63,3a,33,7c,2e,6d,73,69,3a,33,7c,2e,6d,73,70,3a,33,7c,2e,6d,\

73,74,3a,33,7c,2e,6f,63,78,3a,33,7c,2e,6f,70,73,3a,33,7c,2e,70,63,64,3a,33,\

7c,2e,70,69,66,3a,33,7c,2e,70,72,66,3a,33,7c,2e,70,72,67,3a,33,7c,2e,70,73,\

74,3a,33,7c,2e,72,61,72,3a,33,7c,2e,72,65,67,3a,33,7c,2e,73,63,66,3a,33,7c,\

2e,73,63,72,3a,33,7c,2e,73,63,74,3a,33,7c,2e,73,65,61,3a,33,7c,2e,73,68,62,\

3a,33,7c,2e,73,68,73,3a,33,7c,2e,73,69,74,3a,33,7c,2e,74,61,72,3a,33,7c,2e,\

74,67,7a,3a,33,7c,2e,74,6d,70,3a,33,7c,2e,75,72,6c,3a,33,7c,2e,76,62,3a,33,\

7c,2e,76,62,65,3a,33,7c,2e,76,62,73,3a,33,7c,2e,76,73,6d,61,63,72,6f,73,3a,\

33,7c,2e,76,73,73,3a,33,7c,2e,76,73,74,3a,33,7c,2e,76,73,77,3a,33,7c,2e,77,\

65,62,6c,6f,63,3a,33,7c,2e,77,73,3a,33,7c,2e,77,73,63,3a,33,7c,2e,77,73,66,\

3a,33,7c,2e,77,73,68,3a,33,7c,2e,7a,69,70,3a,33,7c,2e,7a,6c,6f,3a,33,7c,2e,\

7a,6f,6f,3a,33,7c,2e,70,64,66,3a,32,7c,2e,66,64,66,3a,32,00

Then it only get the first line only

RegWrite ('HKEY_LOCAL_MACHINE\SOFTWARE\Adobe\Adobe Acrobat\7.0\FeatureLockDown\cDefaultLaunchAttachmentPerms', '"sBuiltInPermList"', 'REG_BINARY', '76657273696f6e3a317c2e6164653a337c2e\')

Also missing many lines. 65.5MB reg file run 282s only out 100KB with 187 lines. While run the code posted by mikiutama out 10MB with 17000+ lines.

Maybe need some changes :D

Edited by nguyenbason
UnderWorldVN- Just play the way you like it
Link to comment
Share on other sites

  • 8 months later...

mikiutama

$result=StringRegExpReplace($result,'\n\r?\n\r?',"")

REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDlls]
"X:\\I386\\system32\\MSVCP60.DLL"=dword:00000006

[HKEY_CLASSES_ROOT\CakewalkBundleFile\Shell\Open\Command]
@="\"X:\\Programs\\Cakewalk\\Music Creator Pro24 2004\\CWMC.EXE\" \"%1\""

RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDlls", "X:\\I386\\system32\\MSVCP60.DLL", "REG_DWORD", "6")

RegWrite("HKEY_CLASSES_ROOT\CakewalkBundleFile\Shell\Open\Command", "", "REG_SZ", "\"X:\Programs\Cakewalk\Music Creator Pro24 2004\CWMC.EXE\" \"%1\"")

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