원본: Sigi2's Reg2Au3 (http://www.autoitscript.com/forum/topic/7771-reg2au3/)
Global $g_file_in, $g_file_in_handle, $g_file_in_size, $g_file_out, $g_file_out_handle
Global $g_reg_delete, $g_reg_key, $g_reg_valuename, $g_reg_type, $g_reg_valuedata
Global $g_eof, $g_lines_written, $g_lines_read, $g_version, $g_wow64
;set default params
$g_version = 0
$step = 0 ; 0 = None, 1 = Key, 2 = Value
$g_reg_delete = False
$g_reg_key = ""
$g_reg_valuename = ""
$g_reg_type = ""
$g_reg_valuedata = ""
$g_eof = False
$g_lines_written = 0
$g_lines_read = 0
$g_wow64 = False
;start and fileselect-dialogs
$g_file_in = FileOpenDialog("reg2bat - 원본 파일 선택", "", "Registry File (*.reg)", 1)
If @error Then _Error("원본 파일이 필요합니다")
$g_file_in_size = FileGetSize($g_file_in)
If $g_file_in_size = 0 Then _Error("원본 파일이 비어있습니다")
$g_file_out = FileSaveDialog("reg2bat - 대상 파일 선택", "", "Batch File (*.bat)", 18, _File_Name($g_file_in) & ".bat")
If @error Then _Error("대상 파일이 필요합니다")
_Files_Open()
;main script
While 1
$line = _File_Read_Line()
Select
Case $g_eof And $line = ""
If $step = 1 Then _File_Write()
ExitLoop
Case $line = ""
Case $g_version = 0
If $line = "REGEDIT4" Then $g_version = 4
If $line = "Windows Registry Editor Version 5.00" Then $g_version = 5
Case StringLeft($line, 1) = "[" And StringRight($line, 1) = "]" ;New REG-Key
$g_reg_valuename = ""
$g_reg_type = ""
$g_reg_valuedata = ""
$line = StringRegExpReplace($line, "\[HKEY_([A-Z])[A-Z]+_?([A-Z]?)[A-Z]*", "HK$1$2")
$line = StringTrimRight($line, 1)
If $step = 1 And StringInStr($line, $g_reg_key) = 0 Then _File_Write()
$g_reg_key = $line
If StringLeft($g_reg_key, 1) = "-" Then ;키 삭제
$g_reg_delete = True
$g_reg_key = StringTrimLeft($g_reg_key, 1)
_File_Write()
$step = 0
Else
$step = 1
EndIf
Case $step = 0
Case Else
;Value Name
$step = 2
$err = 0
If StringLeft($line, 2) = "@=" Then
$g_reg_valuename = "@"
$line = StringTrimLeft($line, 2)
If $line = "-" Then $err = 1
ElseIf StringLeft($line, 1) = '"' Then
$pos = 0
$esc = False
$g_reg_valuename = ""
For $i = 1 To StringLen($line)
$char = StringMid($line, $i, 1)
Select
Case $esc
$esc = False
$g_reg_valuename &= $char
Case $char = '\'
$esc = True
Case $char = '"'
If $i > 1 Then
$pos = $i + 1
ExitLoop
EndIf
Case Else
$g_reg_valuename &= $char
EndSelect
Next
If StringMid($line, $pos, 1) = "=" Then
$line = StringTrimLeft($line, $pos)
Else
$err = 2
EndIf
Else
$err = 3
EndIf
If $err Then
_Warning("잘못된 값 이름 구문 - " & $err)
ContinueLoop
EndIf
;Value Data
If StringLeft($line, 1) = '"' And StringRight($line, 1) = '"' Then
$g_reg_type = "REG_SZ"
$pos = 0
$esc = False
$g_reg_valuedata = ""
For $i = 1 To StringLen($line)
$char = StringMid($line, $i, 1)
Select
Case $esc
$esc = False
$g_reg_valuedata &= $char
Case $char = '\'
$esc = True
Case $char = '"'
If $i > 1 Then
$pos = $i + 1
ExitLoop
EndIf
Case Else
$g_reg_valuedata &= $char
EndSelect
Next
ElseIf StringLeft($line, 1) = "-" Then
$g_reg_delete = True
Else
$type = StringLeft($line, StringInStr($line, ":"))
$line = StringTrimLeft($line, StringLen($type))
Switch $type
Case "dword:"
$g_reg_type = "REG_DWORD"
$g_reg_valuedata = Dec($line)
Case "hex(7):"
$g_reg_type = "REG_MULTI_SZ"
$g_reg_valuedata = _Hex_Convert($line)
Case "hex(2):"
$g_reg_type = "REG_EXPAND_SZ"
$g_reg_valuedata = _Hex_Convert($line)
Case "hex:"
$g_reg_type = "REG_BINARY"
$g_reg_valuedata = StringReplace($line, ",", "")
Case Else
If StringLeft($type, 3) = "hex" Then
_Warning("지원하지 않는 키 형식 - " & $type)
Else
_Warning("잘못된 키 형식 구문")
EndIf
ContinueLoop
EndSwitch
EndIf
_File_Write()
EndSelect
WEnd
_Files_Close()
;returns converted hex-string suitable to regwrite
Func _Hex_Convert($hex)
Local $char, $string
$string = ""
For $i = 1 To StringLen($hex)
$char = Chr(Dec(StringMid($hex, $i, 2)))
If $char = Chr(0) Then $char = @LF
$string &= $char
If $g_version = 5 Then
$i += 5
Else
$i += 2
EndIf
Next
While StringRight($string, 1) = @LF
$string = StringTrimRight($string, 1)
WEnd
Return $string
EndFunc ;==>_Hex_Convert
;extract the filename without extension
Func _File_Name($file)
$fn = StringRight($file, StringLen($file) - StringInStr($file, "\", 0, -1))
If StringInStr($fn, ".") Then $fn = StringLeft($fn, StringInStr($fn, ".") - 1)
Return $fn
EndFunc ;==>_File_Name
;returns a string containing the commandline
Func _Create_Commandline()
Local $cmdline = ''
If $g_reg_delete Then
Else
$g_reg_key = _Clean_String($g_reg_key)
$g_reg_key = StringReplace($g_reg_key, '\\"', '\\\"')
$cmdline = 'REG ADD "' & $g_reg_key & '" /f'
If $g_reg_valuename <> "" Then
$g_reg_valuedata = StringReplace($g_reg_valuedata, '%', '^%') ;환경변수
If $g_reg_valuename = "@" Then
$cmdline &= ' /ve'
Else
$cmdline &= ' /v "' & _Clean_String($g_reg_valuename) & '"'
EndIf
$cmdline &= ' /t ' & $g_reg_type
$cmdline &= ' /d "' & _Clean_String($g_reg_valuedata) & '"'
EndIf
EndIf
$g_reg_valuename = ""
$g_reg_type = ""
$g_reg_valuedata = ""
Return $cmdline
EndFunc ;==>_Create_Commandline
;cleans given String from @LF
Func _Clean_String($val)
$val = StringReplace($val, '"', '\"')
$val = StringReplace($val, @LF, '" & @lf & "') ; ??????
Return $val
EndFunc ;==>_Clean_String
;open the input and output files
Func _Files_Open()
$g_file_in_handle = FileOpen($g_file_in, 0)
If $g_file_in_handle = -1 Then _Error("원본 파일을 열 수 없습니다.")
$g_file_out_handle = FileOpen($g_file_out, 2)
If $g_file_out_handle = -1 Then _Error("대상 파일을 열 수 없습니다.")
EndFunc ;==>_Files_Open
;reads a line from a file
Func _File_Read_Line()
Local $line, $char, $ascii, $eol, $read
$line = ""
Do
$eol = True
$read = FileReadLine($g_file_in_handle)
If @error = -1 Then $g_eof = True
$g_lines_read += 1
$read = StringStripWS($read, 3)
If StringRight($read, 1) = "\" Then
$read = StringTrimRight($read, 1)
$eol = False
EndIf
$line &= $read
Until $eol
Return $line
EndFunc ;==>_File_Read_Line
;Write Commandline to Outputfile
Func _File_Write()
Local $result
$result = FileWriteLine($g_file_out_handle, _Create_Commandline())
If $result = 0 Then _Error($g_file_in & " 파일을 작성할 수 없습니다.")
$g_lines_written += 1
EndFunc ;==>_File_Write
;close the input and output files
Func _Files_Close()
If $g_file_in_handle Then FileClose($g_file_in_handle)
If $g_file_out_handle Then FileClose($g_file_out_handle)
EndFunc ;==>_Files_Close
;cleanup output files
Func _Files_Cleanup()
FileDelete($g_file_out)
EndFunc ;==>_Files_Cleanup
;errorhandling
Func _Error($message)
_Files_Close()
_Files_Cleanup()
MsgBox(16, "reg2bat", "오류:" & @CR & $message)
Exit
EndFunc ;==>_Error
Func _Warning($message)
MsgBox(4144, "reg2bat", "경고:" & @CR & $message & @CR & "줄: " & $g_lines_read)
EndFunc ;==>_Error