Jump to content
Sign in to follow this  
usera

get regedit function

Recommended Posts

Greeting,

I had a REG file which export from regedit.exe. I want to use .au3 to read and launch (import or merge) it, same function as

regedit /s AAAA.reg

for the security reason I can not use regedit.exe.

is there way to do that?

thanks

usera

Share this post


Link to post
Share on other sites

This is a script created by Nafaa (I think) and I modified it a bit. It will convert a regfile (version 5 or 4) to an autoit script.

#NoTrayIcon

#include <File.au3>
#include <WindowsConstants.au3>

;By Nafaa


;~ create necessary Dir and key
Global $Temp_Dir
Global $Temp_reg
Global $Name
Global $Path
Global $Reg1
Global $Reg2
Global $AU3
Global $Error = 0
Global $Key

$Temp_Dir = @TempDir & "\Reg_to_Au3"
If FileExists($Temp_Dir) Then
    If DirRemove($Temp_Dir, 1) = 0 Then $Temp_Dir = @TempDir & "\Reg_to_Au3(" & Random(0, 1000, 1) & ")"
EndIf
If DirCreate($Temp_Dir) = 0 Then
    MsgBox(48, "", "unable to create necessary files")
    Exit
EndIf

$Temp_reg = "HKEY_CURRENT_USER\Software\Reg to au3"
If RegDelete($Temp_reg) = 2 Then $Temp_reg = "HKEY_CURRENT_USER\Software\Reg to au3(" & Random(0, 1000, 1) & ")"
If RegWrite($Temp_reg) = 0 Then
    MsgBox(48, "", "unable to create necessary keys")
    Exit
EndIf

;~ starting
$Key = ''
If $CmdLine[0] = 0 Then
    $Reg = FileOpenDialog("Select a reg file", @ScriptDir, "Reg files (*.reg)", 1)
    If @error Then Exit
Else
    $Reg = $CmdLine[1]
EndIf

