Código
#Region " Is Registry File " ' [ Is Registry File Function ] ' ' // By Elektro H@cker ' ' Examples : ' MsgBox(IsRegFile("C:\RegistryFile.reg")) ' IsRegistryFile Private Function IsRegFile(ByVal RegistryFile As String) As Boolean Dim Regedit_Signature As String = "windows registry editor version 5.00" End Function #End Region
El núcleo de mi programa REG2BAT, mejorado para soportar caracteres inválidos por Batch (para escaparlos)
Código
#Region " Reg2Bat " ' [ Reg2Bat Function ] ' ' // By Elektro H@cker ' ' Examples : ' MsgBox(Reg2Bat("C:\Registry.reg")) Public Enum REG2BAT_Format As Int16 BINARY = 1 DWORD = 2 QWORD = 3 EXPAND_SZ = 4 MULTI_SZ = 5 REG_SZ = 0 End Enum ' Reg2Bat Private Function Reg2Bat(ByVal Reg_File As String) As String ' Source Input ' Join he lines, delete the Regedit linebreaks characters: "\ ", and then split the lines. Dim RegFile() As String = Split( _ .Replace("\@@@Reg2Bat@@@ ", "") _ .Replace("@@@Reg2Bat@@@", Environment.NewLine), _ Environment.NewLine) Dim RegLine As String = String.Empty ' Where the Regedit Line will be stored. Dim RegKey As String = String.Empty ' Where the Regedit Key will be stored. Dim RegVal As String = String.Empty ' Where the Regedit Value will be stored. Dim RegData As String = String.Empty ' Where the Regedit Data will be stored. Dim Batch_Commands As String = String.Empty ' Where the decoded Regedit strings will be stored. Batch_Commands &= ":: Converted with REG2BAT by Elektro H@cker" Batch_Commands &= Environment.NewLine & Environment.NewLine Batch_Commands &= "@Echo OFF" Batch_Commands &= Environment.NewLine & Environment.NewLine ' Start reading the Regedit File For X As Int64 = 0 To RegFile.LongLength - 1 RegLine = RegFile(X).Trim Select Case True Case RegLine.StartsWith(";") ' Comment line Batch_Commands &= Environment.NewLine Batch_Commands &= String.Format("REM {0}", RegLine.Substring(1, RegLine.Length - 1).Trim) Batch_Commands &= Environment.NewLine Case RegLine.StartsWith("[-") ' Key to delete RegKey = RegLine.Substring(2, RegLine.Length - 3).Trim Batch_Commands &= String.Format("REG DELETE ""{0}"" /F", RegKey) Batch_Commands &= Environment.NewLine Case RegLine.StartsWith("[") ' Key to add RegKey = RegLine.Substring(1, RegLine.Length - 2).Trim Batch_Commands &= String.Format("REG ADD ""{0}"" /F", RegKey) Batch_Commands &= Environment.NewLine Case RegLine.StartsWith("@=") ' Default Value to add RegData = Split(RegLine, "@=", , CompareMethod.Text).Last Batch_Commands &= String.Format("REG ADD ""{0}"" /V """" /D {1} /F", RegKey, RegData) Batch_Commands &= Environment.NewLine Case RegLine.StartsWith("""") _ AndAlso RegLine.Split("=").Last = "-" ' Value to delete RegVal = RegLine.Substring(1, RegLine.Length - 4) Batch_Commands &= String.Format("REG DELETE ""{0}"" /V ""{1}"" /F", RegKey, RegVal) Batch_Commands &= Environment.NewLine Case RegLine.StartsWith("""") ' Value to add ' Check data type: Select Case RegLine.Split("=")(1).Split(":")(0).ToLower Case "hex" ' Binary RegVal = Format_Regedit_String(Get_Regedit_Value(RegLine, REG2BAT_Format.BINARY)) RegData = Get_Regedit_Data(RegLine, REG2BAT_Format.BINARY) Batch_Commands &= String.Format("REG ADD ""{0}"" /V ""{1}"" /T ""REG_BINARY"" /D ""{2}"" /F", RegKey, RegVal, RegData) Batch_Commands &= Environment.NewLine Case "dword" ' DWORD (32 bit) RegVal = Format_Regedit_String(Get_Regedit_Value(RegLine, REG2BAT_Format.DWORD)) RegData = Get_Regedit_Data(RegLine, REG2BAT_Format.DWORD) Batch_Commands &= String.Format("REG ADD ""{0}"" /V ""{1}"" /T ""REG_DWORD"" /D ""{2}"" /F", RegKey, RegVal, RegData) Batch_Commands &= Environment.NewLine Case "hex(b)" ' QWORD (64 bIT) RegVal = Format_Regedit_String(Get_Regedit_Value(RegLine, REG2BAT_Format.QWORD)) RegData = Get_Regedit_Data(RegLine, REG2BAT_Format.QWORD) Batch_Commands &= String.Format("REG ADD ""{0}"" /V ""{1}"" /T ""REG_QWORD"" /D ""{2}"" /F", RegKey, RegVal, RegData) Batch_Commands &= Environment.NewLine Case "hex(2)" ' EXPAND SZ RegVal = Format_Regedit_String(Get_Regedit_Value(RegLine, REG2BAT_Format.EXPAND_SZ)) RegData = Format_Regedit_String(Get_Regedit_Data(RegLine, REG2BAT_Format.EXPAND_SZ)) Batch_Commands &= String.Format("REG ADD ""{0}"" /V ""{1}"" /T ""REG_EXPAND_SZ"" /D ""{2}"" /F", RegKey, RegVal, RegData) Batch_Commands &= Environment.NewLine Case "hex(7)" ' MULTI SZ RegVal = Format_Regedit_String(Get_Regedit_Value(RegLine, REG2BAT_Format.MULTI_SZ)) RegData = Format_Regedit_String(Get_Regedit_Data(RegLine, REG2BAT_Format.MULTI_SZ)) Batch_Commands &= String.Format("REG ADD ""{0}"" /V ""{1}"" /T ""REG_MULTI_SZ"" /D ""{2}"" /F", RegKey, RegVal, RegData) Batch_Commands &= Environment.NewLine Case Else ' REG SZ RegVal = Format_Regedit_String(Get_Regedit_Value(RegLine, REG2BAT_Format.REG_SZ)) RegData = Format_Regedit_String(Get_Regedit_Data(RegLine, REG2BAT_Format.REG_SZ)) Batch_Commands &= String.Format("REG ADD ""{0}"" /V ""{1}"" /T ""REG_SZ"" /D ""{2}"" /F", RegKey, RegVal, RegData) Batch_Commands &= Environment.NewLine End Select End Select Next Return Batch_Commands End Function ' Get Regedit Value Private Function Get_Regedit_Value(ByVal Line As String, ByVal REG2BAT_Format As REG2BAT_Format) As String Dim str As String = Nothing Select Case REG2BAT_Format Case REG2BAT_Format.BINARY : str = Split(Line, "=hex:", , CompareMethod.Text).First Case REG2BAT_Format.DWORD : str = Split(Line, "=dword:", , CompareMethod.Text).First Case REG2BAT_Format.QWORD : str = Split(Line, "=hex(b):", , CompareMethod.Text).First Case REG2BAT_Format.EXPAND_SZ : str = Split(Line, "=Hex(2):", , CompareMethod.Text).First Case REG2BAT_Format.MULTI_SZ : str = Split(Line, "=Hex(7):", , CompareMethod.Text).First Case REG2BAT_Format.REG_SZ : str = Split(Line, """=""", , CompareMethod.Text).First Case Else : Return Nothing End Select If str.StartsWith("""") Then str = str.Substring(1, str.Length - 1) If str.EndsWith("""") Then str = str.Substring(0, str.Length - 1) Return str End Function ' Get Regedit Data Private Function Get_Regedit_Data(ByVal Line As String, ByVal REG2BAT_Format As REG2BAT_Format) As String Dim Data As String = Nothing Select Case REG2BAT_Format Case REG2BAT_Format.BINARY Return Split(Line, (Split(Line, "=hex:", , CompareMethod.Text).First & "=hex:"), , CompareMethod.Text).Last.Replace(",", "") Case REG2BAT_Format.DWORD Return "0x" & Split(Line, (Split(Line, "=dword:", , CompareMethod.Text).First & "=dword:"), , CompareMethod.Text).Last.Replace(",", "") Case REG2BAT_Format.QWORD Line = StrReverse(Split(Line, (Split(Line, "=hex(b):", , CompareMethod.Text).First & "=hex(b):"), , CompareMethod.Text).Last.Replace(",", "")) For Each [byte] In Line.Split(",") : Data &= StrReverse([byte]) : Next Return Data Case REG2BAT_Format.EXPAND_SZ Line = Split(Line, (Split(Line, "=Hex(2):", , CompareMethod.Text).First & "=hex(2):"), , CompareMethod.Text).Last.Replace(",00", "").Replace("00,", "") For Each [byte] In Line.Split(",") : Data &= Chr(Val("&H" & [byte])) : Next Return Data.Replace("""", "\""") Case REG2BAT_Format.MULTI_SZ Line = Split(Line, (Split(Line, "=Hex(7):", , CompareMethod.Text)(0) & "=hex(7):"), , CompareMethod.Text).Last.Replace(",00,00,00", ",\0").Replace(",00", "").Replace("00,", "") For Each [byte] In Line.Split(",") If [byte] = "\0" Then Data &= "\0" ' Line separator for multiline. Else Data &= Chr(Val("&H" & [byte])) End If Next Return Data.Replace("""", "\""") Case REG2BAT_Format.REG_SZ Data = Split(Line, (Split(Line, """=""", , CompareMethod.Text)(0) & """="""), , CompareMethod.Text).Last Data = Data.Substring(0, Data.Length - 1) Return Data Case Else Return Nothing End Select End Function ' Format Regedit String Private Function Format_Regedit_String(ByVal str As String) As String str = str.Replace("%", "%%") If Not str.Contains("""") Then Return str str = str.Replace("\""", """") Dim strArray() As String = str.Split("""") For num As Long = 1 To strArray.Length - 1 Step 2 strArray(num) = strArray(num).Replace("^", "^^") ' This replace need to be THE FIRST. strArray(num) = strArray(num).Replace("<", "^<") strArray(num) = strArray(num).Replace(">", "^>") strArray(num) = strArray(num).Replace("|", "^|") strArray(num) = strArray(num).Replace("&", "^&") ' strArray(num) = strArray(num).Replace("\", "\\") Next Return String.Join("\""", strArray) End Function #End Region