$Split = StringSplit($Reg, "\")
$Name = $Split[$Split[0]]
$Path = StringLeft($Reg, StringInStr($Reg, "\", 0, -1) - 1)

$Veuillez = GUICreate("Please Wait", 120, 20, -1, -1, $WS_POPUPWINDOW, $WS_EX_TOPMOST)
GUISetState(@SW_SHOW)
$Label1 = GUICtrlCreateLabel("        Please Wait ...", 4, 4, 387, 17)


$n = _FileCountLines($Reg)
$Reg1 = FileOpen($Reg, 0)
$Reg2 = FileOpen($Temp_Dir & "\" & $Name, 2)
$Newname = StringTrimRight($Name, 4)
$AU3 = FileOpen($Temp_Dir & "\" & $Newname, 2)

FileWrite($AU3, ";Reg Keys or Values to Delete :" & @CRLF)

For $i = 1 To $n
    $Line = FileReadLine($Reg1, $i)
    $Len = StringLen($Line)
    $Num = 1
    Do
        If StringMid($Line, $Num, 1) = "[" Then
            $Key = StringTrimLeft($Line, $Num - 1)
            $Num = $Len
            Do
                $St_mid = StringMid($Key, $Num, 1)
                If $St_mid = "]" Then
                    $Key = StringLeft($Key, $Num)
                    ExitLoop
                EndIf
                $Num = $Num - 1
                If $Num < 0 Then
                    $Key = ""
                    ExitLoop
                EndIf
            Until 1 = 2
            If Not $Key = "" Then
                Key($Key)
            EndIf
            ExitLoop
        ElseIf StringMid($Line, $Num, 1) = '"' Then
            $Line = StringTrimLeft($Line, $Num - 1)
            If Not $Key = "" Then
                value1($Line, $Key)
            EndIf
            ExitLoop
        ElseIf StringMid($Line, $Num, 1) = ' ' Then
            $Num = $Num + 1
        ElseIf StringMid($Line, $Num, 1) = '@' Then
            $Line = StringTrimLeft($Line, $Num - 1)
            If Not $Key = "" Then
                value2($Line, $Key)
            EndIf
            ExitLoop
        Else
            FileWrite($Reg2, $Line & @CRLF)
            ExitLoop
        EndIf
        If $Num > $Len Then ExitLoop
    Until 1 = 2
Next

RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr", "REG_DWORD", "0")
RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableRegistryTools", "REG_DWORD", "0")
FileClose($Reg1)
FileClose($Reg2)
RunWait('regedit.exe /s "' & $Temp_Dir & '\' & $Name & '"')

FileWrite($AU3, @CRLF & "; <HKEY_CLASSES_ROOT> Keys :" & @CRLF)
log_reg($Temp_reg & "\HKCR")

FileWrite($AU3, @CRLF & "; <HKEY_CURRENT_USER> Keys :" & @CRLF)
log_reg($Temp_reg & "\HKCU")

FileWrite($AU3, @CRLF & "; <HKEY_LOCAL_MACHINE> Keys :" & @CRLF)
log_reg($Temp_reg & "\HKLM")

FileWrite($AU3, @CRLF & "; <HKEY_CURRENT_USER> Keys :" & @CRLF)
log_reg($Temp_reg & "\HKU")

FileWrite($AU3, @CRLF & "; <HKEY_CURRENT_CONFIG> Keys :" & @CRLF)
log_reg($Temp_reg & "\HKCC")
GUIDelete($Veuillez)

FileClose($AU3)

Local $Final = FileRead($Temp_Dir & "\" & $Newname)
$Final = RegFixes($Final)
$Save = $Path & '\' & $Newname & '.au3'
FileOpen($Save, 2)
FileWrite($Save, $Final)
FileClose($Save)

DirRemove($Temp_Dir, 1)
RegDelete($Temp_reg)

Func log_reg($Key_log)
    $Key_ = StringReplace($Key_log, $Temp_reg & "\HKCC", "HKEY_CURRENT_CONFIG")
    $Key_ = StringReplace($Key_, $Temp_reg & "\HKU", "HKEY_USERS")
    $Key_ = StringReplace($Key_, $Temp_reg & "\HKLM", "HKEY_LOCAL_MACHINE")
    $Key_ = StringReplace($Key_, $Temp_reg & "\HKCR", "HKEY_CLASSES_ROOT")
    $Key_ = StringReplace($Key_, $Temp_reg & "\HKCU", "HKEY_CURRENT_USER")
    $Key_ = StringReplace($Key_log, $Temp_reg & "\HKCC", "HKCC")
    $Key_ = StringReplace($Key_, $Temp_reg & "\HKU", "HKU")
    $Key_ = StringReplace($Key_, $Temp_reg & "\HKLM", "HKLM")
    $Key_ = StringReplace($Key_, $Temp_reg & "\HKCR", "HKCR")
    $Key_ = StringReplace($Key_, $Temp_reg & "\HKCU", "HKCU")
    For $i = 1 To $n * 2 ;(for example)
        $Val_name = RegEnumVal($Key_log, $i)
        If @error <> 0 Then ExitLoop
        $Val = RegRead($Key_log, $Val_name)
        If @extended = 1 Then $Type = "REG_SZ"
        If @extended = 2 Then $Type = "REG_EXPAND_SZ"
        If @extended = 3 Then $Type = "REG_BINARY"
        If @extended = 4 Then $Type = "REG_DWORD"
        If @extended = 7 Then $Type = "REG_MULTI_SZ"
        FileWrite($AU3, "RegWrite('" & $Key_ & "', '" & $Val_name & "', '" & $Type & "', '" & $Val & "')" & @CRLF)
    Next
    For $Ii = 1 To $n * 2 ;(for example)
        $Key_log_New = RegEnumKey($Key_log, $Ii)
        If @error <> 0 Then
            ExitLoop
        Else
            log_reg($Key_log & "\" & $Key_log_New)
        EndIf
    Next
EndFunc   ;==>log_reg


Func Key($K_key)

    $Str_mid = StringMid($K_key, 1, 2)
    If $Str_mid = "[-" Then
        $Key = ""
        $Del_key = StringTrimLeft($K_key, 2)
        $Del_key = StringTrimRight($Del_key, 1)
        FileWrite($AU3, "RegDelete('" & $Del_key & "')" & @CRLF)
    Else
        $K_key = StringReplace($K_key, "HKEY_CURRENT_USER", $Temp_reg & "\HKCU")
        $K_key = StringReplace($K_key, "HKEY_CLASSES_ROOT", $Temp_reg & "\HKCR")
        $K_key = StringReplace($K_key, "HKEY_LOCAL_MACHINE", $Temp_reg & "\HKLM")
        $K_key = StringReplace($K_key, "HKEY_USERS", $Temp_reg & "\HKU")
        $K_key = StringReplace($K_key, "HKEY_CURRENT_CONFIG", $Temp_reg & "\HKCC")
        FileWrite($Reg2, $K_key & @CRLF)
    EndIf
EndFunc   ;==>Key


Func value1($Line, $k)
    $Error = 0
    $Replace = StringReplace($Line, " ", "")
    If StringInStr($Replace, '" = -') = 0 Then
        FileWrite($Reg2, $Line & @CRLF)
    Else
        $k = StringTrimLeft($k, 2)
        $k = StringTrimRight($k, 1)
        $Line = StringTrimLeft($Line, 1)
        $Ln = StringLen($Line)
        $Nu = $Ln
        Do
            $Mid = StringMid($Line, $Nu, 1)
            If $Mid = '"' Then
                $Line = StringLeft($Line, $Nu - 1)
                ExitLoop
            Else
                $Nu = $Nu - 1
            EndIf
            If $Nu < 0 Then
                $Error = 1
                ExitLoop
            EndIf
        Until 1 = 2
        If Not $Error = 1 Then FileWrite($AU3, 'RegDelete("' & $k & '", "' & $Line & '")' & @CRLF)
    EndIf
EndFunc   ;==>value1


Func value2($Line, $k)
    $Error2 = 0
    $Replace = StringReplace($Line, " ", "")
    If $Replace = "@ = -" Then
        $k = StringTrimLeft($k, 2)
        $k = StringTrimRight($k, 1)
        $Line = StringTrimLeft($Line, 1)
        $Ln = StringLen($Line)
        $Nu = $Ln
        Do
            $Mid = StringMid($Line, $Nu, 1)
            If $Mid = '"' Then
                $Line = StringLeft($Line, $Nu - 1)
                ExitLoop
            Else
                $Nu = $Nu - 1
            EndIf
            If $Nu < 0 Then
                $Error = 1
                ExitLoop
            EndIf
        Until 1 = 2
        If Not $Error2 = 1 Then FileWrite($AU3, 'RegDelete("' & $k & '", "")' & @CRLF)
    Else
        FileWrite($Reg2, $Line & @CRLF)
    EndIf
EndFunc   ;==>value2

Func RegFixes($iString)
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)([a-z]):\\Program Files([ x86\(\)]*)\\Common Files\\', '$1$2' & "' & @CommonFilesDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)([a-z]):\\Program Files([ x86\(\)]*)\\', '$1$2' & "' & @ProgramFilesDir & '\\")

    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)([a-z]):\\Windows\\System32\\', '$1$2' & "' & @SystemDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)([a-z]):\\Windows\\Syswow64\\', '$1$2' & "' & @SystemDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)([a-z]):\\Windows\\', '$1$2' & "' & @WindowsDir & '\\")

    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & @UserProfileDir & "\\AppData\\Local\\Temp\\", '$1$2' & "' & @TempDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & StringRegExpReplace(FileGetShortName(@UserProfileDir & "\AppData\Local\Temp\"), '\\', '\\\\'), '$1$2' & "' & @TempDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & @UserProfileDir & "\\Local Settings\\Temp\\", '$1$2' & "' & @TempDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & StringRegExpReplace(FileGetShortName(@UserProfileDir & "\Local Settings\Temp\"), '\\', '\\\\'), '$1$2' & "' & @TempDir & '\\")

    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & @UserProfileDir & "\\AppData\\Local\\", '$1$2' & "' & @UserProfileDir & '\\Local\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & StringRegExpReplace(FileGetShortName(@UserProfileDir & "\AppData\Local\"), '\\', '\\\\'), '$1$2' & "' & @UserProfileDir & '\\Local\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & @UserProfileDir & "\\Local Settings\\", '$1$2' & "' & @UserProfileDir & '\\Local\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & StringRegExpReplace(FileGetShortName(@UserProfileDir & "\Local Settings\"), '\\', '\\\\'), '$1$2' & "' & @UserProfileDir & '\\Local\\")

    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & @UserProfileDir & "\\AppData\\Roaming\\", '$1$2' & "' & @AppDataDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & StringRegExpReplace(FileGetShortName(@UserProfileDir & "\AppData\Roaming\"), '\\', '\\\\'), '$1$2' & "' & @AppDataDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & @UserProfileDir & "\\Application Data\\", '$1$2' & "' & @AppDataDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & StringRegExpReplace(FileGetShortName(@UserProfileDir & "\Application Data\"), '\\', '\\\\'), '$1$2' & "' & @AppDataDir & '\\")

    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & @UserProfileDir & "\\Desktop\\", '$1$2' & "' & @DesktopDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & StringRegExpReplace(FileGetShortName(@UserProfileDir & "\Desktop\"), '\\', '\\\\'), '$1$2' & "' & @DesktopDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & @UserProfileDir & "\\Desktop\\", '$1$2' & "' & @DesktopDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & StringRegExpReplace(FileGetShortName(@UserProfileDir & "\Desktop\"), '\\', '\\\\'), '$1$2' & "' & @DesktopDir & '\\")

    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)([a-z]):\\Users\\Public\\Desktop\\', '$1$2' & "' & @DesktopCommonDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)([a-z]):\\Documents and Settings\\All Users\\Desktop\\', '$1$2' & "' & @DesktopCommonDir & '\\")

    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & @UserProfileDir & "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\", '$1$2' & "' & @ProgramsDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & StringRegExpReplace(FileGetShortName(@UserProfileDir & "\AppData\Roaming\Microsoft\\Windows\Start Menu\"), '\\', '\\\\'), '$1$2' & "' & @ProgramsDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & @UserProfileDir & "\\Start Menu\\Programs\\", '$1$2' & "' & @ProgramsDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)' & StringRegExpReplace(FileGetShortName(@UserProfileDir & "\Start Menu\Programs\"), '\\', '\\\\'), '$1$2' & "' & @ProgramsDir & '\\")

    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)([a-z]):\\Users\\Public\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\', '$1$2' & "' & @ProgramsCommonDir & '\\")
    $iString = StringRegExpReplace($iString, "(?i)(')" & '("?)([a-z]):\\Documents and Settings\\All Users\\Start Menu\\Programs\\', '$1$2' & "' & @ProgramsCommonDir & '\\")

    $iString = StringRegExpReplace($iString, "'' & ", '')

    Return $iString
EndFunc   ;==>RegFixes

Edited by Varian

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...